Skip to content

VirtuallyThatGuy

Anything VMware , PowerCLI, PowerShell, Automation and some Windows

Menu
  • Home
  • PowerCLI
  • VMware
  • Automation
  • Windows
  • About
Menu

Script: How to Put ESXi Hosts InTo Maintenance Mode, Reboot ESXi Hosts, Reconnect ESXi and Loop Across All Hosts Cluster Using Powershell or Powercli

Posted on 11 December 20214 March 2024 by VirtuallyThatGuy

This is a quick blog post on rebooting​​ ESXi​​ hosts in a cluster by migrating VMs off the hosts, putting the host into maintenance mode, rebooting the hosts, checking host is up, then​​ removing the host from maintenance mode and looping through the same process again for all the hosts in the given cluster. There are many scenarios this task can be handy for example keeping up with regular reboot cycle on a scheduled task, adding or removing storage from the hosts, changing ​​ a setting for all hosts in the cluster, applying driver settings etc.…​​ 

This loops through all hosts in the cluster and below is the script block​​ 

Param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][string[]]$Cluster)
$ClusterName = "*$Cluster*"

## Start-Transcript
Start-Transcript -Path C:\Temp\hostreboot-transcript.txt

## Set Connection String
$cred = Get-Credential
$vCenters = "lab-vc01.lab.local", "lab-vc02.lab.local"
Connect-VIServer -Server $vCenters  -Credential $cred

## Set Parameters

## Param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][string[]]$Cluster)

## Param([Parameter(Mandatory=$true)][String]$Cluster)

Get-Cluster $ClusterName
if((Get-Cluster $ClusterName).count -gt 1)
{
Write-Host "Please Specify Less Than 1 Cluster"

## if the cluster name specified matches more than 1 cluster, the operation will be aborted

exit
}
else {
	write host "$ClusterName is not more than 1 Cluster"
}

## Grab host lists and save in txt file - make sure you have enough permission to write into dir

(Get-Cluster $ClusterName |Get-VMHost).Name | Out-File C:\lab\LabHostReboot.txt 

cd c:\lab\

Get-Content .\LabHostReboot.txt  | %{ 
    $Server = $_;
    Write-Host -ForegroundColor Green "$Server"
    Write-Host -ForegroundColor Yellow "`tServer Is Entering Maintenance Mode"
    Set-VMHost $Server -State maintenance -Evacuate | Out-Null
 
    Write-Host -ForegroundColor Yellow -NoNewline "`tServer is Rebooting"
    Restart-VMHost $Server -Confirm:$false  | Out-Null
 
 ## Wait 5 mins from reboot to check connection state of the Host
    do {
        sleep 2
        $ServerState = (Get-VMHost $Server).ConnectionState
        Write-Host -ForegroundColor Yellow -NoNewline "."
    } while ($ServerState -ne "NotResponding")
    Write-Host -ForegroundColor Yellow -NoNewline "(Server is powered down)"
 
 ## Another 5 mins wait post reboot to make sure server is up and in MM 
    do {
        sleep 2
        $ServerState = (Get-VMHost $Server).ConnectionState
        Write-Host -ForegroundColor Yellow -NoNewline "`."
    } while ($ServerState -ne "Maintenance")
    Write-Host -ForegroundColor Yellow "(Server is Powered Up)"
 
 ## Set connection state to connected post reboot

    Write-Host -ForegroundColor Yellow "`tServer Exiting Maintenance Mode"
    Set-VMHost $Server -State Connected  | Out-Null
    Write-Host -ForegroundColor Yellow "`tHost Reboot Cycle Done!"
    Write-Host ""
}

Stop-Transcript

2 thoughts on “Script: How to Put ESXi Hosts InTo Maintenance Mode, Reboot ESXi Hosts, Reconnect ESXi and Loop Across All Hosts Cluster Using Powershell or Powercli”

  1. Paul McConnon says:
    4 January 2023 at 12:34

    Good script. Was looking to do this very thing this morning and this is exactly what I was trying to do.

    Reply
    1. VirtuallyThatGuy says:
      4 January 2023 at 16:53

      I am glad you found it useful. Enjoy and share the love to the community

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

When autocomplete results are available use up and down arrows to review and enter to go to the desired page. Touch device users, explore by touch or with swipe gestures.

Recent Posts

  • vROps: Management Pack Troubleshooting
  • Windows AD {Active Directory} (PowerShell) samples
  • Migrate VMs Between vCentres Using Powershell or PowerCLI
  • Set VM Tools to Update Automatically on VM Reboot using powershell
  • Windows Administrator Must Have Powershell Commands

Recent Comments

  • JB on Script: How to get VM with Tag Assignment and export results to csv using PowerCLI or Powershell
  • DL on How to change VCSA root password and bypass BAD PASSWORD: it is based on a dictionary word for vCenter VCSA root account warning
  • 360coolp on How to change VCSA root password and bypass BAD PASSWORD: it is based on a dictionary word for vCenter VCSA root account warning
  • Yogesh on ESXi 8.x, 7.x, 6.x Service sfcbd-watchdog Not Running / Fails to Start – VirtuallyThatGuy
  • VirtuallyThatGuy on ESXi 8.x, 7.x, 6.x Service sfcbd-watchdog Not Running / Fails to Start – VirtuallyThatGuy

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017

Categories

  • Automation
  • PowerCLI
  • VMware
  • Windows
© 2025 VirtuallyThatGuy | Powered by Superbs Personal Blog theme