Use PowerCLI to get Quick Stats (Part 1)

by [Published on 30 Sept. 2010 / Last Updated on 30 Sept. 2010]

How to use basic PowerCLI commands to pull information from an ESX host.

If you would like to read the next part in this article series please go to Use PowerCLI to get Quick Stats (Part 2).

Introduction

A while back, I started to do a little bit of work with PowerCLI, VMware’s PowerShell based scripting language. PowerCLI can be used for a number of tasks, including creating, managing and monitoring virtual hosts and guests. In this article, part one of a new series, you will learn how to use basic PowerCLI commands to pull information from an ESX host.

Sometimes you just want to get an at-a-glance view of your VMware environment. Other times, you may want to store historical information outside vCenter/ESX in order to manipulate it in other tools. With PowerCLI, getting some down and dirty statistics is quite simple. PowerShell also provides database connectivity cmdlets that enable you to write statistics information into a SQL Server database. Later in this series, I’m going to show you how to grab pertinent information from an ESX server and also show you how you can write some of this information into a database for later viewing.

In part one – this part – we’ll be focusing on pulling general ESX server information.

Step 1: Obtain PowerCLI

The first order of business is to make sure that you have PowerShell installed on your computer. If you’re running Windows 7 on your desktop, you should be all set. If you’re running an older version of Windows, use your friend Google to discover how to meet this initial prerequisite.

Next, you need to get PowerCLI, which is available for download from VMware. PowerCLI is a snap-in that works with PowerShell and adds more than 200 commands to this powerful scripting language. During the installation of PowerShell, you may receive a notification indicating that other prerequisites will be installed at the same time.

Step 2: Change Default Script Execution Policy

Once you have the tool installed, start a PowerCLI command prompt by going to Start > All Programs > VMware > VMware vSphere PowerCLI. To start PowerCLI as an administrative user, right-click the VMware vSphere PowerCLI option and, from the shortcut menu, choose Run as administrator.

The first time you run PowerCLI, you may receive an error message like the one shown in Figure 1. This message indicated that the script execution policy on this system is set to a restricted state. To change the code execution policy to a state that allows scripts to run. Accomplish this task by typing the following command in at the PowerCLI command prompt.

set-executionpolicy remotesigned

This command configured PowerCLI to run local scripts without hassling you but requires that scripts downloaded from the Internet be signed by a trusted publisher. If you don’t execute this command, you will get errors in PowerCLI that can affect script execution.


Figure 1: PowerShell remote execution policy

Step 3: Connect to an ESX host

For the examples you’ll see here, vCenter isn’t required. You can connect directly to an ESX host against which you can run a wide variety of cmdlets (the PowerShell term for commands). The cmdlet that initiates a connection to your ESX host is Connect-VIServer.

When using this Connect-VIServer cmdlet, you need to specify the name or IP address of your vSphere server, the connections protocol – HTTP or HTTPS – you wish to use and the username and password for a user with enough rights to poll the server for statistics. For my lab server, I use the following command:

Connect-VIServer -Server 192.168.200.1 -Protocol https -User root -Password passwordgoeshere

If the connection is successful, you will be presented with information about the connection.


Figure 2: The connection was successful

Step 4: Get started with the basics

Before we forge ahead, make sure you understand how to get help in PowerCLI. Specifically, to get help with command syntax, type commandname -?. Doing so will provide you with information about the command. You can also use the get-help command to get more detailed information. Use as follows:

  • get-help commandname -examples. Sometimes, the quickest way to learn command syntax is to see it in action.
  • Get-help commandname -detailed.  Get additional information about the command, including parameter descriptions and examples.
  • Get-help commandname -full.  Displays all of the available help for a command, including technical information about its parameters

With the basics of getting help under your belt, let’s take the next step and explore a few basic PowerCLI commands that you can use to extract high level information from your ESX server and virtual machines.

Get information about the host itself

Starting at a higher level, let’s take a look at the physical component – the ESX host itself – before we dive into examining the virtual tier. PowerCLI provides the get-vmhost command that enables you to gather detailed information about the host, including some critically important usage statistics.

To use this command, simply type get-vmhost at the PowerCLI command line. You’ll get a result like the one shown below in Figure 3.


Figure 3: The default results of get-vmhost

In looking at Figure 3, you might see that, although there is some useful information there, it looks like a lot of it is lost. You can see the current system power state, CPU usage and maximum and currently used RAM and the total RAM in the host. However, you can’t see a whole lot beyond what is visible. The results view you see in Figure 3 is PowerShell’s default table formatted output.

You can unlock a whole lot more through the use of the format-list PowerShell command, which formats all of the output in list form. To use format-list, generally abbreviated “fl” in PowerShell, use the pipe character “|” to pipe the results of get-vmhost to the format-list tool. In Figure 4, I’ve execute the command get-vmhost | fl and you can see that the results are drastically different and much friendlier.


Figure 4: List formatted output

As I mentioned, there’s some good information here that can help you get a handle on how your ESX server is operating. All of the pertinent details of the physical environment are listed, including the server make and model, CPU type, number of CPU cores, total CPU capacity, total RAM, currently in use CPU and RAM, and the version of ESX/ESXi that is installed.

Here’s another way that you can use the get-vmhost command to get even more detailed information about the ESX/ESXi product installation:

Get-VMHost | foreach-object {(Get-View $_.ID).Config.Product }

A little bit of explanation is in order. Here’s how this command breaks down:

  • Get-vmhost - You already know the purpose of this command.
  • Pipe symbol (|) - Tells PowerShell to run the results of the previous command – in this case, Get-VMhost – through the next part of the process.
  • Foreach-object - Consider what would need to happen if you were to run a command against an entity for which there are multiple results. Perhaps the get-vmhost command returns more than one ESX server, for example. The foreach-object PowerShell command tells PowerShell to run the succeeding command (in this case, the one that starts with Get-View) for each object returned by the preceding command.
  • Get-view - We’ll be working with this command much more in the next part of this series. Get-view returns the vSphere .Net view objects that correspond to the specified search criteria. The cmdlet retrieves the vSphere .NET view objects specified by their IDs or by their corresponding vSphere inventory objects.
  • $_.ID -The $ portion of this part of the command tells PowerShell to work against the “currently selected object”. This basically means that you don’t need to try to figure out ahead of time the exact name for all of your ESX servers and use those specific names in this command,. The dollar sign allows you to generalize and get the same information more easily, making the script a whole lot more flexible. This is called a pipeline variable.
  • Config.product - We’ll talk more about this in the next part of this series. For now, understand that this is actually the aforementioned search criteria that tells PowerCLI exactly what information is to be culled from the ESX host.

Get a List of Virtual Machines on the Host

You just got a look at how to get information about your host, so let’s move on to the next layer: the virtual machines that are hosted on the physical ESX server we just looked at. This is where the get-vm command comes in to play. The get-vm command allows you to obtain a list of virtual machines running on the server. As was the case with the get-vmhost command, use the “fl” tool to get more detailed information about each virtual machine. You can see in the next two figures that I havethree virtual machines running on this host right now and two of them are in a powered on state. You are also provided with the name of the virtual machine, the number of assigned virtual CPUs and the amount of RAM that has been dedicated to the VM.


Figure 5: Output of the get-vm command in table form


Figure 6: Output of the get-vm command in list form

Obtain Basic Guest Details

The next cmdlet that I’ll brief you on in this article is get-vmguest. This cmdlet requires that you specify the name of a virtual machine. Once you do so, the command returns some state information about that particular virtual machine including the current operating system, run state, host name and screen dimensions. You can see in Figure 4 that I’ve used the command get-vmguest Win7-USB | fl to return information about the virtual machine name Win7-USB.


Figure 7: The results of get-vmguest Win7-USB | fl

Get Some Resource Information – Different Object Types

Although we’ll be discussing the gathering or metrics in depth in part two of this series, I wanted to take this opportunity to show you some of the ways by which you can get general resource information for a virtual machine.

Get-harddisk, as the name implies, is a command in PowerShell that pulls storage information from different levels, depending on the object that you provide to the command.

Figure 8 shows you the results of get-harddisk win7-usb | fl. Win7-USB is one of my virtual machines.


Figure 8: The results of get-harddisk against a single virtual machine

Now, let’s take a look at the same command, but this time, we’ll use the -datastore parameter and specify the name of a data store housed on the ESX host. In this case, the data store is named datastore1. Hence, I’ll use the command get-harddisk -datastore datastore1. Figure 9 gives you a look at the results of this command in table form. Figure 10 shows you the output in list form.


Figure 9: ESX server storage information - table form


Figure 10: ESX storage information - list form

As you can see, by focusing on the ESX server rather than a single virtual machine, the output scope is much broader and it becomes very clear that this particular PowerCLI command, as is the case with many others, can be used to meet a variety of data gathering needs.

Summary

In this first part of this new series, you learned the basics behind using PowerCLI to gather information from a single ESX host. In part 2, you’ll learn how to gather actual performance metrics from an ESX server.

If you would like to read the next part in this article series please go to Use PowerCLI to get Quick Stats (Part 2).

Featured Links