- Product Name
- Version
- Edition
- Host Name
- Number of Cores
- Number of Physical Processors
The script:
function Get-VMHostAudit
{
param([parameter(mandatory=$true)][string]$vCenter,
[parameter(mandatory=$true)][string]$Username,
[parameter(mandatory=$true)][string]$Password,
[parameter(mandatory=$true)][ValidateSet("List","Table")]$Format)
Connect-VIServer -Server $vCenter -User $Username -Password $Password
$ServiceInstance = Get-View ServiceInstance
$LicenseManager = Get-View $ServiceInstance.Content.LicenseManager
$LicenseManagerAssign = Get-View $LicenseManager.LicenseAssignmentManager
$VMhosts=Get-VMHost
$VMhostsTotal=@()
Foreach($VMhost in $VMHosts)
{
$VMHostView=Get-VMHost -Name $VMHost.name | Get-View
$VMhostID=$VMHostView.Config.Host.Value
$VMHostLM=$LicenseManagerAssign.QueryAssignedLicenses($VMhostID)
$VMhostsTotal+=$VMHostView | Select Name,
@{n='Product';e={$_.Config.Product.Name}},
@{n='Version';e={$_.Config.Product.Version}},
@{n='Sockets';e={$_.Hardware.CpuInfo.NumCpuPackages}},
@{n='CPUCores';e={$_.Hardware.CpuInfo.NumCpuCores}},
@{n='LicenseVersion';e={$VMHostLM.AssignedLicense.Name | Select -Unique}},
@{n='LicenseKey';e={$VMHostLM.AssignedLicense.LicenseKey | Select -Unique}}
}
If ($Format -eq "List"){$VMhostsTotal | Fl}
If ($Format -eq "Table"){$VMhostsTotal | Ft -AutoSize}
Disconnect-VIServer -Server $vCenter -Confirm:$False -Force
} # End Function
How to run it:
Just run the script to load the function into your shell and then execute the function. Heres an example:
C:\Scripts\PowerCLI\VMHostAudit.ps1
Get-VMHostAudit -vCenter 192.168.0.1 -Username ITMonsterrrrrr -Password Cr@CkTh!s -Format Table
As I mentioned earlier, add the function to your profile also, then you will always have it when you want to run it. Here is my favorite resource for Powershell profiles. Here is an example of the output, you can choose from either listed form or table form, just hit tab after the format switch to cycle through your options.
Name : 192.168.25.41
Product : VMware ESXi
Version : 5.0.0
Sockets : 1
CPUCores : 4
LicenseVersion : VMware vSphere 5 Enterprise Plus
LicenseKey : 00000-00000-00000-00000-00000
Name : 192.168.25.42
Product : VMware ESXi
Version : 5.0.0
Sockets : 1
CPUCores : 4
LicenseVersion : VMware vSphere 5 Enterprise Plus
LicenseKey : 00000-00000-00000-00000-00000
Name Product Version Sockets CPUCores LicenseVersion LicenseKey
---- ------- ------- ------- -------- -------------- ----------
192.168.25.41 VMware ESXi 5.0.0 1 4 VMware vSphere 5 Enterprise Plus 00000-00000-00000-00000-00000
192.168.25.42 VMware ESXi 5.0.0 1 4 VMware vSphere 5 Enterprise Plus 00000-00000-00000-00000-00000
Name : 192.168.25.41
Product : VMware ESXi
Version : 5.0.0
Sockets : 1
CPUCores : 4
LicenseVersion : VMware vSphere 5 Enterprise Plus
LicenseKey : 00000-00000-00000-00000-00000
Name : 192.168.25.42
Product : VMware ESXi
Version : 5.0.0
Sockets : 1
CPUCores : 4
LicenseVersion : VMware vSphere 5 Enterprise Plus
LicenseKey : 00000-00000-00000-00000-00000
Name Product Version Sockets CPUCores LicenseVersion LicenseKey
---- ------- ------- ------- -------- -------------- ----------
192.168.25.41 VMware ESXi 5.0.0 1 4 VMware vSphere 5 Enterprise Plus 00000-00000-00000-00000-00000
192.168.25.42 VMware ESXi 5.0.0 1 4 VMware vSphere 5 Enterprise Plus 00000-00000-00000-00000-00000
How it works:
After reading the passed parameters of $vCenter, $Username, $Password and $Format, the script connects to vCenter. We grab a view of the vCenter instance its self, we then request a license manager view for the vCenter instance. After we have stored the license manager assignment view for the instance in the $LicenseManagerAssign variable we then grab all of the ESXi hosts for the instance in the $VMHost variable. Then we begin to cycle to through each host and do the following: We grab a view of the host, and assign its host ID to the $VMHostID variable which will be used in a second. We then query the License Manager for the assigned licenses for the ESXi host matching that $VMHostID. With that information stored in the $VMHostLM (licenses manager) variable, we then just compile all of the desired data. We pick up host name, product name, version, cpu cores, and sockets from the $VMhostView variable and add in the license and key by using calculated expressions to query the $VMHostLM variable. We actually add this data to the $VMHostsTotal array, which by doing this will allow all of the hosts to put their data into that array and presented at one time, instead of having a table for each host, we essentially create 1 table by adding each hosts data to that array. After it determines if you defined a list or table to be used, it spews the data and disconnects from vCenter! Thats all she wrote! You could easily add in functionality to export to a CSV or HTML, although with this information I dont see an HTML output being very handy, but I do see a lot of value for using CSV to archive this information. If you need help editing the code to add more functionality, just let me know!
Syntax View:
I use Powershell and Powershell ISE 3.0.