vSphere 5.0 Automation with PowerCLI

Posted by
Alan Renouf
Technical Marketing

At the end of last year, December the 9th 2011 to be exact, I was lucky enough to present at the Dutch VMUG with Luc Dekens who is known very well in the PowerCLI Community and for his excellent PowerCLI blog over at Lucd.Info

Our presentation was entitled “vSphere 5.0 Automation with PowerCLI” and was based on a presentation which was given by Raymond Epping and Jan-Willem Lammers, their presentation showed the top 10 features and enhancements which were introduced in vSphere 5.0.

Luc and I expanded on this by seeing which of these features we could automate in PowerCLI, the session was well received and we came up with some great scripts and functions which Luc and I have been sharing on our blogs over the past weeks. 

You can read more about the scripts and functions by following the links in the “Further reading and scripts” section of this post.


The full presentation is available from the Dutch VMUG site, check it out there are some great slides and scripts in there: http://www.vmug.nl/downloads/VMUG2011/vs5AutomationwithPowercli.pdf

How long do your vMotion/SvMotion take ?

One of the scripts I wanted to highlight was a handy advanced function we created called Get-MotionDuration.

Get-MotionDuration will look back through the events of your vCenter and recall your vMotion and SvMotion history, it will then present you with a list of these events and how long they took, an example of this output can be seen below.


Obviously the timings will change based on the size and how utilized the virtual machine is but this is great for comparing vSphere 4 systems against vSphere 5 where multiple enhancements have been made to the speed of vMotion and SvMotion giving you the benefit of quickly being able to put hosts into maintenance mode or move VMs to new datastores.

Watch it in action

The following video shows this script in action:


Get-MotionDuration Script

Function Get-MotionDuration {
    $events = Get-VIEvent -Start (Get-Date).AddDays(-1)
    $relocates = $events |
        where {$_.GetType().Name -eq "TaskEvent" -and $_.Info.DescriptionId -eq "VirtualMachine.migrate" -or $_.Info.DescriptionId -eq "VirtualMachine.relocate"}
    foreach($task in $relocates){
        $tEvents = $events | where {$_.ChainId -eq $task.ChainId} |
            Sort-Object -Property CreatedTime
            New-Object PSObject -Property @{
                Name = $tEvents[0].Vm.Name
                Type = &{if($tEvents[0].Host.Name -eq $tEvents[-1].Host.Name){"svMotion"}else{"vMotion"}}
                StartTime = $tEvents[0].CreatedTime
                EndTime = $tEvents[-1].CreatedTime
                Duration = New-TimeSpan -Start $tEvents[0].CreatedTime -End $tEvents[-1].CreatedTime

Connect-VIServer MyViServer –User Administrator –Password “Pa$$w0rd”

Get-MotionDuration | FT -AutoSize

Further reading and scripts

The following blog posts were created with examples from the Dutch VMUG presentation, please find time to check them out as they may save you time in the future….


Leave a Reply

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