PowerCLI

 View Only
Expand all | Collapse all

powershell report cluster vmotion report

  • 1.  powershell report cluster vmotion report

    Posted Mar 15, 2022 07:37 PM

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



  • 2.  RE: powershell report cluster vmotion report

    Posted Mar 15, 2022 08:41 PM

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

     



  • 3.  RE: powershell report cluster vmotion report

    Posted Mar 16, 2022 04:55 PM
      |   view attached

    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



  • 4.  RE: powershell report cluster vmotion report

    Posted Mar 16, 2022 05:22 PM

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



  • 5.  RE: powershell report cluster vmotion report

    Posted Mar 16, 2022 07:23 AM


  • 6.  RE: powershell report cluster vmotion report

    Posted Mar 16, 2022 12:21 PM

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



  • 7.  RE: powershell report cluster vmotion report

    Posted Mar 16, 2022 12:47 PM

    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.



  • 8.  RE: powershell report cluster vmotion report

    Posted Mar 16, 2022 12:58 PM

    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



  • 9.  RE: powershell report cluster vmotion report

    Posted Mar 16, 2022 01:02 PM

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



  • 10.  RE: powershell report cluster vmotion report

    Posted Mar 16, 2022 01:04 PM

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



  • 11.  RE: powershell report cluster vmotion report

    Posted Mar 16, 2022 01:06 PM

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



  • 12.  RE: powershell report cluster vmotion report

    Posted Mar 16, 2022 01:39 PM

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



  • 13.  RE: powershell report cluster vmotion report

    Posted Mar 16, 2022 03:09 PM

    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.