Using PowerCLI to Extend VMDK and Windows OS Disk
I wanted to share this handy script i created when you have a need to extend a disk.
This script can be extremely handy as it will take care of extending the VMDK and then going to the OS and then performing the expansion there as well. This has been helpful when needing to extend a large batch of servers (60+).
Syntax: .\ExtendVMDKandGuest VMNAME
You will then be prompted to enter in the Hard Disk #, the size you want to be extended, and then which drive letter that volume belongs to.
The code and Github Link can be found below.
1# PowerCLI Script for adding vmdk to VM and extending disk in windows
2# @davidstamen
3# http://davidstamen.com
4
5param(
6 [string]$VM
7)
8$VM = Get-VM $VM
9Get-VM $VM|Get-HardDisk|FT Parent, Name, CapacityGB -Autosize
10$HardDisk = Read-Host "Enter VMware Hard Disk (Ex. 1)"
11$HardDisk = "Hard Disk " + $HardDisk
12$HardDiskSize = Read-Host "Enter the new Hard Disk size in GB (Ex. 50)"
13$VolumeLetter = Read-Host "Enter the volume letter (Ex. c,d,e,f)"
14Get-HardDisk -vm $VM | where {$_.Name -eq $HardDisk} | Set-HardDisk -CapacityGB $HardDiskSize -Confirm:$false
15Invoke-VMScript -vm $VM -ScriptText "echo rescan > c:\diskpart.txt && echo select vol $VolumeLetter >> c:\diskpart.txt && echo extend >> c:\diskpart.txt && diskpart.exe /s c:\diskpart.txt" -ScriptType BAT