vSphere ESXi Maintenance Mode with PowerCLI

If you need to put all hosts connected to your vCenter server in maintenance mode here is how you can do it:

Get-VMHost | Set-VMHost -State Maintenance

And to exit the maintenance mode on all hosts use the following command:

Get-VMHost | Set-VMHost -State Connected

Now that is not very useful, since you won’t put all your hosts in maintenance mode very often. That’s where we could make use of glob expansion. Here is an example. Lets say we have hosts from esx1 to esx10, and of those I want to put  esx5, esx6 & esx7 in maintenance mode, this is how you can do it:

Get-VMHost -Name  [esx[5-7]* | Set-VMHost -State Maintenance

The above command will get all hosts which start with either esx5,esx6 or esx7.  Now we could have used the Select-Object, but that would be one more cmdlet to type :-).

Similarly you could exit maintenance mode on selective hosts as well:

Get-VMHost -Name  [esx[57]*.pune.fooibirds.in | Set-VMHost -State Connected

The above command would exit maintenance mode on hosts esx5.pune.foobirds.in and esx7.pune.foobirds.in. Pretty nifty ehh!

For more info on wildcards characters (globs) read it here.

Advertisements

One thought on “vSphere ESXi Maintenance Mode with PowerCLI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s