PowerCLI

 View Only
  • 1.  Unable to get correct information from multiple vCenters

    Posted Jun 16, 2022 02:08 PM

    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

     



  • 2.  RE: Unable to get correct information from multiple vCenters

    Posted Jun 16, 2022 02:19 PM

    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.



  • 3.  RE: Unable to get correct information from multiple vCenters

    Posted Jun 16, 2022 03:19 PM

    LucD,

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

    ganapa2000_0-1655392714532.png

     



  • 4.  RE: Unable to get correct information from multiple vCenters
    Best Answer

    Posted Jun 16, 2022 03:28 PM

    Start by using the Server parameter on the cmdlets



  • 5.  RE: Unable to get correct information from multiple vCenters

    Posted Jun 16, 2022 03:55 PM

    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



  • 6.  RE: Unable to get correct information from multiple vCenters

    Posted Jun 16, 2022 04:02 PM

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



  • 7.  RE: Unable to get correct information from multiple vCenters

    Posted Jun 16, 2022 04:14 PM

    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



  • 8.  RE: Unable to get correct information from multiple vCenters

    Posted Jun 16, 2022 04:32 PM

    What are you actually storing in $report?

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



  • 9.  RE: Unable to get correct information from multiple vCenters

    Posted Jun 16, 2022 04:57 PM

    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