Many development teams face similar infrastructure challenges - ESXi hosts left running overnight in environments with insufficient cooling. While ESXi is designed for continuous operation, environmental factors sometimes necessitate scheduled shutdowns.
VMware's PowerCLI provides the most robust solution for ESXi host management. Here's a PowerShell script that safely shuts down an ESXi host:
# ESXi Nightly Shutdown Script
Connect-VIServer -Server "your-esxi-host" -User "root" -Password "your-password"
$shutdownTime = [DateTime]::Today.AddHours(22) # 10 PM shutdown
if ((Get-Date) -ge $shutdownTime) {
# Migrate or shutdown VMs first
Get-VM | Where-Object {$_.PowerState -eq "PoweredOn"} | Stop-VMGuest -Confirm:$false
# Wait for VMs to shutdown gracefully
Start-Sleep -Seconds 300
# Shutdown ESXi host
Stop-VMHost -Confirm:$false -Force
}
For those preferring direct host configuration, ESXi's limited cron implementation can be leveraged:
# SSH into ESXi host and edit crontab
vim-cmd hostsvc/easy_update_crontab "0 22 * * * /sbin/shutdown.sh && /sbin/poweroff"
Before implementing automated shutdowns:
- Ensure no critical VMs are running during shutdown periods
- Verify your host supports wake-on-LAN if morning restart is needed
- Test shutdown procedures during maintenance windows first
Add this simple check to verify successful shutdowns:
# Check last shutdown time
Get-VIEvent -MaxSamples 100 |
Where-Object {$_ -is [VMware.Vim.HostEventArgument]} |
Sort-Object -Property CreatedTime -Descending |
Select-Object -First 5
Many development teams face server overheating issues in non-climate-controlled environments. Our recent ESXi deployment in a daytime-ventilated server room presented exactly this challenge - temperatures spike dramatically when the room gets closed at night. While this particular server doesn't need 24/7 availability, proper shutdown procedures are still essential.
VMware's PowerCLI provides the most straightforward method for scheduled ESXi host shutdowns. Here's a complete PowerShell script that:
# ESXi Nightly Shutdown Script
$esxiHost = "192.168.1.100"
$creds = Get-Credential
Connect-VIServer -Server $esxiHost -Credential $creds
$shutdownTime = "23:00"
$action = {
Get-VM | Shutdown-VMGuest -Confirm:$false
Start-Sleep -Seconds 60
Stop-VMHost -VMHost $esxiHost -Confirm:$false -Force
}
$trigger = New-JobTrigger -Daily -At $shutdownTime
Register-ScheduledJob -Name "NightlyESXiShutdown" -ScriptBlock $action -Trigger $trigger
For environments where PowerCLI isn't available, we can leverage ESXi's BusyBox implementation of cron:
# SSH into ESXi host first
vim-cmd hostsvc/enable_ssh
vim-cmd hostsvc/start_ssh
# Then create a cron job
echo "0 23 * * * /sbin/shutdown.sh && /sbin/poweroff" >> /var/spool/cron/crontabs/root
/etc/init.d/cron restart
Before implementing either solution:
- Ensure all VMs can handle abrupt shutdowns (or implement proper VM shutdown sequences)
- Test during low-usage hours first
- Document the process for other team members
- Consider implementing a wake-on-LAN solution if morning startups are needed
After deployment, verify the shutdowns occur as expected:
# Check PowerCLI job history
Get-Job -Name "NightlyESXiShutdown" | Get-JobHistory
# Check ESXi cron logs
cat /var/log/cron.log | grep shutdown
For environments with vCenter and multiple hosts, consider using Distributed Resource Scheduler (DRS) rules to evacuate and shutdown hosts automatically:
New-DrsRule -Name "NightlyShutdown" -Enabled -KeepTogether $false -Type "MustRunOn"
Set-DrsCluster -Cluster (Get-Cluster) -DrsAutomationLevel FullyAutomated