Thursday, January 26, 2012

Power off all servers in a resource pool

cls
Remove-Variable -Name * -Force -ErrorAction SilentlyContinue # remove any lingering variables
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue

Connect-VIServer visrv1
#Get All VMs in the specific cluster Resource Pool
$TESTVMS = Get-ResourcePool "RESOURCEPOOLNAME" | Get-VM
#For each of the VMs in the Resource Pool 
Foreach ($VM in ($TESTVMS))

 # Power Down the Test VM Guests 
 $VM | Shutdown-VMGuest -Confirm:$false
 } 
# St the amount of time to wait before assuming the guests are stuck  
$waittime = 200 #Seconds 
$Time = (Get-Date).TimeofDay 
do

#Wait for the VMs to be shutdown cleanly 
sleep 1.0 
$timeleft = $waittime - ($Newtime.seconds) 
$numvms = (Get-ResourcePool "SS_topaz" | Get-VM | Where { $_.PowerState -eq "poweredOn" }).Count 
Write "Waiting for startup of $numvms VMs or $timeleft seconds"
$Newtime = (Get-Date).TimeofDay - $Time
}
until ((@(Get-ResourcePool "RESOURCEPOOLNAME" | Get-VM | Where { $_.PowerState -eq "poweredOn" }).Count) -eq 0 -or ($Newtime).Seconds -ge $waittime) 
Write-Host "RESOURCEPOOLNAME Test VM Power On Complete"
Disconnect-VIServer visrv1 -Confirm:$false

No comments:

Post a Comment