Automation

 View Only
Expand all | Collapse all

Count of vMotion and Storage vMotion for the past 24 hrs

  • 1.  Count of vMotion and Storage vMotion for the past 24 hrs

    Posted Apr 28, 2014 03:37 AM

    I am running the below one line to get the Count of vMotion and Storage vMotion which has happened in the past 24 hrs in the environment and it give me the required output , But when I combine them in the script which I use to collect additional information it just returns 0(zero) as the count.

    Count on Storage vMotion and vMotion

    Get-VIEvent -MaxSamples ([int]::MaxValue) -Start (Get-Date).AddDays(-1) |

    Where { $_.GetType().Name -eq "TaskEvent" -and $_.Info.DescriptionId -eq "VirtualMachine.relocate" -or $_.Info.DescriptionId -eq "StorageResourceManager.applyRecommendation"} |

    Measure-Object | Select-Object -ExpandProperty Count

    Get-VIEvent -MaxSamples ([int]::MaxValue) -Start (Get-Date).AddDays(-1) |

    Where { $_.GetType().Name -eq "TaskEvent" -and $_.Info.DescriptionId -eq "VirtualMachine.migrate"} |

    Measure-Object | Select-Object -ExpandProperty Count

    Combined Script

    Get-Cluster |

    ForEach-Object {

      $Cluster = $_

      $VMHost = $Cluster | Get-VMHost

      $CpuUsageMhz = $VMHost | Measure-Object -Property CpuUsageMhz -Sum | Select-Object -ExpandProperty Sum

      $CpuTotalMhz = $VMHost | Measure-Object -Property CpuTotalMhz -Sum | Select-Object -ExpandProperty Sum

      $MemoryUsageGB = $VMHost | Measure-Object -Property MemoryUsageGB -Sum | Select-Object -ExpandProperty Sum

      $MemoryTotalGB = $VMHost | Measure-Object -Property MemoryTotalGB -Sum | Select-Object -ExpandProperty Sum

      $Cluster | Select-Object -Property @{Name="ClusterName";Expression={$_.Name}},

      @{Name="vSphere HA";Expression={$_.HAEnabled}},

      @{Name="HA-Host Monitoring";Expression={$_.ExtensionData.Configuration.DasConfig.HostMonitoring}},

      @{Name="HA-Failover Capacity";Expression={$_.ExtensionData.Summary.CurrentFailoverLevel}},

      @{Name="HA-Admissions Control";Expression={$_.HAAdmissionControlEnabled}}, 

      @{Name="Configuration Issue";Expression={[string]::Join(',',($_.ExtensionData.ConfigIssue | Select -ExpandProperty FullFormattedMessage))}},

      @{Name="vSphere DRS";Expression={$_.DrsEnabled}},

      @{Name="DRS Automation Level";Expression={$_.DrsAutomationLevel}},

      @{Name="Cpu Usage (Mhz)";Expression={$CpuUsageMhz}},

      @{Name="Cpu Total(Mhz)";Expression={$CpuTotalMhz}},

      @{Name="Cpu Usage(%)";Expression={[math]::Round(100*$CpuUsageMhz/$CpuTotalMhz)}},

      @{Name="Memory Usage(GB)";Expression={$MemoryUsageGB}},

      @{Name="Memory Total(GB)";Expression={$MemoryTotalGB}},

      @{Name="Memory Usage (%)";Expression={[math]::Round(100*$MemoryUsageGB/$MemoryTotalGB)}},

      @{Name="Num of VMs";Expression={$_ | Get-VM | Measure-Object | Select-Object -ExpandProperty Count}},

      @{Name="Num of VMs without VMXNET3";Expression={$_ | Get-VM | Get-NetworkAdapter | Where-object {$_.Type -ne "Vmxnet3"} | Measure-Object | Select-Object -ExpandProperty Count}},

      @{Name="Num of vMotions";Expression={$_ | Get-VIEvent -MaxSamples ([int]::MaxValue) -Start (Get-Date).AddDays(-1) | Where { $_.GetType().Name -eq "TaskEvent" -and $_.Info.DescriptionId -eq "VirtualMachine.migrate"} | Measure-Object | Select-Object -ExpandProperty Count}},

      @{Name="Num of SvMotions";Expression={$_ | Get-VIEvent -MaxSamples ([int]::MaxValue) -Start (Get-Date).AddDays(-1) | Where { $_.GetType().Name -eq "TaskEvent" -and $_.Info.DescriptionId -eq "VirtualMachine.relocate" -or $_.Info.DescriptionId -eq "StorageResourceManager.applyRecommendation"} | Measure-Object | Select-Object -ExpandProperty Count}}  

    } |

    Export-Csv "C:\Script\Clusters.csv" -NoTypeInformation -UseCulture

    Could you please someone check the above script and let me know if there is something wrong

    Thanks!!



  • 2.  RE: Count of vMotion and Storage vMotion for the past 24 hrs

    Posted Apr 28, 2014 05:13 AM

    A ForEach block doesn't place any objects on the pipeline, so there will be no objects passed to Export-Csv, and hence your CSV file will be empty.

    One way to solve this, is to use the call operator (&).

    Like this

    &{Get-Cluster |

    ForEach-Object {

    ....

    }} |

    Export-Csv "C:\Script\Clusters.csv" -NoTypeInformation -UseCulture



  • 3.  RE: Count of vMotion and Storage vMotion for the past 24 hrs

    Posted Apr 28, 2014 05:54 AM

    I have tried using the call operator (&) as mentioned above but it still returns only 0,



  • 4.  RE: Count of vMotion and Storage vMotion for the past 24 hrs

    Posted Apr 28, 2014 06:01 AM

    Can you attach the latest version of the script as a file ?



  • 5.  RE: Count of vMotion and Storage vMotion for the past 24 hrs

    Posted Apr 28, 2014 06:23 AM

    Script attached



  • 6.  RE: Count of vMotion and Storage vMotion for the past 24 hrs

    Posted Apr 28, 2014 06:52 AM

    Seems to be working just fine for me.

    You are connected to a vSphere server I assume ?

    Do you get any errors ?



  • 7.  RE: Count of vMotion and Storage vMotion for the past 24 hrs

    Posted Apr 28, 2014 07:48 AM

    Yes I have connected to the vCenter Server and I dont get any error the script run fine but the output is 0



  • 8.  RE: Count of vMotion and Storage vMotion for the past 24 hrs

    Posted Apr 28, 2014 01:49 PM

    Then you will have to do some debugging I'm afraid.

    Put some breakpoints in the script to check if all values are calculated correctly.

    Does Get-Cluster return anything ?



  • 9.  RE: Count of vMotion and Storage vMotion for the past 24 hrs

    Posted Apr 29, 2014 01:33 AM

    Get-Cluster returns be the required output, Even when I run the script except for vMotion and Storage vMotion I get the required output after the script execution..



  • 10.  RE: Count of vMotion and Storage vMotion for the past 24 hrs
    Best Answer

    Posted Apr 29, 2014 05:46 AM

    But wait a minute, do you mean that the "Num of vMotions" and "Num of SvMotions" properties have a 0 value ?

    That is normal, since you ask for the events only for the cluster entity, while you should do that for all the VMs in the cluster.

    Try the attached version.



  • 11.  RE: Count of vMotion and Storage vMotion for the past 24 hrs

    Posted Apr 29, 2014 06:49 AM

    Thanks Luc, Now I get the required output



  • 12.  RE: Count of vMotion and Storage vMotion for the past 24 hrs

    Posted Dec 22, 2020 08:20 PM

    Can you share your script after you have edited it to run for all VMs?