Posey's Tips & Tricks

Retrieving Detailed Information About Hyper-V VMs

There is actually quite a bit of VM info that is hidden from view, but that can be exposed through the Get-VM cmdlet if you know how. Brien walks you through the steps.

PowerShell makes it really easy to get information about the virtual machines (VMs) that are running on Microsoft's Hyper-V.

Entering the Get-VM cmdlet, for example, causes PowerShell to display a table showing the state, CPU usage, memory assignment, uptime, status and version of each of your VMs, as shown in Figure 1.

[Click on image for larger view.] Figure 1: The Get-VM cmdlet displays summary information for each VM.

If you need to see more detailed information about a specific VM, that, too, is no problem. All you have to do is specify the name of the VM and append the Select-Object cmdlet, along with an asterisk, which indicates that you want to see all of the available information for that VM. You can see an example of this in Figure 2.

[Click on image for larger view.] Figure 2: The Select-Object cmdlet can be used to display all of the VM attributes that the Get-VM cmdlet knows about.

So by using Get-VM along with Select-Object, we can display all of the attributes that Get-VM knows about. That's the end of the story, right? Not quite.

There is actually quite a bit of VM info that is hidden from view, but that can be exposed through the Get-VM cmdlet if you know how. And I'm not talking about obscure VM details, either. I am talking about really useful information such as the VM's IP address.

The interesting thing about the extended VM information that I am about to show you is that even though PowerShell does not readily expose this information, the Hyper-V Manager provides clues as to its existence. Let me show you what I mean.

If you open the Hyper-V Manager and select a VM, you will find that three tabs are displayed at the bottom of the console. These tabs provide a basic overview of the VM's status and health. You can see these tabs at the bottom of Figure 3. If you look closely at the figure, you will notice that the Hyper-V Manager displays the IP address that has been assigned to the VM, even though the Get-VM cmdlet does not normally include the guest's IP address within its output. So what is going on?

[Click on image for larger view.] Figure 3: The Hyper-V Manager displays a guest's IP address.

If you look back at Figure 2, you will notice that some of the properties that are displayed by the Get-VM cmdlet are enclosed in brackets, while others are not. The presence of brackets around a property indicates that there are a series of sub-properties that are not being displayed. You can display these sub-properties by modifying the Select-Object portion of the command.

As a reminder, the command that I used to list all of the properties shown in Figure 2 was:

Get-VM DC | Select-Object *

In the command above, DC was the name of my VM, and the * following the Select-Object command means that I want to see all of the available properties. If instead I wanted to look at some sub-properties for the VM, I would need only to know the name of the category of sub-properties. Looking back at Figure 2, I can see that there are brackets surrounding the properties for HardDrives, so that means that HardDrives is a category of sub-properties. Therefore, if I wanted to see a summary of the HardDrive properties, I could enter this command:

Get-VM DC | Select-Object -ExpandProperty HardDrives

You can see the output in Figure 4.

[Click on image for larger view.] Figure 4: Here are some of the hard drive related properties.

But what if I wanted to dig a little bit deeper? Well, believe it or not, you can use a second instance of the Select-Object cmdlet. By appending Select-Object * to the existing command, we can see all of the hard drive-related properties. Here is what the command looks like:

Get-VM DC | Select-Object -ExpandProperty HardDrives | Select-Object *

You can see the output in Figure 5.

[Click on image for larger view.] Figure 5: Here are all of the available hard drive properties.

So now that I have shown you how properties and sub-properties work, let's go back to the original question. How can we make PowerShell display a VM's IP address? The IP address is one of the NetworkAdapters properties. Therefore, if we wanted to see a VM's IP address, we could use this command:

Get-VM DC | Select-Object -ExpandProperty NetworkAdapters | Select-Object IPAddresses

You can see the output in Figure 6.

[Click on image for larger view.] Figure 6: I have retrieved a VM's IP address.

In this case, I am retrieving just the IP address, but I could just as easily retrieve any or all of the other network adapter-related properties.

About the Author

Brien Posey is a 22-time Microsoft MVP with decades of IT experience. As a freelance writer, Posey has written thousands of articles and contributed to several dozen books on a wide variety of IT topics. Prior to going freelance, Posey was a CIO for a national chain of hospitals and health care facilities. He has also served as a network administrator for some of the country's largest insurance companies and for the Department of Defense at Fort Knox. In addition to his continued work in IT, Posey has spent the last several years actively training as a commercial scientist-astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space. You can follow his spaceflight training on his Web site.


comments powered by Disqus

Subscribe on YouTube