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!!