VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Unable to get correct information from multiple vCenters

Hi,

I am unable to get the correct correct information when running for multiple vCenters. I get the correct information, if I run against one vCenter but when I run against multiple vCenters I am getting incorrect info

Please help!!

$vCenters = @(
"vcenter1"
"vcenter2"
"vcenter3"
)
Connect-viserver -Server $vCenters

$report = Foreach ($vc in $global:DefaultVIServers)
{
$Output = @()
Get-Cluster | %{
$hypCluster = $_
## get the GenericMeasureInfo for the desired properties for this cluster's hosts
$infoCPUMEM = Get-View -ViewType HostSystem -Property Hardware.CpuInfo,Hardware.memorysize -SearchRoot $hypCluster.Id |
Select @{n="NumCpuSockets"; e={$_.Hardware.CpuInfo.NumCpuPackages}}, @{n="NumCpuCores"; e={$_.Hardware.CpuInfo.NumCpuCores}}, @{n="NumCpuThreads"; e={$_.Hardware.CpuInfo.NumCpuThreads}},@{n="PhysicalMem"; E={""+[math]::round($_.Hardware.MemorySize / 1GB, 0)}} |
Measure-Object -Sum NumCpuSockets,NumCpuCores,NumCpuThreads,PhysicalMem
## return an object with info about VMHosts' CPU characteristics
$temp= New-Object psobject
$datacenter = Get-Datacenter -Cluster $hypCluster.Name
$NumVMHosts = if ($infoCPUMEM) {$infoCPUMEM[0].Count} else {0}
$NumCpuSockets = ($infoCPUMEM | ?{$_.Property -eq "NumCpuSockets"}).Sum
$NumCpuCores = ($infoCPUMEM | ?{$_.Property -eq "NumCpuCores"}).Sum
$vmdetails = Get-VM -Location $hypCluster
$NumvCPU = ( $vmdetails | Measure-Object NumCpu -Sum).Sum
$VirtualMem= [Math]::Round(($vmdetails | Measure-Object MemoryGB -Sum).Sum, 2)
$PhysicalMem = ($infoCPUMEM | ?{$_.Property -eq "PhysicalMem"}).Sum
##Calculating the vCPU to pCPU ratio AND vRAM to pRAM ratio.
if ($NumvCPU -ne "0") {$cpuRatio= "$("{0:N2}" -f ($NumvCPU/$NumCpuCores))" + ":1"}
if ($VirtualMem -ne "0") {$memRatio= "$("{0:N2}" -f ($VirtualMem/$PhysicalMem))" + ":1"}
$temp | Add-Member -MemberType Noteproperty "Datacenter" -Value $datacenter
$temp | Add-Member -MemberType Noteproperty "ClusterName" -Value $hypCluster.Name
$temp | Add-Member -MemberType Noteproperty "NumVMHosts" -Value $NumVMHosts
$temp | Add-Member -MemberType Noteproperty "NumPCPUSockets" -Value $NumCpuSockets
$temp | Add-Member -MemberType Noteproperty "NumPCPUCores" -Value $NumCpuCores
$temp | Add-Member -MemberType Noteproperty "NumvCPU" -Value $NumvCPU
$temp | Add-Member -MemberType Noteproperty "vCPU-pCPUCoreRatio" -Value $cpuRatio
$temp | Add-Member -MemberType Noteproperty "PhysicalMem(GB)" -Value $PhysicalMem
$temp | Add-Member -MemberType Noteproperty "VirtualMem(GB)" -Value $VirtualMem
$temp | Add-Member -MemberType Noteproperty "vRAM-pRAMRatio" -Value $memRatio
$Output+=$temp
}
#$Output | Sort-Object Account | Export-Csv -NoTypeInformation $outputFile
$Output | Sort-Object Account | Export-Excel -Path $outputFile -AutoFilter -AutoSize -WorksheetName All_vCenters_CPU_Memory_Ratio
}
$report

 

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Start by using the Server parameter on the cmdlets


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

View solution in original post

Reply
0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

What do you mean by "incorrect info"?

Note that depending on your setting of DefaultVIServerMode, a cmdlet like Get-Cluster without a Server parameter, might return all clusters from all vCenters that you are connected to.


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

The Host count, CPU and Memory count shows more than that is present in the each vcenter

ganapa2000_0-1655392714532.png

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Start by using the Server parameter on the cmdlets


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

I tried as below, still no luck 😞

$vCenters = @(
"vCenter1"
"vCenter2"
"vCenter3"
)
Connect-viserver -Server $vCenters

$report = Foreach ($vc in $global:DefaultVIServers)
{
$Output = @()
Get-Cluster -server $vc | %{
$hypCluster = $_
## get the GenericMeasureInfo for the desired properties for this cluster's hosts
$infoCPUMEM = Get-View -ViewType HostSystem -Property Hardware.CpuInfo,Hardware.memorysize -SearchRoot $hypCluster.Id |
Select @{n="NumCpuSockets"; e={$_.Hardware.CpuInfo.NumCpuPackages}}, @{n="NumCpuCores"; e={$_.Hardware.CpuInfo.NumCpuCores}}, @{n="NumCpuThreads"; e={$_.Hardware.CpuInfo.NumCpuThreads}},@{n="PhysicalMem"; E={""+[math]::round($_.Hardware.MemorySize / 1GB, 0)}} |
Measure-Object -Sum NumCpuSockets,NumCpuCores,NumCpuThreads,PhysicalMem
## return an object with info about VMHosts' CPU characteristics
$temp= New-Object psobject
$datacenter = Get-Datacenter -Cluster $hypCluster.Name -server $vc
$NumVMHosts = if ($infoCPUMEM) {$infoCPUMEM[0].Count} else {0}
$NumCpuSockets = ($infoCPUMEM | ?{$_.Property -eq "NumCpuSockets"}).Sum
$NumCpuCores = ($infoCPUMEM | ?{$_.Property -eq "NumCpuCores"}).Sum
$vmdetails = Get-VM -Location $hypCluster -server $vc
$NumvCPU = ( $vmdetails | Measure-Object NumCpu -Sum).Sum
$VirtualMem= [Math]::Round(($vmdetails | Measure-Object MemoryGB -Sum).Sum, 2)
$PhysicalMem = ($infoCPUMEM | ?{$_.Property -eq "PhysicalMem"}).Sum
##Calculating the vCPU to pCPU ratio AND vRAM to pRAM ratio.
if ($NumvCPU -ne "0") {$cpuRatio= "$("{0:N2}" -f ($NumvCPU/$NumCpuCores))" + ":1"}
if ($VirtualMem -ne "0") {$memRatio= "$("{0:N2}" -f ($VirtualMem/$PhysicalMem))" + ":1"}
$temp | Add-Member -MemberType Noteproperty "Datacenter" -Value $datacenter
$temp | Add-Member -MemberType Noteproperty "ClusterName" -Value $hypCluster.Name
$temp | Add-Member -MemberType Noteproperty "NumVMHosts" -Value $NumVMHosts
$temp | Add-Member -MemberType Noteproperty "NumPCPUSockets" -Value $NumCpuSockets
$temp | Add-Member -MemberType Noteproperty "NumPCPUCores" -Value $NumCpuCores
$temp | Add-Member -MemberType Noteproperty "NumvCPU" -Value $NumvCPU
$temp | Add-Member -MemberType Noteproperty "vCPU-pCPUCoreRatio" -Value $cpuRatio
$temp | Add-Member -MemberType Noteproperty "PhysicalMem(GB)" -Value $PhysicalMem
$temp | Add-Member -MemberType Noteproperty "VirtualMem(GB)" -Value $VirtualMem
$temp | Add-Member -MemberType Noteproperty "vRAM-pRAMRatio" -Value $memRatio
$Output+=$temp
}
#$Output | Sort-Object Account | Export-Csv -NoTypeInformation $outputFile
$Output | Sort-Object Account | Export-Excel -Path $outputFile -AutoFilter -AutoSize -WorksheetName All_vCenters_CPU_Memory_Ratio
}
$report

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You didn't use the Server parameter on the Get-View cmdlet


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

After making the changes as suggested, I am output from 1st vcenter and other two vcenters info is missing in the report

$report = Foreach ($vc in $global:DefaultVIServers)
{
$Output = @()
Get-Cluster -server $vc | %{
$hypCluster = $_
## get the GenericMeasureInfo for the desired properties for this cluster's hosts
$infoCPUMEM = Get-View -ViewType HostSystem -Property Hardware.CpuInfo,Hardware.memorysize -SearchRoot $hypCluster.Id -server $vc |
Select @{n="NumCpuSockets"; e={$_.Hardware.CpuInfo.NumCpuPackages}}, @{n="NumCpuCores"; e={$_.Hardware.CpuInfo.NumCpuCores}}, @{n="NumCpuThreads"; e={$_.Hardware.CpuInfo.NumCpuThreads}},@{n="PhysicalMem"; E={""+[math]::round($_.Hardware.MemorySize / 1GB, 0)}} |
Measure-Object -Sum NumCpuSockets,NumCpuCores,NumCpuThreads,PhysicalMem
## return an object with info about VMHosts' CPU characteristics
$temp= New-Object psobject
$datacenter = Get-Datacenter -Cluster $hypCluster.Name -server $vc
$NumVMHosts = if ($infoCPUMEM) {$infoCPUMEM[0].Count} else {0}
$NumCpuSockets = ($infoCPUMEM | ?{$_.Property -eq "NumCpuSockets"}).Sum
$NumCpuCores = ($infoCPUMEM | ?{$_.Property -eq "NumCpuCores"}).Sum
$vmdetails = Get-VM -Location $hypCluster -server $vc
$NumvCPU = ( $vmdetails | Measure-Object NumCpu -Sum).Sum
$VirtualMem= [Math]::Round(($vmdetails | Measure-Object MemoryGB -Sum).Sum, 2)
$PhysicalMem = ($infoCPUMEM | ?{$_.Property -eq "PhysicalMem"}).Sum
##Calculating the vCPU to pCPU ratio AND vRAM to pRAM ratio.
if ($NumvCPU -ne "0") {$cpuRatio= "$("{0:N2}" -f ($NumvCPU/$NumCpuCores))" + ":1"}
if ($VirtualMem -ne "0") {$memRatio= "$("{0:N2}" -f ($VirtualMem/$PhysicalMem))" + ":1"}
$temp | Add-Member -MemberType Noteproperty "Datacenter" -Value $datacenter
$temp | Add-Member -MemberType Noteproperty "ClusterName" -Value $hypCluster.Name
$temp | Add-Member -MemberType Noteproperty "NumVMHosts" -Value $NumVMHosts
$temp | Add-Member -MemberType Noteproperty "NumPCPUSockets" -Value $NumCpuSockets
$temp | Add-Member -MemberType Noteproperty "NumPCPUCores" -Value $NumCpuCores
$temp | Add-Member -MemberType Noteproperty "NumvCPU" -Value $NumvCPU
$temp | Add-Member -MemberType Noteproperty "vCPU-pCPUCoreRatio" -Value $cpuRatio
$temp | Add-Member -MemberType Noteproperty "PhysicalMem(GB)" -Value $PhysicalMem
$temp | Add-Member -MemberType Noteproperty "VirtualMem(GB)" -Value $VirtualMem
$temp | Add-Member -MemberType Noteproperty "vRAM-pRAMRatio" -Value $memRatio
$Output+=$temp
}
#$Output | Sort-Object Account | Export-Csv -NoTypeInformation $outputFile
$Output | Sort-Object Account | Export-Excel -Path $outputFile -AutoFilter -AutoSize -WorksheetName All_vCenters_CPU_Memory_Ratio
}
$report

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

What are you actually storing in $report?

Also, you are overwriting that same worksheet in the XLSX each loop.


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

After I made the changes as below, it is now working as expected. Thanks for your help 🙂

Let me know, if this is correct.

$report = Foreach ($vc in $global:DefaultVIServers)
{
$Output = @()
Get-Cluster -server $vc | %{
$hypCluster = $_
## get the GenericMeasureInfo for the desired properties for this cluster's hosts
$infoCPUMEM = Get-View -ViewType HostSystem -Property Hardware.CpuInfo,Hardware.memorysize -SearchRoot $hypCluster.Id -server $vc |
Select @{n="NumCpuSockets"; e={$_.Hardware.CpuInfo.NumCpuPackages}}, @{n="NumCpuCores"; e={$_.Hardware.CpuInfo.NumCpuCores}}, @{n="NumCpuThreads"; e={$_.Hardware.CpuInfo.NumCpuThreads}},@{n="PhysicalMem"; E={""+[math]::round($_.Hardware.MemorySize / 1GB, 0)}} |
Measure-Object -Sum NumCpuSockets,NumCpuCores,NumCpuThreads,PhysicalMem
## return an object with info about VMHosts' CPU characteristics
$temp= New-Object psobject
$datacenter = Get-Datacenter -Cluster $hypCluster.Name -server $vc
$NumVMHosts = if ($infoCPUMEM) {$infoCPUMEM[0].Count} else {0}
$NumCpuSockets = ($infoCPUMEM | ?{$_.Property -eq "NumCpuSockets"}).Sum
$NumCpuCores = ($infoCPUMEM | ?{$_.Property -eq "NumCpuCores"}).Sum
$vmdetails = Get-VM -Location $hypCluster -server $vc
$NumvCPU = ( $vmdetails | Measure-Object NumCpu -Sum).Sum
$VirtualMem= [Math]::Round(($vmdetails | Measure-Object MemoryGB -Sum).Sum, 2)
$PhysicalMem = ($infoCPUMEM | ?{$_.Property -eq "PhysicalMem"}).Sum
##Calculating the vCPU to pCPU ratio AND vRAM to pRAM ratio.
if ($NumvCPU -ne "0") {$cpuRatio= "$("{0:N2}" -f ($NumvCPU/$NumCpuCores))" + ":1"}
if ($VirtualMem -ne "0") {$memRatio= "$("{0:N2}" -f ($VirtualMem/$PhysicalMem))" + ":1"}
$temp | Add-Member -MemberType Noteproperty "Datacenter" -Value $datacenter
$temp | Add-Member -MemberType Noteproperty "ClusterName" -Value $hypCluster.Name
$temp | Add-Member -MemberType Noteproperty "NumVMHosts" -Value $NumVMHosts
$temp | Add-Member -MemberType Noteproperty "NumPCPUSockets" -Value $NumCpuSockets
$temp | Add-Member -MemberType Noteproperty "NumPCPUCores" -Value $NumCpuCores
$temp | Add-Member -MemberType Noteproperty "NumvCPU" -Value $NumvCPU
$temp | Add-Member -MemberType Noteproperty "vCPU-pCPUCoreRatio" -Value $cpuRatio
$temp | Add-Member -MemberType Noteproperty "PhysicalMem(GB)" -Value $PhysicalMem
$temp | Add-Member -MemberType Noteproperty "VirtualMem(GB)" -Value $VirtualMem
$temp | Add-Member -MemberType Noteproperty "vRAM-pRAMRatio" -Value $memRatio
$Output+=$temp
}
#$Output | Sort-Object Account | Export-Csv -NoTypeInformation $outputFile
$Output
}
$report | Sort-Object Datacenter | Export-Excel -Path $outputFile -AutoFilter -AutoSize -WorksheetName All_vCenters_CPU_Memory_Ratio

Tags (1)
Reply
0 Kudos