On my quest to learn more about PowerCLI, I have been playing around with some more cmdlets in my lab. As a simple task, I have figured out how to tell ESX or ESXi hosts to enter and exit maintenance mode. Here’s how we do this. First of all ensure you are connected to your vCenter server instance using Connect-VIServer ServerName.
Enter maintenance mode:
Set-VMHost ESXi-01.noobs.local -State "Maintenance"
Exit maintenance mode:
Set-VMHost ESXi-01.noobs.local -State "Connected"
Set host to “disconnected” state:
Set-VMHost ESXi-01.noobs.local -State "Disconnected"
So now that we know the basics of setting the state of a VMware host, how about we get slightly more technical and perform one of the above operations on a bunch of hosts in one go? Powershell / PowerCLI is all about automation after all! Note that in the following script, I have also include a simple “if / else” statement to prompt the user running the script manually as we are about to send all ESX(i) hosts into maintenance mode! Use this at your own risk of course, it is just for demonstration purposes. You may want to modify to select hosts to enter maintenance mode on certain criteria. For example, all hosts in a particular cluster, or all hosts with a certain property. Here is the script I would use to perform the operation on all the ESX(i) hosts found in vCenter:
$VCServer = “yourvcservername” Connect-VIServer $VCServer $confirm = Read-Host “Are you sure you want all hosts to enter maintenance mode? (type yes to continue) “ if ($confirm -eq "yes") { Get-VMHost | Set-VMHost -State "Maintenance" } else { "Script aborted (you didn't confirm by typing yes)" }
Once the script is executed, you should get a progress indicator whilst hosts are being dealt with. Afterwards you’ll get some output from each host listing its relevant Connection Status and statistics. Like so:
In the above example, we set some variables, and use some basic logic checking with an IF ELSE statement and an equal to (-eq) operator. We also see how to perform a few operations on ESX or ESXi hosts. I hope this helps anyone starting out with PowerCLI. Please do leave any comments, suggestions or improvements in the comments section!