Hi,
I wanted to know the Powr Cli script for finding out the no of vms powered on and powered-off per Cluster and output in csv format.
thanks
Kumar
Hello, vmhyperv-
You should be able to do this pretty easily with the Group-Object cmdlet. Using some standard PowerCLI cmdlets, it would be like:
(Get-Cluster | %{
$oCluster = $_
$oPowerStateInfo = $oCluster | Get-VM | Group-Object -Property PowerState -NoElement
New-Object -Type PSObject -Property @{
ClusterName = $oCluster.Name
VMsOn = ($oPowerStateInfo | ?{$_.Name -eq "PoweredOn"}).Count
VMsOff = ($oPowerStateInfo | ?{$_.Name -eq "PoweredOff"}).Count
} ## end new-object
} ## end foreach-object
) | Export-Csv C:\Temp\myVMPowerInfo.csv -NoTypeInformation -UseCulture
But, since speed matters, I prefer to use the Get-View cmdlet instead of the Get-Cluster and Get-VM cmdlets, which is as such:
(Get-View -ViewType ClusterComputeResource -Property Name | %{
$viewCluster = $_
$oPowerStateInfo = Get-View -ViewType VirtualMachine -Property Runtime.PowerState -SearchRoot $viewCluster.MoRef | Group-Object -Property @{e={$_.Runtime.PowerState}} -NoElement
New-Object -Type PSObject -Property @{
ClusterName = $oCluster.Name
VMsOn = ($oPowerStateInfo | ?{$_.Name -eq "PoweredOn"}).Count
VMsOff = ($oPowerStateInfo | ?{$_.Name -eq "PoweredOff"}).Count
} ## end new-object
} ## end foreach-object
) | Export-Csv C:\Temp\myVMPowerInfo.csv -NoTypeInformation -UseCulture
The second way is faster, increasingly as the size of the environment increases.
Enjoy.
Hello Matt,
Thanks for your prompt help.I will check the script in office and will let you know how its goes but my question has been answered.
Thanks
Kumar (vmhyperv)