Use PowerCLI to get Quick Stats

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

Use PowerCLI to Get Quick Stats (Part 2)

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.

If You Skipped Part 1…

There are two critical steps to complete before you get started.

  • Make sure you configure PowerShell to allow remote script execution. set-executionpolicyremotesigned
  • Connect to your vCenter server or to an individual ESX/ESXi host. Connect-VIServer -Server 192.168.200.1 -Protocol https -User root -Password passwordgoeshere

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
DatastoreIDList
Description
DrsAutomationLevel Displays the DRS automation level for the virtual machine. Valid values are valiFullyAutomated, Manual, PartiallyAutomated.
ExtensionData 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.
ResourcePoolId The ID associated with the resource pool.
Uid Another unique ID associated with the virtual machine. My system: /VIServer=root@192.168.200.1:443/VirtualMachine=VirtualMachine-16/
UsbDevices Gets a list of USB devices attached to the virtual machine
UsedSpaceGB Amount of space actually being used by the virtual machine
VApp VApps associated with the virtual machine
Version Returns the virtual machine hardware version. My virtual machine is at version 7 hardware.
VMhost Returns the same information as the Host parameter
VMHostID Same as HostID.
VMResourceConfiguration Displays resource allocation information associated with the virtual machine, including CPU affinity, CPU limits and more.
VMSwapfilePolicy 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.

by  www.virtualizationadmin.com

Advertisements

Trả lời

Please log in using one of these methods to post your comment:

WordPress.com Logo

Bạn đang bình luận bằng tài khoản WordPress.com Đăng xuất / Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Đăng xuất / Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Đăng xuất / Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Đăng xuất / Thay đổi )

Connecting to %s