Skip to content

Instantly share code, notes, and snippets.

@exchange12rocks
Last active November 25, 2020 19:36
Show Gist options
  • Save exchange12rocks/daabff57c5912454dfe9a4f3e5cc6c2d to your computer and use it in GitHub Desktop.
Save exchange12rocks/daabff57c5912454dfe9a4f3e5cc6c2d to your computer and use it in GitHub Desktop.
Dynamic VHDX is smaller than used space inside it
$VHDDesc = @{
Dynamic = @{
Letter = 'Z'
Size = 100GB
Path = 'B:\Temp\Dynamic'
}
Fixed = @{
Letter = 'Y'
Size = 70GB
}
}
New-VHD -Path ('{0}.vhdx' -f $VHDDesc.Dynamic.Path) -Dynamic -SizeBytes $VHDDesc.Dynamic.Size
$DiskNumber = (Mount-VHD -Path ('{0}.vhdx' -f $VHDDesc.Dynamic.Path) -Passthru).Number
Initialize-Disk -Number $DiskNumber -PartitionStyle GPT
New-Volume -DiskNumber $DiskNumber -FileSystem NTFS -DriveLetter $VHDDesc.Dynamic.Letter -FriendlyName DynamicVHDX
New-VHD -Path ('{0}:\Fixed.vhdx' -f $VHDDesc.Dynamic.Letter) -SizeBytes $VHDDesc.Fixed.Size -Fixed
$DiskNumber = (Mount-VHD -Path ('{0}:\Fixed.vhdx' -f $VHDDesc.Dynamic.Letter) -Passthru).Number
Initialize-Disk -Number $DiskNumber -PartitionStyle GPT
New-Volume -DiskNumber $DiskNumber -FileSystem NTFS -DriveLetter $VHDDesc.Fixed.Letter -FriendlyName StaticVHDX
$bytes = 1GB
[System.Security.Cryptography.RNGCryptoServiceProvider] $rng = New-Object System.Security.Cryptography.RNGCryptoServiceProvider
$rndbytes = New-Object byte[] $bytes
$rng.GetBytes($rndbytes)
[System.IO.File]::WriteAllBytes(('{0}:\test.txt' -f $VHDDesc.Fixed.Letter), $rndbytes)
$rng.GetBytes($rndbytes)
[System.IO.File]::WriteAllBytes(('{0}:\test2.txt' -f $VHDDesc.Fixed.Letter), $rndbytes)
'Files size: {0} GB' -f ((Get-Item Y:\*.txt | ForEach-Object { $_.Length } | Measure-Object -Sum).Sum / 1024 / 1024 / 1024); 'Fixed VHDX size (on volume {1}:): {0} GB' -f ((Get-Item ('{0}:\Fixed.vhdx' -f $VHDDesc.Dynamic.Letter)).Length / 1024 / 1024 / 1024), $VHDDesc.Dynamic.Letter; 'Dynamic VHDX size (supports volume {1}:): {0} GB' -f ((Get-Item ('{0}.vhdx' -f $VHDDesc.Dynamic.Path)).Length / 1024/1024/1024), $VHDDesc.Dynamic.Letter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment