Script

------------------------------------------------------------------------------------------

# 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
Add-PSSnapin vmWARE.VimAutomation.Core
$vc="vcname"
$date = Get-Date -Format 'yyyyMMdd'
$SourceVMs = "C:\PS\vmlist.txt"
$list = Get-Content $SourceVMs


# New hard drive size you want (should be larger than current drive size)
$NewCapacityGB=25
 
# 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="4444abcde!"
 
# END Variables
 
# BEGIN Script
 
# Connect to vCenter via PowerCLI
Connect-VIServer $vc

 
# BEGIN foreach loop
foreach ($VM in $list) {
 
# 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 2"}  | 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 E >> 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