Home > Blogs > VMware PowerCLI Blog > Monthly Archives: August 2012

Monthly Archives: August 2012

PowerShell and PowerCLI at VMworld 2012 – San Francisco

Alan Renouf
Posted by
Alan Renouf
Technical Marketing

Its VMworld time again… yes already !

At VMworld San Francisco there are some great sessions which include PowerShell and PowerCLI this year, I hope you have already added them to your session builder but if not then please see below.

You will find a mixture of Sessions, Group Discussions and a Hands On Lab.

With integration and automation being on the top of everyone's list at the moment I can highly recommend attending these sessions to get a heads up on what's going on in the world of PowerCLI…

And if you are still undecided then skip to the bottom of this post where you will be able to watch a couple of quick session videos explaining more about some of these sessions.

INF-VSP1252 – What’s New with vSphere Automation

Click here to add

In this session Technical Marketing automation experts William Lam and Alan Renouf will take you through VMware Automation.

You will learn which products are available, which products to use and how Automation fits into the VMware Suite of products. In this session Alan and William will take you through the exciting features available to use when automating VMware products, both beginners and experts will learn how to use new features to make your life easier and more productive.

Monday, Aug 27, 2:30 PM – 3:30 PM

Wednesday, Aug 29, 9:30 AM – 10:30 AM

William Lam – Sr. Technical Marketing Engineer, VMware, Inc.

Alan Renouf – Sr. Technical Marketing Architect, VMware, Inc.


INF-BCO2155 – vCloud DR for Oxford University Computing Services – Real World Example


Resiliency is a key aspect of any infrastructure—it is even more important in infrastructure-as-a-service solutions. This session will include a tour of a real world vCloud DR solution deployed at Oxford University Computing Services (OUCS). Throughout the session there will be detailed guidance on how both the management and resource clusters were designed, deployed and automated. Furthermore the details of how SRM in conjunction with VMware vSphereTM PowerCLI (PowerCLI) was used to automate the end-to-end recovery of a vCloud Director–based infrastructure. This session will offer a perfect complimentary follow on from the whitepaper regarding the high-level process produced by Duncan Epping and Chris Colotti and the Automation session by Alan Renouf and Aidan Dalgleish. The paper combined with the Automation session will describe the process and the automation principles whereas this session will describe a complete real world implementation.

Monday, Aug 27, 3:30 PM – 4:30 PM

Aidan Dalgleish – Consulting Architect, VMware, Inc.

Adrian Parks – Senior Systems Administrator, Oxford University Computing Services

Gary Blake – Senior Consultant, VMware, Inc.


INF-VSP1329 – PowerCLI Best Practices: The Return!


Building on the success of our PowerCLI Best Practices sessions from VMworld 2011, we want to show you our next collection of PowerCLI best practices. This session will show and demonstrate PowerCLI in all its aspects managing and automating not just VMware vSphere® but also VMware vCloud Director®, VMware® View™, VMware vShield™ and more

Tuesday, Aug 28, 3:00 PM – 4:00 PM

Wednesday, Aug 29, 8:00 AM – 9:00 AM

Alan Renouf – Sr. Technical Marketing Architect, VMware, Inc.

Luc Dekens – Systems Engineer, Eurocontrol Maastricht.


INF-VSP2448 – Automating Bare Metal to the Cloud and Beyond


When working in the cloud automation is key, working at the scale of the cloud VMware customers need an easy and reliable method of deployment which gives guaranteed results 100% of the time. Cisco, VMware and Bluelock will show you how to automate the build of your complete vCloud Director Infrastructure from the bare metal up to the cloud and beyond. Learn how PowerShell can be used to create a single easily readable and adaptable script to ensure your system is built quickly and efficiently, watch as a system is built before your very eyes with no magic tricks or rabbits in sight.

Tuesday, Aug 28, 12:00 PM – 1:00 PM

Eric Williams – Technical Marketing Engineer, Cisco Systems Inc.

Alan Renouf – Sr. Technical Marketing Architect, VMware, Inc.

Jake Robinson – Solutions Architect, BlueLock


INF-VSP1856 – Become a Rock Star with PowerCLI and vCenter Orchestrator


Automation is the future of cloud, and in this session attendees will learn how to identify areas in their environment that are primed for less administrator interaction. The combination of PowerCLI and VMware® vCenter™ Orchestrator puts considerable power in even the greenest users. Attendees will learn how to identify areas for automation and start learning to discern which tool is best suited for the job and when to use them together.

Monday, Aug 27, 4:00 PM – 5:00 PM

Josh Atwell – Systems Administrator, Cisco Systems Inc.


INF-VSP2164 – Automation of vCloud Director Disaster Recovery


Resiliency is a key aspect of any infrastructure—it is even more important in infrastructure-as-a-service solutions. This session will include guidance, examples and demonstrations of the use of VMware vSphereTM PowerCLI (PowerCLI) to automate the recovery of a vCloud Director–based infrastructure. In particular the session will focus on the automation of the recovery steps for vCloud Director managed vApp workloads, that cannot be recovered with vSphere Site Recovery Manager (SRM). This session will offer a perfect complimentary follow on from the whitepaper regarding the high-level process released by Duncan Epping and Chris Colotti. The paper discussed the process and this session will describe the automation

Tuesday, Aug 28, 4:30 PM – 5:30 PM

Alan Renouf – Sr. Technical Marketing Architect, VMware, Inc.

Aidan Dalgleish – Consulting Architect, VMware, Inc.



Group Discussions

GD27 – PowerCLI with Alan Renouf


During this group discussion we will talk about what can be automated, discuss the merits of using PowerShell and PowerCLI, show examples and talk about the best methods of achieving integration. We will also discuss tips and tricks you have learned and the best methods for automating the VMware products. Come prepared with examples and questions and remember this is a Group Discussion!

Monday, Aug 27, 10:30 AM – 11:30 AM

Thursday, Aug 30, 12:00 PM – 1:00 PM

Alan Renouf – Sr. Technical Marketing Architect, VMware, Inc.


Hands on Labs

HOL-INF-10 – Script and Develop Your Cloud Solution with PowerCLI and the vSphere Web Client SDK

This lab is presented as two 30 minute lightning labs to help you extend your VMware solution. The PowerCLI module covers automating vSphere and vCloud Director. Novice users will learn to use the tool and more advanced users will get familiar with the new functionality available in the latest release of the product. You will walk away with a better understanding of PowerCLI and how it can help you in your day-to-day work. The vSphere Web Client SDK module teaches you several techniques for extending the vSphere Web Client. The goal of this module is to demonstrate the close integration that is possible with the vSphere Web Client SDK, and the ease with which you can integrate your own solutions into the vSphere Web Client.


Attend the self paced Hands on Lab.



Still cant decide ?  Take a look at some of the session videos where the presenters explain more information…..

Waiting for OS customization to complete

Posted by
Vitali Baruh
PowerCLI QE team

Provisioning of new virtual machines often includes Operating System (OS) customization.

A typical provisioning workflow could look like this:

1.  Clone a virtual machine from a template and specify a customization specification

2.  Start the VM. This triggers the customization process

3.  Wait for the customization to complete

4.  Perform additional setup steps


Step three is quite tricky, because there’s no easy way to find out when the customization has completed within the guest operating system and if it has succeeded or has failed.

This step could be implemented in an optimistic manner with sleep for 10 minutes, but it could also be implemented in a more robust and informative way. To do this, we could create a script with the following specification:


  • List of virtual machines which should be monitored for the completion of a customization process
  • Timeout in seconds that the script should wait for the customization to end


  • List of PSObjects where each object will hold a passed VM and its customization status – successful, failed, started, et.

The function is based on several virtual machine events defined by vCenter Server:

  • VmStartingEvent – posted when VM is powered on
  • CustomizationStartedEvent – posted when customization is started
  • CustomizationSucceeded – posted when customization succeeds
  • CustomizationFailed – posted when customization fails

Our script workflow is pretty simple:

  • 1. For each VM we will look for the last VmStarting event, because the customization process starts after the VM has been powered on
    • 1.1. If such an event is found, change the status to “CustomizationNotStarted”
    • 1.2. If such an event is not found, change the status to “VmNotStarted”
  • 2. Start a loop
    • 2.1. For each VM with status “CustomizationNotStarted”
      • 2.1.1. Check for posted CustomizationStartedEvent after the last power-on operation
        • If such an event is found, change the status to “CustomizationStarted”
    • 2.2. For each VM with status “CustomizationStarted”
      • 2.2.1. Check for Succeded or Failed Event
        • If such an event is found, change the status to the corresponding “CustomizationSucceeded” or “CustomizationFailed” values
    • 2.3. Check is there more VMs with status “CustomizationNotStarted” or “CustomizationStarted” (we should continue to monitor these VMs)
      • 2.3.1. If no, break the loop
    • 2.4. Check whether the specified timeout has elapsed
      • 2.4.1. If yes, break the loop
  • 3. Return result

Now let see the script in action!

Step 1 – retrieve template, OS customization spec, target vmhost and target datastore.


As result we have several variables that are needed to start cloning with customization tasks.

Step 2 – start clone operations for two VMs


As result we have $vms variable holding our virtual machines.

Step 3 – start virtual machines, which triggers customization process and monitor it with our script.

The script is started with two virtual machines and also we specify to wait maximum 600 seconds for OS customization to complete.


After the start the script reports each VM which will be monitored.

The next screen shows that after about 45 second customization start events have been posted for both VMs.


The last screen shot shows the moments when the two customization succeed events have been posted and also the result of the script.


Finally the $result variable contains two custom objects. Each object contains VM and CustomizationStatus properties.

Our script could iterate over the $result variable and to perform some additional setup actions – installing software not included in the template, applying specific software configurations, etc.

Of course for these additional setup actions we can use the Invoke-VMScript and Copy-VMGuestFile cmdlets.

Now let’s see the code of WaitVmCustomization.ps1 script:


Waits customization process for list virtual machines to completes.

Waits customization process for list virtual machines to completes.
The script returns if customization process ends for all virtual machines or if the specified timeout elapses.
The script returns PSObject for each specified VM.
The output object has VM and CustomizationStatus properties.

$vm = 1..10 | foreach { New-VM -Template WindowsXPTemplate -OSCustomizationSpec WindowsXPCustomizaionSpec -Name "winxp-$_" }
.\WaitVmCustomization.ps1 -vmList $vm -timeoutSeconds 600

The script is based on sveral vCenter events.
* VmStarting event – this event is posted on power on operation
* CustomizationStartedEvent event – this event is posted for VM when customiztion has started
* CustomizationSucceeded event – this event is posted for VM when customization has successfully completed
* CustomizationFailed – this event is posted for VM when customization has failed

Possible CustomizationStatus values are:
* "VmNotStarted" – if it was not found VmStarting event for specific VM.
* "CustomizationNotStarted" – if it was not found CustomizationStarterdEvent for specific VM.
* "CustomizationStarted" – CustomizationStartedEvent was found, but Succeeded or Failed event were not found
* "CustomizationSucceeded" – CustomizationSucceeded event was found for this VM
* "CustomizationFailed" – CustomizationFailed event wass found for this VM

   # VMs to monitor for OS customization completion
   [VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine[]] $vmList,
   # timeout in seconds to wait
   [int] $timeoutSeconds = 600

# constants for status
      $STATUS_VM_NOT_STARTED = "VmNotStarted"
      $STATUS_CUSTOMIZATION_NOT_STARTED = "CustomizationNotStarted"
      $STATUS_STARTED = "CustomizationStarted"
      $STATUS_SUCCEEDED = "CustomizationSucceeded"
      $STATUS_FAILED = "CustomizationFailed"
# constants for event types     
      $EVENT_TYPE_CUSTOMIZATION_STARTED = "VMware.Vim.CustomizationStartedEvent"
      $EVENT_TYPE_CUSTOMIZATION_SUCCEEDED = "VMware.Vim.CustomizationSucceeded"
      $EVENT_TYPE_CUSTOMIZATION_FAILED = "VMware.Vim.CustomizationFailed"
      $EVENT_TYPE_VM_START = "VMware.Vim.VmStartingEvent"

# seconds to sleep before next loop iteration
function main($vmList, $timeoutSeconds) {
   # the moment in which the script has started
   # the maximum time to wait is measured from this moment
   $startTime = Get-Date
   # we will check for "start vm" events 5 minutes before current moment
   $startTimeEventFilter = $startTime.AddMinutes(-5)
   # initializing list of helper objects
   # each object holds VM, customization status and the last VmStarting event
   $vmDescriptors = New-Object System.Collections.ArrayList
   foreach($vm in $vmList) {
      Write-Host "Start monitoring customization process for vm '$vm'"
      $obj = "" | select VM,CustomizationStatus,StartVMEvent
      $obj.VM = $vm
      # getting all events for the $vm,
      #  filter them by type,
      #  sort them by CreatedTime,
      #  get the last one
      $obj.StartVMEvent = Get-VIEvent -Entity $vm -Start $startTimeEventFilter |
         where { $_ -is $EVENT_TYPE_VM_START } |
         Sort CreatedTime |
         Select -Last 1
      if (-not $obj.StartVMEvent) {
         $obj.CustomizationStatus = $STATUS_VM_NOT_STARTED
      } else {
         $obj.CustomizationStatus = $STATUS_CUSTOMIZATION_NOT_STARTED
   # declaring script block which will evaulate whether
   # to continue waiting for customization status update
   $shouldContinue = {
      # is there more virtual machines to wait for customization status update
      # we should wait for VMs with status $STATUS_STARTED or $STATUS_CUSTOMIZATION_NOT_STARTED
      $notCompletedVms = $vmDescriptors |

         where { $STATUS_NOT_COMPLETED_LIST -contains $_.CustomizationStatus }

      # evaulating the time that has elapsed since the script is running
      $currentTime = Get-Date
      $timeElapsed = $currentTime – $startTime
      $timoutNotElapsed = ($timeElapsed.TotalSeconds -lt $timeoutSeconds)
      # returns $true if there are more virtual machines to monitor
      # and the timeout is not elapsed
      return ( ($notCompletedVms -ne $null) -and ($timoutNotElapsed) )
   while (& $shouldContinue) {
      foreach ($vmItem in $vmDescriptors) {
         $vmName = $vmItem.VM.Name
         switch ($vmItem.CustomizationStatus) {
               # we should check for customization started event
               $vmEvents = Get-VIEvent -Entity $vmItem.VM -Start $vmItem.StartVMEvent.CreatedTime
               $startEvent = $vmEvents | where { $_ -is $EVENT_TYPE_CUSTOMIZATION_STARTED }
               if ($startEvent) {
                  $vmItem.CustomizationStatus = $STATUS_STARTED
                  Write-Host "Customization for VM '$vmName' has started"
            $STATUS_STARTED {
               # we should check for customization succeeded or failed event
               $vmEvents = Get-VIEvent -Entity $vmItem.VM -Start $vmItem.StartVMEvent.CreatedTime
               $succeedEvent = $vmEvents | where { $_ -is $EVENT_TYPE_CUSTOMIZATION_SUCCEEDED }
               $failedEvent = $vmEvents | where { $_ -is $EVENT_TYPE_CUSTOMIZATION_FAILED }
               if ($succeedEvent) {
                  $vmItem.CustomizationStatus = $STATUS_SUCCEEDED
                  Write-Host "Customization for VM '$vmName' has successfully completed"
               if ($failedEvent) {
                  $vmItem.CustomizationStatus = $STATUS_FAILED
                  Write-Host "Customization for VM '$vmName' has failed"
            default {
               # in all other cases there is nothing to do
               #    $STATUS_VM_NOT_STARTED -> if VM is not started, there's no point to look for customization events
               #    $STATUS_SUCCEEDED -> customization is already succeeded
               #    $STATUS_FAILED -> customization
         } # enf of switch
      } # end of the freach loop
      Write-Host "Sleeping for $WAIT_INTERVAL_SECONDS seconds"
   } # end of while loop
   # preparing result, without the helper column StartVMEvent
   $result = $vmDescriptors | select VM,CustomizationStatus
   return $result

#calling the main function
main $vmList $timeoutSeconds

And here is the code for the scenario script:

# prepare necesary objects for clone operations
$template = Get-Template WindowsXPTemplate
$spec = Get-OSCustomizationSpec WindowsXPSpec
$vmhost = Get-VMHost | select -First 1
$datastore = Get-Datastore -VMHost $vmhost -Name Storage1

# Start cloning operations
$vms = 1..2 | % { New-VM -Name "WinXP-$_" -ResourcePool $vmhost -Template $template -Datastore $datastore -OSCustomizationSpec $spec }

# Trigger customization and wait its completion
$vms = $vms | Start-VM
$result = .\WaitVmCustomization.ps1 -vmList $vms -timeoutSeconds 600

Several notes about the script

  • It doesn’t accept pipeline input, because it checks the customization status for multiple virtual machines simultaneously. This won’t be possible if the process block of the script is executed for each object passed by the pipeline.
  • It also shows how to work with specific type of events.
  • Its VIEvent searches are optimized by specifying concrete entity and start time filters

Hope that this blog post is interesting and useful.

More about Vitali Baruh…

Vitali joined VMware and PowerCLI team in the beginning of 2009. He is member of the quality engineering part of the team and his main role is the functional verification of the vSphere, vCloud and License PowerCLI components.

As all members of the team he is working to deliver a good and valuable product. He is also working to improve all processes and tools involved in product development and validation.