Imagine having to expand disk for more than 20 VMs and you sadly opt to do it the click next and next and next and click next way? Well you know where I am going with this. I must admit I use to do it this way until a colleague wowed me with a script which copies files to windows temp directory and auto expand from disk mgmt. without having to login to the box… this served me well during my time at my at an Online Betting Company. I picked the one liner from his by expanding it and below is what I came up with….
Expand VM Hard Disk
#download pstools from https://docs.microsoft.com/en-us/sysinternals/downloads/psexec open zip and copy PsExec.exe & PsExec64 to scripts folder
#create diskpart.txt in scripts folder and include below
rescan
select volume 2
extend
# Run PowerCLI as domain admin account or enough privileges
# Path into script folder and save below as PS.1
$vCenter=”uk3p-vc01.lab.local”
$NewSizeGB=60
$vms=("Ron-Test01")
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.exe \\$vm diskpart /s C:\windows\temp\diskpart.txt
}
disconnect-viserver -Confirm:$false
diskpart.txt: # create diskpart.txt in scripts folder and include below. This gets copied using the account specified above to temp folder of the server you’re expanding.
#you can use a csv if you’re exanpanding more than one VM by calling below
$VMlist = import-csv –path c:\scripts\expandVMS.csv
get-vm $VMlist.Name | get-harddisk | where .....
Imagine having to expand the disk for over 20VMs using the manual approach. Clicking next and next and next and next? Well be my guest as per below….
Using the WebClient
Open VMware Infrastructure (VI) Client and connect to VirtualCenter or the ESX host.
Right-click the virtual machine.
Click Edit Settings.
Select Virtual Disk.
Increase the size of the disk.
Note: If this option is greyed out, the disk may be running on snapshots or the disk may be at the maximum allowed size depending on the block size of the datastore.Follow the steps in Increasing the size of a disk partition (1004071) so the guest operating system is aware of the change in disk size.
Below One Liner Can be used instead of the diskpart
Get-HardDisk -vm $vms | Where {$_.Name -eq "Hard disk 1"} | Set-HardDisk -CapacityGB 60 -ResizeGuestPartition -Confirm:$false
# Run PowerCLI as domain admin account or enough privileges
# Path into script folder and save below as PS.1
$vCenter=”uk3p-vc01.lab.local”
$NewSizeGB=60
$vms=("Ron-Test01")
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.exe \\$vm diskpart /s C:\windows\temp\diskpart.txt
}
disconnect-viserver -Confirm:$false
diskpart.txt: # create diskpart.txt in scripts folder and include below. This gets copied using the account specified above to temp folder of the server you’re expanding.
#you can use a csv if you’re exanpanding more than one VM by calling below
$VMlist = import-csv –path c:\scripts\expandVMS.csv
get-vm $VMlist.Name | get-harddisk | where .....
I have tried this script but disk expand only working in Vmware end not in server disk management. it shows disk unallocated space.
Powershell has a commandlet called Invoke-VmScript you can use to do this without having to use 3rd party pstools.
I love pstools and all, but psexec frequently fails if you don’t have SMB/CIFs to the box over the network, or if there are any RPC issues.
The only requirement for Invoke-VmScript is that Vmware tools be installed and functional on the VM Guest.
Thanks Jeremy for contributing to the community.
However I tried to modify the script but still its not working. it will working in Vmware end not in server disk management. it shows disk unallocated space. can you please share the exact script code please
Get-HardDisk -vm $vms | Where {$_.Name -eq “Hard disk 1”} | Set-HardDisk -CapacityGB 38 -ResizeGuestPartition -Confirm:$false
This can be used to extend the OS diskpart as well if needed
This is not supported on Windows 2019 servers
I am yet to test it in 2019 Server. I will do and update where needed.