VMware Esxi rolling reboot after move the host maintenance mode


VMware Esxi rolling reboot after move the host maintenance mode

if ($args.count -ne 2) {
Write-Host "Usage: rolling_reboot.ps1 <vcenter-nte1.mint> <Host.txt>"
exit
}
# Set vCenter and Cluster name from Arg
$vCenterServer = $args[0]
$VIHosts = $args[1]

# Connect to vCenter
Connect-VIServer -Server $vCenterServer  -WarningAction Ignore

# 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

#remove the host
Write-Host "removing the host"
Remove-VMHost -VMHost $CurrentServer -confirm:$false | Out-Null
Write-Host "wait for 8 minutes"
# Wait for Server to show as down
#do {
#sleep 15
#$ServerState = (get-vmhost $ServerName).ConnectionState
#}
#while ($ServerState -ne "NotResponding")
#Write-Host "$ServerName is Down"
sleep 480
# Wait for server to reboot and add
do {
sleep 30
$ServerState = (get-vmhost $ServerName -ErrorAction Ignore ).ConnectionState
Write-Host "Waiting for Reboot …"
}
while ($ServerState -ne "Connected")
Write-Host "$ServerName is up"
Set-VMHost -VMhost $ServerName -VMSwapfileDatastore "*esxiswap" | Out-Null


# Exit maintenance mode
# Write-Host "Exiting Maintenance mode"
# Set-VMhost $CurrentServer -State Connected | Out-Null
Write-Host "** Reboot Complete **"
Write-Host " Host is booted with latest image profile"
}

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

# Disconnect from vCenter
Disconnect-VIServer -Server $vCenterServer -Confirm:$False

Comments

Popular posts from this blog

Using Non-Maskable Interrupt (NMI) facilities to troubleshoot unresponsive VMware Virtual Machine.

Removing invalid linked clone entries automatically using the ViewDBChk tool

VMTool update steps on Horizon VDI Parent VM