VMware Cloud Community
anthonyhwynn
Contributor
Contributor

powershell report cluster vmotion report

try looking anyone can share powershell to pull report cluster vmotion report and then export out to exle file.  thanks

Labels (1)
Reply
0 Kudos
12 Replies
scott28tt
VMware Employee
VMware Employee

Thread reported so moderators know it should be moved to the PowerCLI area.

 


-------------------------------------------------------------------------------------------------------------------------------------------------------------

Although I am a VMware employee I contribute to VMware Communities voluntarily (ie. not in any official capacity)
VMware Training & Certification blog
Reply
0 Kudos
LucD
Leadership
Leadership

Have a look at Get the vMotion/svMotion history


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
anthonyhwynn
Contributor
Contributor

try the script but when it export it not show anything relate to DRS, is this script working with vmware ver 6.7.  thanks

Reply
0 Kudos
LucD
Leadership
Leadership

I changed line 163 to

$eventTypes = "DrsVmMigratedEvent", "VmMigratedEvent", "VmRelocatedEvent"

and then called like this

Get-Cluster -Name cluster | Get-MotionHistory -Days 1 -Recurse

and that returned all vMotions and svMotions from the last day.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
anthonyhwynn
Contributor
Contributor

this is the scrip i try, it only up to 101 line.  please assist  thanks

 

 

$start = (Get-Date).AddHours(-$hours)
$tasknumber = 999 # Windowsize for task collector
$eventnumber = 100 # Windowsize for event collector
$tgtTaskDescriptions = "VirtualMachine.migrate","Drm.ExecuteVMotionLRO"
$migrations = @()
$report = @()

# Get the guest for which we want the report
$vmHash = @{}
Get-Cluster -Name "P1" | Get-VM | %{
$vmHash[$_.Name] = $_.Host
}

# Retrieve the vMotion tasks and the corresponding events
$taskMgr = Get-View TaskManager
$eventMgr = Get-View eventManager

$tFilter = New-Object VMware.Vim.TaskFilterSpec
$tFilter.Time = New-Object VMware.Vim.TaskFilterSpecByTime
$tFilter.Time.beginTime = $start
$tFilter.Time.timeType = "startedTime"

$tCollector = Get-View ($taskMgr.CreateCollectorForTasks($tFilter))

$dummy = $tCollector.RewindCollector
$tasks = $tCollector.ReadNextTasks($tasknumber)

while($tasks){
$tasks | where {$tgtTaskDescriptions -contains $_.DescriptionId} | % {
$task = $_
$eFilter = New-Object VMware.Vim.EventFilterSpec
$eFilter.eventChainId = $task.EventChainId

$eCollector = Get-View ($eventMgr.CreateCollectorForEvents($eFilter))
$events = $eCollector.ReadNextEvents($eventnumber)
while($events){
$events | % {
$event = $_
switch($event.GetType().Name){
"VmBeingHotMigratedEvent" {
$migrations += New-Object PSObject -Property @{
VMname = $task.EntityName
Source = $event.Host.Name
Destination = $event.DestHost.Name
Start = $task.StartTime
Finish = $task.CompleteTime
Result = $task.State
User = $task.Reason.UserName
DRS = &{if($task.DescriptionId -like "Drm.*"){$true}else{$false}}
}
}
Default {}
}
}
$events = $eCollector.ReadNextEvents($eventnumber)
}
$ecollection = $eCollector.ReadNextEvents($eventnumber)
# By default 32 event collectors are allowed. Destroy this event collector.
$eCollector.DestroyCollector()
}
$tasks = $tCollector.ReadNextTasks($tasknumber)
}

# By default 32 task collectors are allowed. Destroy this task collector.
$tCollector.DestroyCollector()

# Handle the guests that have been vMotioned
$grouped = $migrations | Group-Object -Property VMname
$grouped | Sort-Object -Property Count -Descending | where{$vmHash.ContainsKey($_.Name)} | %{
$i = 1
$row = New-Object PSObject
Add-Member -InputObject $row -Name VM -Value $_.Name -MemberType NoteProperty
$_.Group | Sort-Object -Property Finish | %{
# The original location of the guest
if($i -eq 1){
Add-Member -InputObject $row -Name ("Time" + $i) -Value $start -MemberType NoteProperty
Add-Member -InputObject $row -Name ("Host" + $i) -Value $_.Source -MemberType NoteProperty
$i++
}
# All the vMotion destinations
Add-Member -InputObject $row -Name ("Time" + $i) -Value $_.Finish -MemberType NoteProperty
Add-Member -InputObject $row -Name ("Host" + $i) -Value $_.Destination -MemberType NoteProperty
Add-Member -InputObject $row -Name ("DRS" + $i) -Value $_.DRS -MemberType NoteProperty
Add-Member -InputObject $row -Name ("User" + $i) -Value $_.User -MemberType NoteProperty
$i++
}
$report += $row
$vmHash.Remove($_.Name)
}

# Add remaining guests to report
$vmHash.GetEnumerator() | %{
$row = New-Object PSObject
Add-Member -InputObject $row -Name VM -Value $_.Name -MemberType NoteProperty
Add-Member -InputObject $row -Name Time1 -Value $start -MemberType NoteProperty
Add-Member -InputObject $row -Name Host1 -Value $_.Value -MemberType NoteProperty
Add-Member -InputObject $row -Name DRS1 -Value $false -MemberType NoteProperty
$report += $row
}

$report | Export-Csv "C:\tmp\vMotion-history.csv" -NoTypeInformation -UseCulture

Reply
0 Kudos
LucD
Leadership
Leadership

That is not the script I linked to.
Where does this come from?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
anthonyhwynn
Contributor
Contributor

it from your website...can you please share again thanks

Reply
0 Kudos
LucD
Leadership
Leadership

The link is in my 1st reply in this thread.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
anthonyhwynn
Contributor
Contributor

thanks it working now,  how do i export all info into xls file.

Reply
0 Kudos
LucD
Leadership
Leadership

If a CSV is sufficient you can pipe the result to an Export-Csv cmdet.
If you want an XLSX file, you will have to install the ImportExcel module, and pipe the result to the Export-Excel cmdlet.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
anthonyhwynn
Contributor
Contributor

after run script, 

Get-MotionHistory -Entity (Get-VM VM*) -Days 1 |
Export-Csv C:\motion-report1.csv -NoTypeINformation -UseCultureand

 

then run this script get an error

Reply
0 Kudos
LucD
Leadership
Leadership

That error seems to say that Get-VM VM* didn't return anything.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Tags (1)
Reply
0 Kudos