Best Practices


Working with Maintenance Mode in PowerShell

by Steve  | June 30th, 2009

Today’s topic is working with PowerShell in vWire to enter and exit Maintenance Mode.  If you do not have vWire installed yet you can download it here.  Once you have vWire installed you will notice that there is a script action to exit Maintenance Mode.  When writing scripts for entering and exiting Maintenance Mode, exiting is by far the easiest to do.   Let’s take a look at the code to Exit Maintenance Mode.

          foreach ($h in $input)
          {
          Set-VMHost -VMHost $h -State “Connected” -Confirm:$false | % { “Exiting maintenance mode on ‘” + $h + “‘”}
          }

The first step is to loop through the piped $input to get the name of each of the VMware ESX hosts that you want to exit Maintenance Mode.  Once we have the name then we set the state of the host to Connected which will cause the host to exit Maintenance Mode.
Let’s take a quick look at all the options the -State switch will offer us:
If the State parameter is specified as Connected, then:
- if the host is currently connected and not in maintenance mode, does nothing.
- if the host is in maintenance mode, exits the maintenance mode.
- if the host is not connected or not responding, tries to reconnect.

If the State parameter is specified as Disconnected, then:
- if the host is currently connected, attempts to disconnect.
- if the host is not connected or not responding, does nothing.

If State is specified as Maintenance, then:
- if the host is currently connected and not in maintenance mode, enters maintenance mode.
- if the host is currently connected and in maintenance mode, does nothing.
- if the host is not connected or not responding, attempts to reconnect and enter maintenance mode.

If you notice, vWire does not ship with an action to Enter Maintenance Mode.  I built a quick down and dirty script or action to perform this task. You can download this action here from the community content section to be able to import into your vWire installation. Entering Maintenance mode actually requires a few more steps to work properly.  Let’s look at the code and see what is involved.

          $listESX = Get-VMHost
          ForEach ($vSRV in $input){
          ForEach ($ESX in $listESX){If ($ESX -cnotmatch $vSRV){
          Get-VMHost -Name $vSRV| Get-VM | Move-VM -Destination (Get-vmhost $ESX)
          }
          }
          Get-VMHost -Name $vSRV | Set-VMHost -State maintenance | % { “Entering maintenance mode on ‘” + $vSRV + “‘”}
          }

Notice that the first step is to get a list of all the VMware ESX hosts in the cluster. From there we will loop through the piped $input looking for each VMware ESX Server. Now, before we can enter Maintenance Mode, we will need to vMotion all the running virtual machines from the host we are performing the action on.  In the code $vSRV is the name of the VMware ESX host that we are working to put into Maintenance Mode and we want to make sure we use all the other VMware ESX servers in the cluster to migrate the virtual machines to.  The -cnotmatch switch will do just that to make sure the ESX hosts we got from the Get-VMHost command is not the name of the host we are working on

Now the script can migrate all the running virtual machines to all the other hosts in the cluster and then will enter Maintenance Mode successfully.

Until next time Happy Scripting!


Tags , , , ,

This entry was posted on Tuesday, June 30th, 2009 at 11:48 am and is filed under Virtual Tech. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Comments

[...] special thanks goes to Steve Beaver. His post “Working with Maintenance Mode in PowerShell” helped me figure out the automatic VMotion to the remaining [...]


Add a Comment

x

Subscribe to The Virtual Black Hole RSS Feed Email Notification

Enter your email address:

Delivered by FeedBurner