Introduction

In part one of this series, you learned how to use PowerCLI to gather basic information about your ESX hosts and virtual machines running on those hosts. That was the first step on a journey toward gathering quick statistical information from your virtual environment. In this part of the series, we’ll dig a little deeper into the process.

You already know that you need to understand your hardware before you can begin to evaluate associated metrics. After all, a value of “30 IOPS” means nothing without some context. Is that 30 IOPS of performance between an ESX host and a storage array or is that 30 IOPS between a virtual machine and a storage array? Is that 30 IOPS being generated from a specific virtual device or is it an aggregate value across all virtual hard disks in a virtual machine?

A tip

Before you get too deep into your PowerCli adventures, look for a really good PowerShell GUI tool. I’ve been using a trial of Idera’s PowerShell Plus tool and have found it to be fantastic. I’ve also used Quest Software’s free version of PowerGUI for some scripting tasks in the past and have also found it to be a good tool. I particularly like Idera’s tool, however, because it makes it really easy for casual PowerShell developers to drill down into what can be complex data structures. Figure 1 gives you a look at PowerShell Plus.

Figure 1: Idera PowerShell Plus

You’ll find that using a tool like this makes life a whole lot easier particularly since many of these tools have features that truly streamline the script development process. I’m not going to spend much time discussing these GUI-based tools in this article except to make you aware of their existence.

Virtual Machine Details

We’ll start by discovering how to gather additional details about a running virtual machine. In order to make this a bit easier, we’ll actually assign the results of the get-vm command, which we discussed in part 1 of this series, to a variable that we can then manipulate to get the information we want. By learning how to, for example, list the hard disks associated with a virtual machine, you’ll be able to get better statistics down the line.

To assign all of a single virtual machine’s properties to a variable, use a syntax like the following:

$v = get-vm win7-usb

This will assign all of the results from the get-vm win7-usb command to the newly created variable named $v. Ideally, in the real world, you’ll use more descriptive variable names, but to keep things a little clearer for demonstration purposes, I’m using shorter names.

Tip:This is a “point in time” snapshot of how the virtual machine looked at the time its information was committed to the variable named $v. If you make configuration changes later on, you need to run the same command again to update the variable’s contents.

Now, let’s get a little bit of information about what’s been assigned to this variable. In order to do so, let’s start with something basic. Simply execute the command $v with no parameters. You will get something similar to the result shown in Figure 2.

Figure 2: A high level look at the VM

You will note that this is the exact same output you’d get if you were to execute get-vm win7-usb from the command line instead. This validates that we are looking at information for the right virtual machine.

While this is all well and good, it’s not that interesting. Let’s take a look at a few variations that provide more information about the virtual machine.

$v.NetworkAdapters

This command displays the network adapters that are currently installed in the virtual machine along with the network adapter name, type, virtual network name, MAC address and whether or not Wake On LAN is enabled.

Figure 3: Network adapter information

What you are really doing with commands like this is accessing properties (NetworkAdapters) for the object that you assigned to the variable ($v).

Table A: Virtual machine object properties

CDDrives

A list of the CD/DVD devices connected to the virtual machine

CustomFields

A list of any custom fields that have been created for the virtual machine

Allows you to access deeper details for a virtual machine (From VMware PowerCLI 4.1 docs: Exposing the View object that corresponds to an object returned by a PowerCLI cmdlet through the object's ExtensionData property)

FloppyDrives

Details about the floppy drive connected to the virtual machine

Folder

Displays the name of the folder in which the VM resides

FolderID

Displays the folder ID for the above

Guest

When used by itself (i.e. $v.guest | fl), returns a full list of guest virtual machine parameters such as the name of the operating system, the guest machine’s IP address, list of assigned disks, dimensions of the guest OS display and other information. (Figure 4)

HAIsolationResponse

Indicates whether the virtual machine should be powered off if a host determines that it is isolated from the rest of the computer resource

HardDisks

Returns a list of the hard drives you have assigned to the virtual machine.

HARestartPriority

Specify the HA restart priority of the new virtual machine. The valid values are Disabled, Low, Medium, High, and ClusterRestartPriority.

Host

Displays information about the host on which the virtual machine resides.

HostId

The ID of the host. In my example, this is HostSystem-ha-host.

Id

The internal ID of the virtual machine. For Win7-USB on my system, this is VirtualMachine-16.

MemoryMB

Returns the amount of memory allocated to the virtual machine

Name

Returns the “friendly name” of the virtual machine.

NetworkAdapters

You saw this one in action above the table. Displays the network adapters that are currently installed in the virtual machine along with the network adapter name, type, virtual network name, MAC address and whether or not Wake On LAN is enabled

Notes

Displays notes that you have associated with a particular virtual machine. In Figure 5, you can see the note that I have provided in the vSphere Client. In Figure 6 you see what happens when you pull this information and display it in PowerCLI.

NumCpu

Displays the number of virtual CPUs that have been assigned to the virtual machine.

PersistentId

A unique ID number associated with each virtual machine.

PowerState

Can be PoweredOff, PoweredOn or Suspended.

ProvisionedSpaceGB

Returns the amount of space allocated to the virtual machine.

ResourcePool

Displays the name of the resource pool in which the virtual machine exists. You can get a lot of additional information about the resource pool (i.e. get the CPU reservation value by using $v.ResourcePool.CPUReservationMhz. Figure 8 shows you a list of the available resource pool specifics.

Displays the swap file policy for the virtual machine. This can be WithVM, Inherit or InHostDatastore.

Table 1

Figure 4: Obtain both general and specific information about the guest operating system

Figure 5: Information about the host computer

Figure 6: Notes associated with this virtual machine

Figure 7: The Notes field as displayed in PowerGUI

Figure 8: Resource Pool details

Figure 9: Virtual machine resource configuration

Getting a Full Properties List

In the preceding section, you learned about how to grab specific information – properties – associated with a virtual machine that was captured into a variable ($v). You can get a full list of the properties associated with the object in this variable in a couple of different ways.

First, you could simply execute the command $v | fl, but this will still miss some information and there is actually a much better, more generic way.

Use the following command to pull a list of all properties associated with the kind of object polled by that command.

get-vm | get-member -MemberType property

You will get a screen like the one shown below n Figure 10.

Figure 10: All of the properties associated with the Get-VM command

The get-member –MemberType property part of this command is where the magic happens. You can pipe any command through the get-member cmdlet to get a list of the properties associated with that command. If you’d like to list Methods in addition to Properties for a particular command, you can omit the membertype parameter. Try it at home by executing get-vm | get-member.

In Figure 10, you can see that I’ve used the Get-Member cmdlet against Get-VMguest to obtain a list of methods and properties associated with that command.

Figure 11: Get-Member for Get-VMGuest

Digging a Little Deeper

Now, let’s go even a bit deeper into the virtual machine variable we have been working with. This particular virtual machine happens to have two hard drives assigned to it, as you can see in Figures 12 and 13.

Figure 12: Two hard drives are listed as being associated with this VM

Figure 13: Here is a full list of the hard drive parameters

This is good information as it tells you a lot about the storage associated with this virtual machine including the storage format (thick, thin), the disk size, and the disk name in the virtual machine. What if, however, you really only want to get information about one of the disks? As you can see from the examples above, you’re seeing information from both disks.

It’s not too hard! You can use the “Get” “method” associated with the hard drive resource type to pull information for a specific drive number. Microsoft defines a PowerShell method as “a set of instructions that specify a particular action you can take with that object.”

In this case, try out the following command:

$v.HardDisks.Get(1) | fl

This command will return the details for hard drive number 1 in the system, but be aware that the numbering for these kinds of objects starts at 0, so you’re really getting the details for the second hard drive in the system, as shown in Figure 14.

Figure 14: Details for the VM's second hard drive

There are other ways, such as the Get-HardDisk command, that you can use to get hard drive information, but the method I’ve shown above keeps everything in a single variable.

Summary

We covered quite a bit in this part of the series and we’re going to build on in the next part of the series.

The Author — Scott D. Lowe

Scott has written thousands of articles and blog posts and has authored or coauthored three books, including Microsoft Press’ Exchange Server 2007 Administrators Companion and O’Reilly’s Home Networking: The Missing Manual. In 2012, Scott was also awarded VMware's prestigious vExpert designation for his contributions to the virtualization community.

In this part of the article series, we will continue from part one and explain other items that are available on the Hardware tab, but our focus is geared primarily towards storage devices attached to a Hyper-V host... Read More

Taking Control of VM Sprawl (Part 6)

This article continues the discussion of VM sprawl control by looking at some techniques for extracting event log information using PowerShell... Read More