Place a vSphere Host into Maintenance Mode via PowerCLI
- Ben Liebowitz
- 0
- 11366
So, we had two different vSphere hosts, in two different environments, have hardware faults this week. One had a bad memory module, and the other a bad FAN. As I launched the VI client for each environment and placed the problem host into maintenance mode, I started to think… “I bet I can automate this!”
Depending on how complex your environment, you may or may not find this useful. For me, I have 6 different production vCenter servers, one QA vCenter and a LAB vCenter. Keeping all 8 Thick Clients open on my laptop can really kill the resources and I constantly get logged out of the web clients if I’m not clicking around. BUT I always leave my PowerCLI window open and can connect to the vCenter I need, quicker than launching the VI Client, typing in the name of the vCenter I want to connect to, selecting the Use Windows Session Credentials Check-box and clicking LOGIN.
Since I run my PowerCLI window with the same credentials as I use to login to each vCenter, I don’t have to pass credentials or have it prompt me. I just use the command below:
connect-viserver vCENTER
Now, if you already have the thick/web client open, it’s probably faster to just right click on the host and enter maintenance mode. However, if you’re not yet connected to vCenter, using a script may be easier. Also, if the part repair isn’t going to be done until after hours, you can schedule the script to run later. Especially, if you’re not going to be online and are going to allow an on-site/NOC technician to handle it. You’ll probably want to add a command to shutdown the host also.
# Place the selected host into Maintenance Mode. Get-VMHost -Name PROBLEM_HOST | set-vmhost -State Maintenance # Shutdown the host Stop-VMhost -VMhost PROBLEM_HOST -Confirm:$false
After I ran that command, I started to think about how I can expand on that and decided to have the script list out the hosts and let me choose the host from the list. I also added a note how to remove that host from Maintenance Mode when I was ready, so I could just copy/paste that back into my PowerCLI window… I left the shutdown command out of the script below as I don’t always want to shutdown the host right away, but if you want to put it in, feel free!
########################################### # # PowerCLI Script to place host into Maintenance Mode # Created by BLiebowitz on 11/2/2015 # ########################################### # Choose which host to place into maintenance mode (you MUST connect to vCenter first) Write-host "Choose which vSphere host to place into Maintenance Mode." write-host "" $IHOST = Get-VMhost | Select Name | Sort-object Name $i = 1 $IHOST | %{Write-Host $i":" $_.Name; $i++} $DSHost = Read-host "Enter the number for the host to place into Maintenance Mode." $SHOST = $IHOST[$DSHost -1].Name write-host "You have selected" $SHOST"." # Place Selected host into Maintenance Mode Get-VMHost -Name $SHOST | set-vmhost -State Maintenance # Display Note how to exit the host from Maintenance Mode. write-host "" write-host "To exit Maintenance Mode, Copy/Paste and run the line below:" write-host "get-vmhost -name $SHOST | set-vmhost -state Connected"
Hope you can find this script as useful!
Thanks!
Ben Liebowitz, VCP, vExpert
NJ VMUG Leader