cgarciamoran
Contributor
Contributor

Multiple vCenter VM / Host Count

Jump to solution

Hey Folks,

So im trying to get a Count of VM / Hosts in mutiple vCenters so I can generate a report and email it. Im now expert by any means but I thought this would work except I keep getting the Total # of Hosts / VM's in the results of each VC I query, Ive tried setting the VAR's to 0/null and get the same results also moves the connect-viserver outside and inside the for loop same results.

So what am I missing

## Connect to VirtualCenter

Connect-VIServer -Server $vCenter | Out-Null

foreach ($VC in $vCenter)

    {

       

        # Total number of hosts

       

        $TotalVMHosts = Get-VMHost

        $TotalVMHostsCount = $TotalVMHosts.count

   

        # Total number of guests

       

        $TotalVMs = Get-VM

        $TotalVMsCount = $TotalVMs.count

   

        $VMHostresult = New-Object PSObject

        $VMHostresult | add-member -MemberType NoteProperty -Name "vSphere vCenter" -Value $VC

        $VMHostresult | add-member -MemberType NoteProperty -Name "Total Vms" -Value $TotalVMsCount

        $VMHostresult | add-member -MemberType NoteProperty -Name "Total Hosts" -Value $TotalVMHostsCount

        $VMHostresult

       

       

        }

#Disconnect from VirtualCenter , Last line of script

Disconnect-VIServer $vCenter -Confirm:$false | Out-Null

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Try something like this.

It will give you the totals per vCenter, and at the end the grand total.

$vCenter = @(""Vcenter1", "Vcenter2", 'Vcenter3')

$TotalVMHosts = @()

$TotalVMHostsCount = 0

$TotalVMs = @()

$TotalVMCount = 0

## Connect to VirtualCenter

Connect-VIServer -Server $vCenter | Out-Null

foreach ($VC in $vCenter)

{

      

        # Total number of hosts

      

        $tempEsx = Get-VMHost -Server $VC

        $TotalVMHosts += $tempEsx

        $TotalVMHostsCount += $tempEsx.count

  

        # Total number of guests

      

        $tempVM = Get-VM -Server $VC

        $TotalVMs += $tempVM

        $TotalVMsCount += $tempVM.count

        $VMHostresult = New-Object PSObject

        $VMHostresult | add-member -MemberType NoteProperty -Name "vSphere vCenter" -Value $VC

        $VMHostresult | add-member -MemberType NoteProperty -Name "Total Vms" -Value $tempVM.Count

        $VMHostresult | add-member -MemberType NoteProperty -Name "Total Hosts" -Value $tempEsx.Count

        $VMHostresult

}

$VMHostresult = New-Object PSObject

$VMHostresult | add-member -MemberType NoteProperty -Name "vSphere vCenter" -Value 'All'

$VMHostresult | add-member -MemberType NoteProperty -Name "Total Vms" -Value $TotalVMsCount

$VMHostresult | add-member -MemberType NoteProperty -Name "Total Hosts" -Value $TotalVMHostsCount

$VMHostresult

#Disconnect from VirtualCenter , Last line of script

Disconnect-VIServer $vCenter -Confirm:$false | Out-Null


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

View solution in original post

12 Replies
LucD
Leadership
Leadership

Are you already connected to multiple vCenters before you start the script?

In the script there is only one Connect-VIServer.

What is in $global:defaultviservers before you start the script?


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

0 Kudos
cgarciamoran
Contributor
Contributor

Sorry forgot this part on top of the script, that I use for the multiple vCenter Value

$vCenter = @("Vcenter1", "Vcenter2", 'Vcenter3')

0 Kudos
LucD
Leadership
Leadership

Try something like this.

It will give you the totals per vCenter, and at the end the grand total.

$vCenter = @(""Vcenter1", "Vcenter2", 'Vcenter3')

$TotalVMHosts = @()

$TotalVMHostsCount = 0

$TotalVMs = @()

$TotalVMCount = 0

## Connect to VirtualCenter

Connect-VIServer -Server $vCenter | Out-Null

foreach ($VC in $vCenter)

{

      

        # Total number of hosts

      

        $tempEsx = Get-VMHost -Server $VC

        $TotalVMHosts += $tempEsx

        $TotalVMHostsCount += $tempEsx.count

  

        # Total number of guests

      

        $tempVM = Get-VM -Server $VC

        $TotalVMs += $tempVM

        $TotalVMsCount += $tempVM.count

        $VMHostresult = New-Object PSObject

        $VMHostresult | add-member -MemberType NoteProperty -Name "vSphere vCenter" -Value $VC

        $VMHostresult | add-member -MemberType NoteProperty -Name "Total Vms" -Value $tempVM.Count

        $VMHostresult | add-member -MemberType NoteProperty -Name "Total Hosts" -Value $tempEsx.Count

        $VMHostresult

}

$VMHostresult = New-Object PSObject

$VMHostresult | add-member -MemberType NoteProperty -Name "vSphere vCenter" -Value 'All'

$VMHostresult | add-member -MemberType NoteProperty -Name "Total Vms" -Value $TotalVMsCount

$VMHostresult | add-member -MemberType NoteProperty -Name "Total Hosts" -Value $TotalVMHostsCount

$VMHostresult

#Disconnect from VirtualCenter , Last line of script

Disconnect-VIServer $vCenter -Confirm:$false | Out-Null


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

cgarciamoran
Contributor
Contributor

Okay that worked much better than mine thanks!, the only issue seems to be the Grand total , the Host count is fine, but the VM Count shows the # for the last vcenter VM count instead of the Grand total of VM's

0 Kudos
LucD
Leadership
Leadership

My bad, I forgot the + sign.
I updated the code above.


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

0 Kudos
cgarciamoran
Contributor
Contributor

Lol and I looked over 5 times and I missed it as well , Many Many thanks!

0 Kudos
flickerflacker
Contributor
Contributor

Hello LucD.

How could you please help me with getting this to output to a text file? I keep coming up empty, and get just a file with totals instead of actual lines. Any thoughts would be greatly appreciated. 

-ff

0 Kudos
LucD
Leadership
Leadership

How did you try to place the result in a TXT file?
And which lines do you want in there, all the totals or only the grand total?


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

0 Kudos
flickerflacker
Contributor
Contributor

I just added a | out-file c:\count.txt to the last $vmhostresult line and I get a total. What I'm trying to do is out put a txt file that looks like the final output in the buffer. 

0 Kudos
flickerflacker
Contributor
Contributor
#############ref: LucD -https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Multiple-vCenter-VM-Host-Count/m-p/2876171#M104022-#############
#↔_↔
#import module
import-module vmware.powercli
$vCenter = @(")
$TotalVMHosts = @()
$TotalVMHostsCount = 0
$TotalVMs = @()
$TotalVMCount = 0
## Connect to VirtualCenters
Connect-VIServer -Server $vCenter -user | Out-Null
foreach ($VC in $vCenter)
{     
        # Total number of hosts
        $tempEsx = Get-VMHost -Server $VC
        $TotalVMHosts += $tempEsx
        $TotalVMHostsCount += $tempEsx.count 
        # Total number of guests     
        $tempVM = (Get-VM -Server $VC).where{$_.PowerState -eq 'PoweredOn' -and $_.Guest.OSFullName -match 'Windows Server'}
		$tempVML = (Get-VM -Server $VC).where{$_.PowerState -eq 'PoweredOn' -and $_.Guest.OSFullName -match 'Linux|Other'}
        $TotalVMs += $tempVM
		$TotalVMLs += $tempVML
        $TotalVMsCount += $tempVM.count
		$TotalVMLsCount += $tempVML.count
        $VMHostresult = New-Object PSObject
        $VMHostresult | add-member -MemberType NoteProperty -Name "vSphere vCenter" -Value $VC
        $VMHostresult | add-member -MemberType NoteProperty -Name "Windows" -Value $tempVM.Count
		$VMHostresult | add-member -MemberType NoteProperty -Name "Linux/Other" -Value $tempVML.Count
        $VMHostresult | add-member -MemberType NoteProperty -Name "Total Hosts" -Value $tempEsx.Count
        $VMHostresult
}
$VMHostresult = New-Object PSObject 
$VMHostresult | add-member -MemberType NoteProperty -Name "vSphere vCenter" -Value 'All'
$VMHostresult | add-member -MemberType NoteProperty -Name "Windows" -Value $TotalVMsCount
$VMHostresult | add-member -MemberType NoteProperty -Name "Linux/Other" -Value $TotalVMLsCount
$VMHostresult | add-member -MemberType NoteProperty -Name "Total Hosts" -Value $TotalVMHostsCount
$VMHostresult | out-file c:\scripts\vsi_count\count.txt
#Disconnect from VirtualCenter , Last line of script
Disconnect-VIServer $vCenter -Confirm:$false | Out-Null
0 Kudos
LucD
Leadership
Leadership

That is since you are only writing the totals to the TXT file.
Those last lines should be something like this

 

        $VMHostresult | out-file c:\scripts\vsi_count\count.txt -Append
}
$VMHostresult = New-Object PSObject 
$VMHostresult | add-member -MemberType NoteProperty -Name "vSphere vCenter" -Value 'All'
$VMHostresult | add-member -MemberType NoteProperty -Name "Windows" -Value $TotalVMsCount
$VMHostresult | add-member -MemberType NoteProperty -Name "Linux/Other" -Value $TotalVMLsCount
$VMHostresult | add-member -MemberType NoteProperty -Name "Total Hosts" -Value $TotalVMHostsCount
$VMHostresult | out-file c:\scripts\vsi_count\count.txt -Append

 

But be aware that sending the results like that to TXT file will cause repeated headers.


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

flickerflacker
Contributor
Contributor

Big miss on my part. Thank a lot for your help.

0 Kudos