VMware Cloud Community
vmhyperv
Contributor
Contributor

No of VMs powered on and Powered-off per cluster

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

Tags (2)
Reply
0 Kudos
2 Replies
mattboren
Expert
Expert

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.

vmhyperv
Contributor
Contributor

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)

Reply
0 Kudos