Gathering VMWare Metrics

Anytime that you a large environment, gathering metrics in any form can be daunting and cumbersome.  Using scripting languages can usually greatly improve the efficiency of this process as well as be more accurate.  Whether it’s VMWare, SQL Server, or some other platform, it would behoove you to see what scripting languages it may support.

In a recent case, I had to gather some metrics from a VMWare implementation.  The environment had multiple hosts and each host had multiple virtual machines within them. I needed to gather metadata about each virtual machine on certain hosts.  I was looking data like:

  • Number of CPUs
  • How much memory is allocated
  • What is the operating system version
  • What virtual host is the VM on

Thankfully VMWare has its own flavor of Powershell, called PowerCLI, which can be used to gather all of the metrics easily and quickly.  You can find the documentation on PowerCLI here.  When looking into this scripting language keep in mind that different versions of VMWare will have different dialects for PowerCLI.  If you find that a particular cmdlet isn’t working, check the documentation to see if it is available for your particular version of ESX.

The Script

To start with, I’ll need credentials in order to connect to the vcenter server. From that vantage point, I can then jump into the pool of VMware resources.  Security is an utmost concern for me always, so getting the appropriate credentials securely is the way to go.  Thank fully, we can use the get-credential cmdlet which will secure my user name and password into a credential object.

#get credentials safely
$pass = get-credential

Getting the credentials in this manner allows me to not have to hard code anything into the script, namely my password.  This cmdlet will prompt you in a separate dialog window to enter your user id as well as your password.  The credential object can then be passed into the connect-viserver cmdlet by using the -credential switch.

#connet to the vcenter server
connect-viserver -server server1.domain.com -credential $pass

In the above cmdlet, we also pass in the -server value which tells the cmdlet which server I want to initiate a connection to.  Next, we can get a list of all virtual machines that reside on a given host, namely host1 and host2.  The host name isn’t a string by default so in order to make a comparison, I had to use the ToString() method.  Note that there might be a better alternative to this, but I needed the script quickly, so I didn’t waste a lot of time researching options.

#get all of the vm's that we are interested in which in this case reside on a particular host(s).
$vms = get-vm | ? {$_.VMHost.tostring() -eq "host1.domain.com" -or $_.VMHost.tostring() -eq "host2.domain.com"}
The $vms variable will be populated with a list of all of the virtual machines.  Now I can use a foreach loop and iterate through them and get their properties. I also wanted the ability to export the data into Excel where I could make it look prettier. In order to do this, I had to install the ImportExcel module.
#Note: the ImportExcel module is needed: 
install-module -name ImportExcel
foreach ($vm in $vms){
#get VM properties
$vm | select * | Export-excel -worksheetname "Virtual Machines" -path c:\temp\host1.xlsx -Append

The other set of information that I wanted was to make sure that the virtual machines were using the appropriate SCSI controllers.  For non-operating system drives, they should be using the paravirtual drivers.  We can use PowerCLI to get that information as well.  This will finish out the ForEach loop.

#get SCSI Controller Information
$vm | Get-scsicontroller | export-excel -worksheetname "SCSI Controllers" -path c:\temp\host1.xlsx -Append
}

Notice that in both export statements I included the Append switch.  If you do not include that, the process will over-write the entire file and the only thing that it would contain is the last virtual machine that the script touched.  This doesn’t do any good so we must append to the file.

You can see the entire script in the DCAC Github Repository.

Summary

This was a quick PowerCLI script that I put together to fetch as many metrics about each virtual machine from the ESX hosts.  It’s quick and short script and it can certainly be improved upon, such as adding in variables to make it cleaner.  Make sure if you make improvements, do a pull request on GitHub so we can incorporate your improvements!

© 2019, John Morehouse. All rights reserved.

Hey you! Leave me a comment and start a discussion!

This site uses Akismet to reduce spam. Learn how your comment data is processed.