vMotion and SvMotion Details with PowerCLI

Today I was asked if there was a way to list the vMotions and SvMotions which had occurred in an infrastructure, not only this but they needed to know which hosts the VMs had moved to, the reason for this was licensing.

Firstly they needed to confirm that certain VMs were firstly setup with DRSAffinityrules as disabled, this one was straight forward in PowerCLI:

Get-VM | Select Name, DRSAutomationLevel

image

Secondly they wanted a list of the vMotions and SvMotions which had taken place over the last week and specifically the source and destination hosts, a post on the VMware PowerCLI Blog gives us most of this information and was easy to adjust to include the source and destination hosts…

image

The adjusted script can be found below:

Function Get-MotionDuration {
    $events = Get-VIEvent -Start (Get-Date).AddDays(-7)
    $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
        if($tEvents.Count){
            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
				SourceHost = $tEvents[0].Host.Name
				DestinationHost = $tEvents[-1].Host.Name
            }
        }
    }
}


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

Get-MotionDuration | FT -AutoSize

4 thoughts on “vMotion and SvMotion Details with PowerCLI”

  1. I got blank output in any parameter, so i modified script from practical admin and working with this output.

    Name Created UserName Type SourceHost TargetHost Datacenter

  2. It’s looking great, but when i execute it i don’t get any output no matter which parameters i use (i also don’t get any errors).

    PowerCLI version: 5.0.1.4431 and 5.5.0.5836 (tried to execute the script on each one of them)

    vCenter version: 4.1 and 5.1 (tried to execute the script on each one of them)

    Please help me, thanks!

Leave a Reply