Resize Guest System Partition with PowerCLI

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.

1
2
3
4
5
6
7
8
9
10
$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

3 comments

  1. Adam Stahl says:

    I had to do this very same thing a few days ago against 52 VMs. I came up with something very similar and just wanted to share it. My method works without storing username/password in the script. It’s not pure powershell, but it worked nicely for me.

    Extend_Disk.ps1:
    BEGIN
    $vCenter=”vmvc.rhsnet.org”
    $NewSizeGB=50
    $vms=(“VM1″,”VM2″,”VM3″)

    connect-viserver $vCenter

    foreach ($vm in $vms) {
    get-vm $vm | get-harddisk | where {$_.name -eq “Hard Disk 1″} | set-harddisk -confirm:$false -capacityGB $NewSizeGB
    copy diskpart.txt \\$vm\c$\windows\temp\
    psexec \\$vm diskpart /s C:\windows\temp\diskpart.txt
    }

    disconnect-viserver -Confirm:$false
    END

    diskpart.txt:
    rescan
    select volume 2
    extend

  2. @Adam, thanks for your comment. I didn’t know about the ‘rescan’ command inside diskpart — I’ve actually made use of it a couple times since I saw your post. Thanks for the tip!

    Just for reference, the Invoke-VMScript cmdlet does not require the username and password to be stored in the script. By default the cmdlet works kind of like psexec does in your example, where the current running user credentials are used. In my scenario the user running the script didn’t have admin rights to the VMs that were being extend and alternate credentials were needed.

  3. daisy-love says:

    Try partition software is much better than disk management.

    Resizing system partition requires careful operation for wrong action might cause system crash.

    You’d better first back up your system and then resize system partition with partition softare

    http://www.partition-tool.com/easeus-partition-manager/extend-system-partition.htm

Leave a Reply

Your email address will not be published. Required fields are marked *


*

Notify me of followup comments via e-mail. You can also subscribe without commenting.