I recently needed to resize system partitions on several Windows 2008R2 virtual machines. To do this with Set-HardDisk the virtual machines must be powered off and you need a helper VM. I was looking for a way to do this without downtime, as that can be arranged when executing the steps manually (grow the disk, log into the guest, rescan disks and then extend the partition). I came up with the following workaround and thought it would be worth sharing. The idea is to set the hard disk to the new size with Set-HardDisk and then use Invoke-VMScript to run diskpart from within the VM. I included the -ResizeGuestPartition switch on Set-HardDisk as that appears to complete the task of re-scanning for disks within disk manager.
[cc lang=”powershell”]
$guestName=”newDiskTest”
$guestUser=”Administrator”
$guestPass=”Aw3s0m3pwd”
$newSizeGB=40
Get-HardDisk -vm “newDiskTest” |
?{$_.name -eq “hard disk 1”} |
Set-HardDisk -CapacityKB ($newSizeGB*1MB) -ResizeGuestPartition -GuestUser $guestUser -GuestPassword $guestPass -confirm:$false -ErrorAction:SilentlyContinue
Invoke-VMScript -vm $guestName -ScriptText “echo select vol c > c:\diskpart.txt && echo extend >> c:\diskpart.txt && diskpart.exe /s c:\diskpart.txt” -GuestUser $guestUser -GuestPassword $guestPass -ScriptType BAT
[/cc]
