VMware Cloud Community
wreedMH
Hot Shot
Hot Shot

Rolling vSAN Cluster Reboot Script

I found this script on the internet. I modified it a bit to use it as my rolling vSAN cluster reboot script. Problem is it reboots each host twice before moving onto the next. Here is the code, any ideas why it does this?

# 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 -vsandatamigrationmode EnsureAccessibility | 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

0 Kudos
4 Replies
LucD
Leadership
Leadership

Are you sure there are no vCenter connections open when you run the script?
You could check $global:defaultVIServers at the beginning of the script.

And how do the reboots look?

ESX1

ESX1

ESX2

ESX2

or

ESX1

ESX2

ESX1

ESX2


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
wreedMH
Hot Shot
Hot Shot

I will have to check later, script is running and its working just rebooting them twice. It looks like:

ESXI 1

ESXI 1

ESXI 2

ESXI 2

ESXI 3

ESXI 3

0 Kudos
wreedMH
Hot Shot
Hot Shot

Must have been connected twice, I restarted Powershell and it works as expected now.

0 Kudos
LucD
Leadership
Leadership

I expected something like that :smileygrin:


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos