vSphere PowerCLI: Standby and Resume vSphere Hosts

I’ve always been curious about scripting some of the more tedious tasks that I perform in the vSphere client.  Plus, the VCAP-DCA requires proficiency in PowerCLI and vCLI.  So, I’ll be dusting off my programming skills from college as I prepare for the exam.  My first couple attempts at scripting are included below…

One of the more mundane tasks in my home lab involves moving my hosts into and out of Stand-by.  After consulting my handy vPowerCLI Reference courtesy of Mr. Antone Heyward (theHyperadvisor), and loading my new favorite PowerShell IDE, PowerGUI, I dove headfirst into a couple simple scripts.

Standard Disclaimer: These code samples are provided free of charge and are for educational purposes only.  No expectation of support, technical or otherwise, from either myself or my employer, VCE, should be implied by this posting.  Do not use these samples in production.  Thank you.

Ugh, now that the legalese is out of the way, here are the scripts that I created:

First, prepare PowerGUI for PowerCLI and connect to your vCenter server

Code:
. "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1"

Connect-VIServer -Server vCenter -User VMTrooper\Administrator -Password NunyaBizness

 

Resume vSphere Hosts from Stand-By:

Code:
foreach ($esxi in Get-VMHost -Location Infrastructure) {Start-VMHost $esxi -RunAsync -Confirm:$false}

Stand-By vSphere Hosts (assuming that you have shut your VMs down properly):

Code:
foreach ($esxi in Get-VMHost -Location Infrastructure) {Suspend-VMHost $esxi -RunAsync -Confirm:$false}

The Resume\Standby scripts first get a list of the hosts in my Infrastructure Cluster (Get-VMHost -Location Infrastructure).

Then, with each implementation of the for loop, $esxi temporarily holds one of the vSphere Host objects.  The required action (start\suspend) is then performed on the host with the following flags:

-RunAsync: PowerShell will not wait for each action (host boot\standby) to complete before moving to the next host in the list.  Useful since I don’t have UCS at my home :’-(  and my home machines take a looooooong time to boot up.

-Confirm:$false: PowerShell will not ask for confirmation before executing the start\suspend action.

If you have any useful scripting workflows for host power state management, share them below.

Of course, if you are serious about getting started with PowerCLI, make sure to check out Josh Atwell’s session (INF-VSP1856 – Become a Rock Star with PowerCLI and vCenter Orchestrator) at VMworld 2012 on August 27 at 4PM PST.  If you won’t be at VMworld,  check out his blog: www.vtesseract.com

Leave a Reply

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