Tag Archive: VMware

Aug 03 2011

PowerShell Hashtables

I was poking around in PowerCLI (Powershell for VMware) trying to identify all hosts with additional information like, Memory Allocation, Cluster Ownership and Storage Assignment. This information is somewhat easy to gather normally, but I found a useful little piece of code which makes it all that more compact, yet still simple enough to read.

This code uses what is known as “hash tables” to enclose another piece of code inside the primary code block. In this example above, I use the ‘get-vmhost’ to get all the VM’s attached to vCenter, then pull some information bound to each primary VM such as which cluster they were from, the total amount of memory for each host and an array of the datastores that were assigned to each.

What this command does, it uses the ‘get-vmhost’ to gather each host, then pipe this information to the standard select statement to gather selective information. In this case, the NAME of the host is the first information, but then we see @ symbol and then {N=”Cluster”;E={get-cluster -VMHost $_}}. This creates a new Column to be displayed with the Title “Cluster”, as displayed after the N=. The E= is the command that is run, which in this case is the ‘get-cluster…’ The $_ after the -VMHost indicates that we want to use the already stored value before the pipe (|) in the current loop cycle.

The resulting output you will recieve from this piece of code is as follow:

In some instances you will find that the output it too large to fit in one line of output. In this instance, you can output the result to a .CSV file. This will give you the ability to open the file in excel (or another spreadsheet application) with all the information visible.

This piece of code will¬†definitely¬†help in reducing the amount of code you type, and doesn’t take that long to learn how to use it effectively.

You can find more information on splatting by googling “powershell hashtables”.

» Newer posts

Fetch more items