VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Unable to get the host count

I am unable to get the host count for below script and getting error

Measure-Object : Input object "Host123" is not numeric.
At D:\CPU_Allocation_Info.ps1:47 char:19
+ ... $results | Measure-Object -sum "Host", "Total Sockets Per Host", "To ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidType: (Host123:PSObject) [Measure-Object], PSInvalidOperationException
+ FullyQualifiedErrorId : NonNumericInputObject,Microsoft.PowerShell.Commands.MeasureObjectCommand

Please help

$results = foreach ($vmhost in Get-VMHost)
{
$vms = $vmhost | Get-VM | where{$_.PowerState -eq 'PoweredOn'}
$vmsVcpucount= ($vms | Measure-Object -Property numcpu -Sum).sum
""|Select @{N="Cluster";E={Get-Cluster -VMHost $VMHost}},
@{N='Host';E={$vmhost.name}},
@{N="Total Sockets Per Host";E={$VMHost.ExtensionData.Summary.Hardware.numCpuPkgs}},
@{N="Total Cores Per Socket";E={($VMHost.ExtensionData.Summary.Hardware.numCpuCores)/($VMHost.ExtensionData.Summary.Hardware.numCpuPkgs)}},
@{N='Total Cores';E={$vmhost.numcpu}},
@{N='Total Logical Processors (vCPUs)';E={$vmhost.numcpu * 2}},
@{N='Logical Processors Allocated (vCPUs)';E={$vmsVcpucount}},
@{N='Available Logical Processors (vCPUs)';E={($vmhost.numcpu * 2)-$vmsVcpucount}}
}
$sum = $results | Measure-Object -sum "Host", "Total Sockets Per Host", "Total Cores Per Socket", "Total Cores", "Total Logical Processors (vCPUs)", "Logical Processors Allocated (vCPUs)", "Available Logical Processors (vCPUs)"
$row1 = "" | Select "Cluster", "Host", "Total Sockets Per Host", "Total Cores Per Socket", "Total Cores", "Total Logical Processors (vCPUs)", "Logical Processors Allocated (vCPUs)", "Available Logical Processors (vCPUs)"
$results += $row1
$row2 = "" | Select "Cluster", "Host", "Total Sockets Per Host", "Total Cores Per Socket", "Total Cores", "Total Logical Processors (vCPUs)", "Logical Processors Allocated (vCPUs)", "Available Logical Processors (vCPUs)"
$row2."Cluster" = 'Total'
$row2."Host" = $sum | where{$_.Property -eq "Host"} | select -ExpandProperty Count
$row2."Total Sockets Per Host" = $sum | where{$_.Property -eq "Total Sockets Per Host"} | select -ExpandProperty Sum
$row2."Total Cores Per Socket" = $sum | where{$_.Property -eq "Total Cores Per Socket"} | select -ExpandProperty Sum
$row2."Total Cores" = $sum | where{$_.Property -eq "Total Cores"} | select -ExpandProperty Sum
$row2."Total Logical Processors (vCPUs)" = $sum | where{$_.Property -eq "Total Logical Processors (vCPUs)"} | select -ExpandProperty Sum
$row2."Logical Processors Allocated (vCPUs)" = $sum | where{$_.Property -eq "Logical Processors Allocated (vCPUs)"} | select -ExpandProperty Sum
$row2."Available Logical Processors (vCPUs)" = $sum | where{$_.Property -eq "Available Logical Processors (vCPUs)"} | select -ExpandProperty Sum
$results += $row2

$results | sort Cluster, Host | Export-Excel -Path $reportlocation1 -AutoFilter -AutoSize -WorksheetName Host_CPU_Alloc

 

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Then you will need to change that into

$row2."Host" = $results.Count - 1


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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Like the error says, you have a value of type String in the Host property.
You can't make a Sun of Strings.

You could do

$results = foreach ($vmhost in Get-VMHost) {
    $vms = $vmhost | Get-VM | where { $_.PowerState -eq 'PoweredOn' }
    $vmsVcpucount = ($vms | Measure-Object -Property numcpu -Sum).sum
    "" | select @{N = "Cluster"; E = { Get-Cluster -VMHost $VMHost } },
    @{N = 'Host'; E = { $vmhost.name } },
    @{N = "Total Sockets Per Host"; E = { $VMHost.ExtensionData.Summary.Hardware.numCpuPkgs } },
    @{N = "Total Cores Per Socket"; E = { ($VMHost.ExtensionData.Summary.Hardware.numCpuCores) / ($VMHost.ExtensionData.Summary.Hardware.numCpuPkgs) } },
    @{N = 'Total Cores'; E = { $vmhost.numcpu } },
    @{N = 'Total Logical Processors (vCPUs)'; E = { $vmhost.numcpu * 2 } },
    @{N = 'Logical Processors Allocated (vCPUs)'; E = { $vmsVcpucount } },
    @{N = 'Available Logical Processors (vCPUs)'; E = { ($vmhost.numcpu * 2) - $vmsVcpucount } }
}
$sum = $results | Measure-Object -Sum "Total Sockets Per Host", "Total Cores Per Socket", "Total Cores", "Total Logical Processors (vCPUs)", "Logical Processors Allocated (vCPUs)", "Available Logical Processors (vCPUs)"
$row1 = "" | select "Cluster", "Host", "Total Sockets Per Host", "Total Cores Per Socket", "Total Cores", "Total Logical Processors (vCPUs)", "Logical Processors Allocated (vCPUs)", "Available Logical Processors (vCPUs)"
$results += $row1
$row2 = "" | select "Cluster", "Host", "Total Sockets Per Host", "Total Cores Per Socket", "Total Cores", "Total Logical Processors (vCPUs)", "Logical Processors Allocated (vCPUs)", "Available Logical Processors (vCPUs)"
$row2."Cluster" = 'Total'
$row2."Host" = $results.Count
$row2."Total Sockets Per Host" = $sum | where { $_.Property -eq "Total Sockets Per Host" } | select -ExpandProperty Sum
$row2."Total Cores Per Socket" = $sum | where { $_.Property -eq "Total Cores Per Socket" } | select -ExpandProperty Sum
$row2."Total Cores" = $sum | where { $_.Property -eq "Total Cores" } | select -ExpandProperty Sum
$row2."Total Logical Processors (vCPUs)" = $sum | where { $_.Property -eq "Total Logical Processors (vCPUs)" } | select -ExpandProperty Sum
$row2."Logical Processors Allocated (vCPUs)" = $sum | where { $_.Property -eq "Logical Processors Allocated (vCPUs)" } | select -ExpandProperty Sum
$row2."Available Logical Processors (vCPUs)" = $sum | where { $_.Property -eq "Available Logical Processors (vCPUs)" } | select -ExpandProperty Sum
$results += $row2

$results | sort Cluster, Host | Export-Excel -Path $reportlocation1 -AutoFilter -AutoSize -WorksheetName Host_CPU_Alloc


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Hi Luc,

I am getting incorrect host count. getting one count extra. I think it is a counting the first line which is blank.

 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Then you will need to change that into

$row2."Host" = $results.Count - 1


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Thank you very much LucD. That worked 🙂

0 Kudos