VMware Cloud Community
desadi10
Enthusiast
Enthusiast

Power off server ESXi automatically

Hi,

Tomorrow, the technicians go to cut the electric current to do the maintenance of the company.

I need to power off the server ESXi automatically planning the hour with his virtual machines remotely before this correctly.

I would like to know if there is a possibility to power off the server correctly by command or by graphic interface without have to power off machine by machine, that is to say, to power off the server ESXi and the machines by the same time

Thanks so much.

Best Regards.

3 Replies
MBreidenbach0
Hot Shot
Hot Shot

For standalone hosts you can configure automatic VM startup/shutdown but this doesn't work well in a HA cluster.

For my lab environment I'm using PowerCLI scripts which do this (shutdown VMs, put hosts in maintenance mode, shutdown ESXi hosts).

Do you have a single or multiple ESXi hosts ?

0 Kudos
desadi10
Enthusiast
Enthusiast

Hi MBreidenbach0,

Thanks so much for your reply.

I have 2 hosts with their virtual machines but these hosts are not in a HA cluster.

The PowerCLI scritps that you use in you lab environment, were they created by yourself or they are downloaded by the vmware web site?

Thanks so much.

0 Kudos
MBreidenbach0
Hot Shot
Hot Shot

I wrote these scripts but they aren't really special; there are similar scripts available on the internet; just google for PowerCLI shutdown script and you should find a lot of them.

Here's one of them; it shuts down VMs of one host then puts the host in maintenance mode then shuts down the host. This only works with one host and it doesn't consider dependencies (like... shut down application server then sql server then domain controller.

connect-viserver $VMHost

###################################################################################################

# Shutdown VMs

###################################################################################################

write-host -ForegroundColor Green 'Shutting down running VMs...'

Get-VMHost $VMHost | Get-VM | Where-Object{$_.PowerState -eq 'PoweredOn'} | Shutdown-VMGuest -Confirm:$false | Out-Null

write-host -ForegroundColor Green 'Wait until all VMs are PoweredOff...'

$RunningVMsTotal = (Get-VMHost $VMHost | Get-VM | Where-Object{$_.PowerState -eq 'PoweredOn'} | Measure-Object).Count

if ($RunningVMsTotal -gt 0) {

do {

Start-Sleep 5

$RunningVMs = (Get-VMHost $VMHost | Get-VM | Where-Object{$_.PowerState -eq 'PoweredOn'} | Measure-Object).Count

$StoppedVMs = $RunningVMsTotal - $RunningVMs

Write-Progress -Activity "Waiting for VM to shut down" -status "$StoppedVMs of $RunningVMsTotal" -percentComplete ($StoppedVMs / $RunningVMsTotal * 100)

} while ($RunningVMs -ne 0)

}

###################################################################################################

# Enter Maintenance Mode

###################################################################################################

write-host -ForegroundColor Green "Set Maintenance Mode for $VMHost..."

Set-VMHost -VMHost $VMHost -State 'Maintenance' -Confirm:$false | Out-Null

write-host -ForegroundColor Green "Wait for Maintenance Mode ..."

do {

Start-Sleep 5

$VMHostState = (Get-VMHost $VMHost).State

} while ($VMHostState -ne 'Maintenance')

###################################################################################################

# Shutdown Host

###################################################################################################

write-host -ForegroundColor Green "Shutdown Host ..."

Stop-VMHost -VMHost $VMHost -Confirm:$false | Out-Null

disconnect-viserver $VMHost -Confirm:$false

}

}