Dear All,
I would like to extend a disk to certain size on multiple VM (Morethan 200), presume it is huge time taking, can anyone please suggest with any script which would allow me to do it through automation.
Thanks in Advance.
Rachis!
# PowerCLI-Extend-Disks.ps1
# Must be run in VMware PowerCLI
# Written by Jason Pearce, www.jasonpearce.com, (2015 June)
# Inspiration from Brian Wuchner, Adam Stahl, and of course Luc Dekens (LucD)
# BEGIN Variables
# vCenter that contains target VMs
$vCenter="vcenter.example.local"
# New hard drive size you want (should be larger than current drive size)
$NewCapacityGB=60
# One or more virtual machines you want to target (modify and uncomment this line)
# $VMs=(Get-Cluster -Name "ClusterName" | Get-VM -Name "VM-Prefix-*")
# $VMs=("VM1","VM2","VM3")
# Virtual Machine Windows Credentials (a local admin account)
$GuestUser="administrator"
$GuestPassword="password"
# END Variables
# BEGIN Script
# Connect to vCenter via PowerCLI
Connect-VIServer $vCenter
# BEGIN foreach loop
foreach ($VM in $VMs) {
# Have vSphere PowerCLI increase the size of the first hard drive in each target VM
Get-VM $VM | Get-HardDisk | Where-Object {$_.Name -eq "Hard Disk 1"} | Set-HardDisk -CapacityGB $NewCapacityGB -Confirm:$false
# Run DISKPART in the guest OS of each of the specified virtual machines
Invoke-VMScript -VM $VM -ScriptText "ECHO RESCAN > C:\DiskPart.txt && ECHO SELECT Volume C >> C:\DiskPart.txt && ECHO EXTEND >> C:\DiskPart.txt && ECHO EXIT >> C:\DiskPart.txt && DiskPart.exe /s C:\DiskPart.txt && DEL C:\DiskPart.txt /Q" -ScriptType BAT -GuestUser $GuestUser -GuestPassword $GuestPassword
}
# END foreach loop
# Disconnect from vCenter
Disconnect-VIserver -Confirm:$false
# END Script
Just a small remark, that scrip will only work for VMs that have a Windows Guest OS, and then only for specific (recent) versions.
If you have a Linux distro as the guest OS, you will need to extend the disk inside the guest OS differently.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
