Number of Physical & Virtual CPU’s per host & Cluster
Recently I received a comment on an older post of mine which gave output of the number of virtual CPU’s on a host and also in a cluster, the comment asked if there was a way to export this to a text or html file which is easy enough in PowerShell.
After reading my code I was astounded at how bad it was, one thing i wish I had time for was to go back to some of my earlier code to tidy it up with the tips and tricks I have learnt over the last year and also amend it to use the new features provided to us by the PowerCLI team to make things easier.
One such feature is a cmdlet in the current release of PowerCLI (4.1 U1) (If your not sure what version you are running then just type Get-PowerCLIVersion at the prompt).
The cmdlet I am referring to is New-VIProperty, this cmdlet allows us to add additional properties to the returned values from certain objects, for a detailed post on this cmdlet look at Luc’s great post here I can also highly recommend his list of New-VIProperty examples which is amazing and can be found here.
Basically this allows us to expand our returned objects from cmdlets to give us further information for an easy example if we run Get-VM we will receive a number of different default properties but what if we wanted something that wasn’t on the list of returned properties ? Normally we would need to use a PowerShell expression to add this data when we select the data:
Get-VM | Select Name, @{Name="ToolsVersion";Expression={$_.ExtensionData.config.tools.ToolsVersion}}
Whilst this works there are a couple of reasons I don’t like to use it, firstly its hard to read, when teaching people about this its always a hard one to explain as it doesn’t look as English as the rest of PowerShell and I make a big deal of telling them how PowerShell is so easy to learn as it almost reads as English.
Secondly, Imagine if you have lots of code in your expression, this can start getting complicated and doesn’t really belong in the main part of your script as far as i am concerned.
So how would this look with New-VIProperty ?
New-VIProperty -Name ToolsVersion -ObjectType VirtualMachine `
-ValueFromExtensionProperty ‘config.tools.ToolsVersion’ `
-Force
Get-VM | Select Name, ToolsVersion
Now doesn’t this look nicer, basically we are defining the property before we run the Get-VM and therefore all the issues I mentioned above are now resolved.
Anyway, back to my original reason for writing this post – I took the original script and re-wrote it in this new format which makes it much easier to work with and is much nicer to view, below is the new code:
New-VIProperty -ObjectType VMHost -name NumvCPUs `
-Value {
$TotalvCPU = 0
$Args[0] | Get-VM | Foreach {
$TotalvCPU += $_.NumCPU
}
$TotalvCPU
} -Force
New-VIProperty -ObjectType VMHost -name NumPoweredOnvCPUs `
-Value {
$TotalvCPU = 0
$Args[0] | Get-VM | Where { $_.PowerState -eq "PoweredOn" } | Foreach {
$TotalvCPU += $_.NumCPU
}
$TotalvCPU
} -Force
New-VIProperty -Name NumvCPUs -ObjectType Cluster `
-Value {
$TotalvCPU = 0
$Args[0] | Get-VMHost | Foreach {
$TotalvCPU += $_.NumvCPUs
}
$TotalvCPU
} `
-Force
New-VIProperty -ObjectType Cluster -name NumPoweredOnvCPUs `
-Value {
$TotalvCPU = 0
$Args[0] | Get-VMHost | Foreach {
$TotalvCPU += $_.NumPoweredOnvCPUs
}
$TotalvCPU
} -Force
New-VIProperty -Name NumCPU -ObjectType Cluster `
-Value {
$TotalPCPU = 0
$Args[0] | Get-VMHost | Foreach {
$TotalPCPU += $_.NumCPU
}
$TotalPCPU
} `
-Force
Get-VMHost | Select Name, NumCPU, NumvCPUs, NumPoweredOnvCPUs
Get-Cluster | Select Name, NumCPU, NumvCPUs, NumPoweredOnvCPUs
Which gives us a nice output of:
Or this can now easily be exported to a text file using Out-File or a HTML file by changing the following line:
Get-VMHost | Select Name, NumCPU, NumvCPUs, NumPoweredOnvCPUs | ConvertTo-Html | Out-File c:\Tmp\HostvCPUs.html
Failover of persistent desktops using SRM and View 4.6 PowerShell or PowerCLI Fanboy ?











Im getting an error on line 2 char 2:
Missing expression after unary operator ‘-’.
At :line:2 char:2
+ -V <<<< alue {
Did the code copy ok? Looks like a copy and paste issue?
Possibly, do you mind saving this as a .ps1 or .txt and I can give it another shot?
Sure thing, drop me an email on ajw.renouf at gmail.com and I will send it over.
Script works like a charm…. just had an issue with the copy. Thanks a lot!
hi would you please send me acopy of your code?