Categories
How-To Software Technical

VMware vSphere PowerCLI – Power Off & On Guests

Looking for a method to restart a non-persistent environment in ESXi or vCenter, I was able to use PowerCLI to issue the commands needed. Originally I was looking to automate the recovery of a snapshot when it dawned on me that a non-persistent disk would facilitate the same thing.  However I would need to power off the server and back on again for it to work.

And this is what came from my searching and trial and error

Powering Off

Connect-VIServer -Server 1.1.1.1
 
# Get All the ESX Hosts
$ESXSRV = Get-VMHost
 
# For each of the VMs on the ESX hosts
Foreach ($VM in ($ESXSRV | Get-VM)){
    # Shutdown the guest cleanly
    $VM | Shutdown-VMGuest -Confirm:$false
}
 
# Set the amount of time to wait before forcing power off
$waittime = 300 #Seconds
 
$Time = (Get-Date).TimeofDay
do {
    # Wait for the VMs to be Shutdown
    sleep 1.0
    $timeleft = $waittime - ($Newtime.seconds)
    $numvms = ($ESXSRV | Get-VM | Where { $_.PowerState -eq "poweredOn" }).Count
    Write "Waiting for shutdown of $numvms VMs or until $timeleft seconds"
    $Newtime = (Get-Date).TimeofDay - $Time
    } until ((@($ESXSRV | Get-VM | Where { $_.PowerState -eq "poweredOn" }).Count) -eq 0 -or ($Newtime).Seconds -ge $waittime)
 
 
Write-Host "Shutdown Complete"

Powering On

Connect-VIServer -Server 1.1.1.1
 
# Get All the ESX Hosts
$ESXSRV = Get-VMHost
 
# For each of the VMs on the ESX hosts
Foreach ($VM in ($ESXSRV | Get-VM)){
    # Shutdown the guest cleanly
    $VM | Start-VM
}
 

Write-Host "Startup Complete"

Both – Power Off wait then Power On

Connect-VIServer -Server 1.1.1.1
 
# Get All the ESX Hosts
$ESXSRV = Get-VMHost
 
# For each of the VMs on the ESX hosts
Foreach ($VM in ($ESXSRV | Get-VM)){
    # Shutdown the guest cleanly
    $VM | Shutdown-VMGuest -Confirm:$false
}
 
# Set the amount of time to wait before forcing power off
$waittime = 300 #Seconds
 
$Time = (Get-Date).TimeofDay
do {
    # Wait for the VMs to be Shutdown
    sleep 1.0
    $timeleft = $waittime - ($Newtime.seconds)
    $numvms = ($ESXSRV | Get-VM | Where { $_.PowerState -eq "poweredOn" }).Count
    Write "Waiting for shutdown of $numvms VMs or until $timeleft seconds"
    $Newtime = (Get-Date).TimeofDay - $Time
    } until ((@($ESXSRV | Get-VM | Where { $_.PowerState -eq "poweredOn" }).Count) -eq 0 -or ($Newtime).Seconds -ge $waittime)
 
 
Write-Host "Shutdown Complete"

#For each of the VMs on the ESX hosts
Foreach ($VM in ($ESXSRV | Get-VM)){
    # Shutdown the guest cleanly
    $VM | Start-VM
}
 

Write-Host "Startup Complete"

Next was to use a batch file to run a task to execute the PowerCLI commands. This was done by doing the following set as a scheduled task

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -psconsolefile "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noexit -command c:\scripts\script_name_goes_here.ps1

 Thanks Hristo for your help

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.