VMware Cloud Community
BenLiebowitz
Expert
Expert
Jump to solution

Script to count PoweredOn VMs in multiple vCenters

I have a script that will give me the total count of Powered on VMs in each environment, but I would love for it to automatically add the values together and display an overall total, instead of exporting each vCenter's count to a CSV. 

For example, I'd like it to see total VC01=100, VC02=200, VC03=50, VC04=250, VC05=100.  Total = 700 Powered On VMs. 

Anyone have any suggestions? 

------------------------

# Build array for each vCenter with vDS switch

  $array = "vc01", "vc0e", "vc0e", "vc04", "vc05"

  for($count=0;$count -lt $array.length; $count++)

{

# Connect to All vCenter Servers, one at a time.

connect-viserver $array[$count]

# Get VM counts & export to CSV

Get-VMHost | Get-VM | where-object {$_.PowerState -eq "PoweredOn"}| Measure-Object | export-csv -notypeinformation c:\ben\vmware\$($array[$count])_vm_count.csv

# Disconnect from vCenter Servers

disconnect-viserver -confirm:$false

}

------------------------

Thanks in advance!

Ben Liebowitz, VCP vExpert 2015, 2016, & 2017 If you found my post helpful, please mark it as helpful or answered to award points.
Tags (1)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

If you only need the total, try like this

# Build array for each vCenter with vDS switch

$array = "vc01", "vc0e", "vc0e", "vc04", "vc05"

$sum = @()

for($count=0;$count -lt $array.length; $count++)

{

# Connect to All vCenter Servers, one at a time.

    connect-viserver $array[$count]

# Get VM counts & export to CSV

    $sum += (Get-VMHost | Get-VM | where-object {$_.PowerState -eq "PoweredOn"}).Count

# Disconnect from vCenter Servers

    disconnect-viserver -confirm:$false

}

$sum | Measure-Object -Sum | select -ExpandProperty Sum


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

4 Replies
LucD
Leadership
Leadership
Jump to solution

If you only need the total, try like this

# Build array for each vCenter with vDS switch

$array = "vc01", "vc0e", "vc0e", "vc04", "vc05"

$sum = @()

for($count=0;$count -lt $array.length; $count++)

{

# Connect to All vCenter Servers, one at a time.

    connect-viserver $array[$count]

# Get VM counts & export to CSV

    $sum += (Get-VMHost | Get-VM | where-object {$_.PowerState -eq "PoweredOn"}).Count

# Disconnect from vCenter Servers

    disconnect-viserver -confirm:$false

}

$sum | Measure-Object -Sum | select -ExpandProperty Sum


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

BenLiebowitz
Expert
Expert
Jump to solution

You're the MAN!

That worked great!

I added this to the end to clear the screen and display the count.

clear 

write-host ""

write-host "Total Powered On VMs: "

$sum | Measure-Object -Sum | select -ExpandProperty Sum

Thanks again Luc! 

Ben Liebowitz, VCP vExpert 2015, 2016, & 2017 If you found my post helpful, please mark it as helpful or answered to award points.
0 Kudos
ScottDriver42
Enthusiast
Enthusiast
Jump to solution

Ben, did you want a breakdown by VC as well?

As usual Luc is correct, but if you wanted a breakdown you could modify like this:

### get vm counts

$vc=(Get-VMHost | Get-VM | where-object {$_.PowerState -eq "PoweredOn"}).Count

write-host "$array[$count] - $vc"   

$sum+=$vc

Blog: https://virtualvt.wordpress.com/ | Twitter: VTsnowboarder42
BenLiebowitz
Expert
Expert
Jump to solution

No, I'm looking for overall count, but I'll keep this as a reference.  Smiley Happy  Thanks!

Ben Liebowitz, VCP vExpert 2015, 2016, & 2017 If you found my post helpful, please mark it as helpful or answered to award points.
0 Kudos