Hey Folks,
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
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
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
Sorry forgot this part on top of the script, that I use for the multiple vCenter Value
$vCenter = @("Vcenter1", "Vcenter2", 'Vcenter3')
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
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
My bad, I forgot the + sign.
I updated the code above.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Lol and I looked over 5 times and I missed it as well , Many Many thanks!
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
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
#############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
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
Big miss on my part. Thank a lot for your help.