Extend VM Guest Partition w/PowerCLi

Cover

How often do you have to increase your VMs’ partitions? Fairly often, right? You have to find your VM in the vSphere Client, then click Edit Settings and first to increase VM Hard disk. After that you have to open RDP to your VM, open Disk Management, choice Rescan Disks from the right click menu and only after that one more right click on the desired partition and Extend Volume …. Pooh!

What if I tell you that all this can be done from one place with no GUI at all (no vSphere Client, no Remote Desktop, no VM console)? And this magic place is PowerCLi console armed with my PowerCLi Vi-Module module!

There are two new functions to deal with VM Guest partitions: Get-VMGuestPartition and Expand-VMGuestPartition.

Get-VMGuestPartition

  • The function Get-VMGuestPartition is very simple to use. Just pipeline your VM(s) to the function and you are done.
Get-VM vm1 | Get-VMGuestPartition
Get-VM vm1, vm2 | Get-VMGuestPartition
Get-VM ‘vm[1-9]’ | Get-VMGuestPartition
Get-VM | Get-VMGuestPartition
  • The both Windows and Linux OS guests are supported. The only requirement is running VMTools! VM with no VMTools installed or running will be silently (with no output and no error) ignored.

01.Get-VMGuestPartition

  • The function returns objects, therefore you can filter and format the output.
Get-VM | Get-VMGuestPartition | ? {$_.FreeSpaceMB -lt 100} | Format-Table -AutoSize
Get-VM | Get-VMGuestPartition | ? {$_.'Usage%' -gt 95} | ogv
  • The Usage% property is generated by the New-PercentageBar function, included in the module.

02.Get-VMGuestPartition

Expand-VMGuestPartition

  • Why Expand- but not Extend-? Because Extend is not approved PowerShell verb.
Get-Verb e* | sort Verb
  • The Expand-VMGuestPartition is more complicated, interactive function and it has several prerequisites in addition to running VMTools!

  • Before we will be able to use the function we have to create at least two VICredentialStore Items, one is for ESXi hosts and another for VM Guests.

New-VICredentialStoreItem -Host VMHost -User root -Password $Your_root_Password
New-VICredentialStoreItem -Host VMGuest -User Administrator -Password $Your_Guest_Password
  • The New-VICredentialStoreItem cmdlet requires three parameters. The -User and -Password are actually credentials to interact with VM Host and VM Guest OS and -Host is only pointer to these credentials and we will utilize it in our function.

03.Expand-VMGuestPartition_NewVICredStoreItem

  • The only mandatory parameter for Expand-VMGuestPartition is -VM. Every other parameter is optional and may be supplied interactively at the function run time! This functionality is provided by Write-Menu function, included in the module.

Example

  • We will take a VM with two Hard disks.
Get-VM vm1 | Get-HardDisk | select Name, CapacityGB, Filename

04.Expand-VMGuestPartition_GetVmHardDisks

  • Let start the process.
Get-VM vm1 | Expand-VMGuestPartition
  • The function will ask you to choice ESXi host and VM Guest credentials.
  • After that the function provides you with three easy steps: [Step 1..3] choice desired VM Hard disk, [Step 2..3] choice resultant Hard disk size and [Step 3..3] choice partition to extend.

The interactive Steps

[Step 1..3]

  • Let see the steps in more details. The Step 1 allows you to choice VM Hard disk to increase. Please note there is Exit entry. This menu allows to extend partition without increasing the disk. What!!? May be someone already has increased it earlier or previous partition extension was failed but the disk was increased successfully. Things happen Emoj.

  • After the disk was chosen, you will get additional useful information about it and underlying Datastore.

[Step 2..3]

  • The Step 2 dynamically builds for you sizes menu. This menu driven by variables in the Begin { } statement. It depends on initial Hard disk size and those variables.

$MaxHddSize = 2000
$SizeJump1 = 10
$SizeJump2 = 50
$SizeJump3 = 100
$LastStep2 = 1000
  • You can edit them at your discretion. One of the important variables is $MaxHddSize. It restricts increasing VM Hard disks above 2TB (partition size limit for MBR disks). If you are using GPT disks in your environment, you can adjust this variable to a bigger value. Just remember there are another limits: NTFS is currently limited to 256TB for a single volume and Virtual disk size limited to 62TB.

[Step 3..3]

  • The last step is to choice partition to extend. At this moment there is no 100%-way to map VM Guest partitions to VM Hard disks and you have to do this manually Emoj. But there is good news, if you were wrong in your choice or you have supplied wrong guest credentials or something else went wrong you can repeat the process and at this time to choice Exit in the Step 1.

The final Step

  • The function is waiting for 40 seconds and calls the Get-VMGuestPartition to show the changes. This delay is needed for the changes to take effect.

The optional parameters

  • The function has several optional parameters, such -HostCred, -GuestCred, -Confirm and -Verbose.
Get-VM vm1 | Expand-VMGuestPartition -HostCred (Get-VICredentialStoreItem esxlab1) -GuestCred (Get-VICredentialStoreItem guestlab) -Confirm:$false -Verbose
  • For more details about them, please take a look at the content based help and examples.
Get-Help Get-VMGuestPartition
Get-Help Expand-VMGuestPartition -Examples
Get-Help Expand-VMGuestPartition -Full
Get-Help Expand-VMGuestPartition -Parameter guestcred

Summary

  • For fans of short commands there two aliases: exvmpart for the Expand-VMGuestPartition and gvmpart for the Get-VMGuestPartition.
Get-Command -Module Vi-Module -Noun vmguest*
Get-Alias -Definition Expand-VMGuestPartition, Get-VMGuestPartition
  • The Expand-VMGuestPartition returns True on success (was able to extend a partition) and False on fail.

  • The Expand-VMGuestPartition supports NT6 or above Windows guests only. Outdated guests like Windows XP or Windows 2003 Server are not supported (the guest limitation).

  • The Expand-VMGuestPartition utilizes embedded PowerCLi Invoke-VMScript cmdlet and Windows DISKPART.EXE command line utility.

Get-Help Invoke-VMScript –Online
diskpart /?
  • To get help about specific diskpart command, for example SELECT VOLUME, run the diskpart in interactive mode and execute help + your command.
diskpart
DISKPART> help sel vol
DISKPART> exit
  • The most diskpart commands have short aliases, like SELECT is a sel, VOLUMEvol, PARTITIONpart, etc. So, the following long command create partition primary can be converted to short one cre part pri.

  • Both the functions require running VMTools within target guests.

2 thoughts on “Extend VM Guest Partition w/PowerCLi

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s