• Skip to primary navigation
  • Skip to main content

jdgreen.io

The personal blog of James Green

  • Home
  • Technology
    • Announcements
    • Industry
    • Reviews/Guides
    • Tech Field Day
    • Interviews
  • Health
    • Food
    • Biohacking
    • Fitness
  • Faith
  • Book Notes
    • Business Books
    • Faith Books
    • Health Books
    • Technology Books
  • About
  • Show Search
Hide Search

PowerCLI – Rolling Reboot vSphere Cluster

James Green · Mar 17, 2014 ·

From time to time, I need to reboot all hosts in a cluster. If it’s not for an Update Manager operation, I need a way to run through the whole cluster (not manually). I need to place one host in maintenance mode, reboot it, exit maintenance mode, and move to the next one. PowerCLI to the rescue, as usual! Feel free to modify or share! And of course, use at your own risk 🙂

PowerCLI script

[code language=”powershell”] # Check to make sure both arguments exist
if ($args.count -ne 2) {
Write-Host "Usage: reboot-vmcluster.ps1 <vCenter> <HostList.txt>"
exit
}

# Set vCenter and Cluster name from Arg
$vCenterServer = $args[0] $VIHosts = $args[1]

# Connect to vCenter
Connect-VIServer -Server $vCenterServer | Out-Null

# Get VMware Server Object based on name passed as arg
$ESXiServers = Get-Content $VIHosts | %{Get-VMHost $_}

# Reboot ESXi Server Function
Function RebootESXiServer ($CurrentServer) {
# Get VI-Server name
$ServerName = $CurrentServer.Name

# Put server in maintenance mode
Write-Host "** Rebooting $ServerName **"
Write-Host "Entering Maintenance Mode"
Set-VMhost $CurrentServer -State maintenance -Evacuate | Out-Null

# Reboot host
Write-Host "Rebooting"
Restart-VMHost $CurrentServer -confirm:$false | Out-Null

# Wait for Server to show as down
do {
sleep 15
$ServerState = (get-vmhost $ServerName).ConnectionState
}
while ($ServerState -ne "NotResponding")
Write-Host "$ServerName is Down"

# Wait for server to reboot
do {
sleep 60
$ServerState = (get-vmhost $ServerName).ConnectionState
Write-Host "Waiting for Reboot …"
}
while ($ServerState -ne "Maintenance")
Write-Host "$ServerName is back up"

# Exit maintenance mode
Write-Host "Exiting Maintenance mode"
Set-VMhost $CurrentServer -State Connected | Out-Null
Write-Host "** Reboot Complete **"
Write-Host ""
}

## MAIN
foreach ($ESXiServer in $ESXiServers) {
RebootESXiServer ($ESXiServer)
}

# Disconnect from vCenter
Disconnect-VIServer -Server $vCenterServer -Confirm:$False
[/code]

VMware automation, cluster, ESXi, powercli, reboot, vSphere

James Green is an enterprise IT consultant, a product of an amazing IT community, and a partner in ActualTech Media. He is a serial vExpert designee and a passionate Tech Field Day delegate and supporter. » Read Full Bio...

James Green: View My Blog Posts

Copyright © 2023 · Monochrome Pro on Genesis Framework · WordPress · Log in

Posting....