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!
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
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
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, 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
No, I'm looking for overall count, but I'll keep this as a reference. Thanks!