Tuesday, October 23, 2012

PowerCLI: ESXi Host Audit

I try to keep up with the VMware communities/message boards for PowerCLI related questions and saw one that I thought was interesting. Someone was interested in creating a script that could preform an audit of their ESXi hosts in inventory. He wanted the script to include the following:
  • Product Name
  • Version
  • Edition
  • Host Name
  • Number of Cores
  • Number of Physical Processors
So Mr. Buzzlightyear... if that is your real name, here it is, I know I am little late, but I think my script is a little easier to understand and use. In reality I turned the script actually into a function called: Get-VMHostAudit. Run this script, or add it to your PowershellCli profile and off you are to the races.

The script:

function Get-VMHostAudit

Connect-VIServer -Server $vCenter -User $Username -Password $Password
$ServiceInstance = Get-View ServiceInstance
$LicenseManager = Get-View $ServiceInstance.Content.LicenseManager
$LicenseManagerAssign = Get-View $LicenseManager.LicenseAssignmentManager
Foreach($VMhost in $VMHosts)
    $VMHostView=Get-VMHost -Name $VMHost.name | Get-View
    $VMhostsTotal+=$VMHostView | Select Name,
        @{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:


Get-VMHostAudit -vCenter -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           :
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        : 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                   
----          -------     ------- ------- -------- --------------                   ----------           VMware ESXi 5.0.0         1        4 VMware vSphere 5 Enterprise Plus 00000-00000-00000-00000-00000 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.


  1. Hi IT Monster, but you do realize that my code was not intended to do what your code is doing right ? It does completely different thing than yours when you are fetching license for a single esx. And that was just an idea for "Buzzlightyear" so he can adjust it however he wants.Again you can not do a full license report like in my post using your approach. You do not need to feel baffled, you might ask why somebody does it like that, and you will know that this is the only way to fetch those information.
    Anyway ;) Nice function, nice post, nice blog. Have a nice one

    1. The baffled part was just a funny way to use what I thought was a goofy picture ;) There is nothing wrong with your code, and actually I usually do a lot with HTML outputs for Powershell scripts. Giving ideas for code and helping people look in the right areas is what the Powershell community is all about. I read through your code and thought it was really well done, I had some free time and thought I would narrow a script down for Buzzlightyear to get him data more specific for the ESXi hosts. My post is a misleading, and I should fix that! Thanks for your comment, I favorited your blog and hopefully you and I could share some cool ideas back and forth!Have a good one psvmware!

  2. No worries Jeff. I like the picture too ;) One quick thought about your script. It would be way flexible if you would just return objects by default . If you would do so, it would be possible to pipe it to export-csv easily, and nothing needs to be modified in order to make a csv from that. Have in mind that whatever goes through format-list, format-table , will be not understood by export-csv cmdlet. Those switches are cool that you are giving option to make a ft or fl. So if you would by default return $VMhostsTotal, it would be easy to make csv right away.
    Please don't feel like i was pushing you to change text here ;) I accept all comments about my code, my blog etc.. you don't need to change text in your blog ;)
    Regards Greg

    1. The text didnt come off to readers like it sounded in my head. No push from you, you just helped me realize it didnt come off the way I wanted. Thanks for tips on the function, very good ideas. Piping it into a CSV would be a quick alteration and a super good idea. Thanks! Have a good one Greg!

  3. The text didnt come off to readers adore it sounded in my head. No push from you, you simply helped me recognize it didnt come off the way I desired. watch online movies
    Thanks for pointers on the operate, excellent strategies. Piping it into a CSV would be a quick alteration and a perfect good inspiration. Thanks! Have a just right one Greg!