We recently had a sudden requirement to increase the C: drives of a set of VMs to assist with an Operating system patching process – its not practical to to manually do that one by one – so PowerCLI to the rescue. So if you get a scenario where you need to expand an existing disk of a virtual machine by a specified amount of GB (especially a bulk of VMs), the following script will make life much easier.
Get the current size of the required disk
Use the code below to get the size (in GB) of the harddisk in question (here I’m getting the size of “hard disk 1”)
$vm_info_dsk = (Get-HardDisk -VM $vm_name | Where-Object {$_.Name -eq "Hard Disk 1"}).CapacityGB
You can make this a more expandable/usable code by making the Hard disk number a variable as well. With this you can change the name of the Hard disk easily without having to replace all the instances where the name was used.
$hard_disk_number = "Hard Disk 1"
$vm_info_dsk = (Get-HardDisk -VM $vm_name | Where-Object {$_.Name -eq $hard_disk_number}).CapacityGB
Setting the hard disk to the new size
You can change the size of the disk using the Set-HardDisk command. Here I’m going to assume the size of the disk needs to be increased by 10GB
$space_to_increase = 10
$new_capacity = $vm_info_dsk + $space_to_increase
Get-HardDisk -VM $vm_name | Where-Object {$_.Name -eq "Hard Disk 1"} | Set-HardDisk -capacityGB $new_capacity -confirm:$false
How it all comes together (the complete script)
Lets see how to put this all in to a robust expandable script.
The script will read a list of vms from the vm_list.txt text file (vms should be separated by a new line) which would be useful for bulk list of VMs – for more clarity on Get-Content read that post here. To change any parameter (different disk size, different harddisk) simply change the relevant variable.
#Read list from a text file vm_list.txt - separated by a new line
$vm_list = Get-Content "vm_list.txt"
$vcenter = "vCenter1"
$space_to_increase = 20
$hard_disk_number = "Hard Disk 1"
Connect-VIServer $vcenter
foreach($vm_name in $vm_list){
write-host "Fetching information of $vm_name"
$vm = Get-VM $vm_name
$vm_info_dsk = (Get-HardDisk -VM $vm_name | Where-Object {$_.Name -eq $hard_disk_number}).CapacityGB
Write-Host "$vm $hard_disk_number is $vm_info_dsk"
$new_capacity = $vm_info_dsk + $space_to_increase
Write-Host "new hard disk capacity $new_capacity"
Get-HardDisk -VM $vm_name | Where-Object {$_.Name -eq $hard_disk_number} | Set-HardDisk -capacityGB $new_capacity -confirm:$false
#Check if it is successfully set and display the new size
$vm_info_dsk = (Get-HardDisk -VM $vm_name | Where-Object {$_.Name -eq $hard_disk_number}).CapacityGB
Write-Host "$vm new hard disk 1 is $vm_info_dsk"
}
Disconnect-VIServer -Confirm:$false