Hello folks,
Could I ask yours assist in my problem with scripting?
Goal: to count number of physical CPUs of ESXi hosts where Windows VMs are running. To do it across several vCenter servers.
What I reach myself:
** all hosts in vcenter in connected state
$mgmt_hosts = Get-VMHost | Where-Object {$_.ConnectionState -eq "Connected"} | select Name
** all VMs in powered on state with Windows OS
Get-VM |Where-object{$_.powerstate -eq "PoweredOn"}| Where-Object {$_.guest -match "Windows"} | select name, powerstate
Now I have array of hosts($mgmt_hosts) and gona push the 2nd there to get apropriate VMs:
foreach($mgmt_host in $mgmt_hosts){Get-VM -Location $mgmt_host |Where-object{$_.powerstate -eq "PoweredOn"}| Where-Objec
t {$_.guest -match "Windows"} | select name, powerstate}
here I tried to compare -Location property of VM with ESXi hostname, but afterwards get an error:
Get-VM : Cannot bind parameter 'Location'. Cannot convert the "" value of type "System.Management.Automation.PSCustomObject" to type "VMwar
e.VimAutomation.ViCore.Types.V1.Inventory.VIContainer".
At line:1 char:52
+ foreach($mgmt_host in $mgmt_hosts){Get-VM -Location <<<< $mgmt_host |Where-object{$_.powerstate -eq "PoweredOn"}| Where-Object {$_.guest
-match "Windows"} | select name, powerstate}
+ CategoryInfo : InvalidArgument: (:) [Get-VM], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM
Guys, how to compare VMs location property with each host? manythanks!
The Location parameter on the Get-VM needs to be a VIContainer.
Your script is passing the objects that are returned by the Select-Object cmdlet, which are not VIContainers.
Do something like this (leave out the Select in the first line)
$mgmt_hosts = Get-VMHost | Where-Object {$_.ConnectionState -eq "Connected"}
foreach($mgmt_host in $mgmt_hosts){Get-VM -Location $mgmt_host |Where-object{$_.powerstate -eq "PoweredOn"}| Where-Objec
t {$_.guest -match "Windows"} | select name, powerstate}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks
How can I count the number of physical sockets per ESXi host?
$host.NumCpu property returns number of logical CPUs but not real CPUs.
I tried this
PowerCLI C:\> foreach($used_host in $used_hosts) {Get-VMHost | %{Get-View $_.ID} | % { $cpus += $_.summary.hardware.numcpupkgs} }
but indeed it fetchs the number of logic CPUs
The numCpuPkgs property in the HostHardwareSummary object
gives the "number of physical CPU packages". What makes you think these are logical CPUs ?
Btw you can fetch that value also like this
Get-VMHost | Select Name,@{N="#CPU";E={$_.ExtensionData.Summary.Hardware.numCpuPkgs}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Very helpfull
but go further and faced with another prob.
$win_hosts = foreach($used_host in $used_hosts){
Get-VMHost $used_host.Name | Select Name,@{N="#CPU";E={$_.ExtensionData.Summary.Hardware.numCpuPkgs}}
}
$win_hosts variable now have consists of list of hostnames and #CPUs
but in what way can I count now the total summation of these Sockets? how to call the property of $win_hosts variable which include those #CPU variables?
$counterofCpuPckgs=0
$win_hosts = foreach($used_host in $used_hosts){
$counterofCpuPckgs+=$used_host.ExtensionData.Summary.Hardware.numCpuPkgsGet-VMHost $used_host.Name | Select Name,@{N="#CPU";E={$_.ExtensionData.Summary.Hardware.numCpuPkgs}}
}
"Total number of CpuPkgs is $counterofCpuPckgs"
Huh, guys, thanks gor your assistance, but I'm sure I have a mistake somewhere. Here is the full script:
# Connect-VIServer n.n.n.n
$win_hosts=0;
$win_host=0;
$used_hosts=0;
$used_host=0;
$num_cpus = 0;
$used_hosts = Get-VMHost | Where-Object {$_.ConnectionState -eq "Connected"} | select Name;
$win_hosts=foreach($used_host in $used_hosts){
Get-VM -Location $used_host.name |Where-object{$_.powerstate -eq "PoweredOn "}| Where-Object{$_.guest -match "Windows"} |
Get-VMHost $used_host.Name |
Select Name,@{N="#CPU";E={$_.ExtensionData.Summary.Hardware.numCpuPkgs}}
$num_cpus+=$used_host.ExtensionData.Summary.Hardware.numCpuPkgs;
};
$num_cpus in the end is equal zero. Smthng gone wrong...
I'm not sure what you are trying to do, but try this variation
# Connect-VIServer n.n.n.n
$win_hosts=0
$win_host=0
$used_hosts=0
$used_host=0
$num_cpus = 0$used_hosts = Get-VMHost | Where-Object {$_.ConnectionState -eq "Connected"}
foreach($used_host in $used_hosts){
$num_cpus+= $used_host.ExtensionData.Summary.Hardware.numCpuPkgs
}
That should at least populate $num_cpus.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference