Use PowerShell to determine the IP address your VMs are using

People who do not work in IT often assume that IT pros have access to absolutely all of their company’s IT resources. Although that might have been the case at one time, IT responsibilities have become increasingly siloed in recent years. In production environments, virtualization administrators rarely have permission to log into the virtual machines that they manage. While there are undoubtedly advantages to segmenting administrative access rights in this way, doing so can also make it challenging to troubleshoot problems. If a particular VM is experiencing network connectivity problems, for example, then the virtualization administrator might not be able to log into the VM to diagnose the problem. Fortunately, there are ways of gaining access to a virtual machine’s network configuration details (such as the IP address that the VM is using) without having to log into the VM to do so.

Get-VM cmdlet: Helpful — to a degree

One of the PowerShell cmdlets that administrators use most often to get information about a virtual machine is Get-VM. As handy as this cmdlet is, however, there is a limit to the information that it can provide. To show you what I mean, I have created a virtualized domain controller on a Hyper-V server. This domain controller is named DC. Typing Get-VM DC causes PowerShell to display some really basic information about the VM. As you can see in the figure below, for example, PowerShell displays the VM’s state, how much memory the VM is using, and how long the VM has been running. While the information shown on this screen can be helpful, it isn’t going to be sufficient for troubleshooting most networking problems.


Fortunately, the Get-VM cmdlet is capable of displaying much more detailed information than what is shown in the screenshot above. If you want to see a more detailed view of the virtual machine’s various attributes, then enter this command:

Get-VM <virtual machine name> | Select-Object *

You can see what this command’s output looks like in the screenshot below.

So, where’s the VM’s IP address?

The screen capture shown above includes information that would be legitimately useful in a troubleshooting situation. However, you might have noticed that the Get-VM cmdlet’s output does not list the virtual machine’s IP address. Even so, there is a way to determine the VM’s IP address and other network-related configuration settings.

The screenshot above only shows part of the Get-VM cmdlet’s output. If you look at the screenshot below, you can see the last few items that were displayed. As you read through the list of properties, you will notice that one of them is named NetworkAdapters. If you look closely, however, you will also notice that whereas the other properties shown in the screen capture have values associated with them, NetworkAdapters is showing a value of {Network Adapter, Network Adapter, Posey}.


The fact that the value is enclosed in braces indicates that NetworkAdapters is a property that can be expanded. Expanding this property will allow PowerShell to display the virtual machine’s network configuration, including its IP address. You can perform the expansion by using the Get-VM cmdlet in conjunction with the Select-Object cmdlet and the ExpandProperty switch. Here is what the command looks like:

Get-VM <virtual machine name> | Select-Object -ExpandProperty NetworkAdapters

If you look at the next screenshot, you can see that this particular VM has three virtual network adapters. You can also see the IPv4 and the IPv6 addresses for each.


As you can see in the figure above, some VMs have multiple network adapters, and each network adapter typically has both an IPv4 and an IPv6 address. If you want a list of virtual machines, their network adapter names, and their IPv4 addresses (without the IPv6 address), you will have to move beyond the command line and create a script. Here is a script that I created for this purpose:

$VMs = Get-VM
ForEach($VM in $VMs){
$Adapters=($VM | Get-VMNetworkAdapter)
ForEach ($Adapter in $Adapters){
Write-Host($VM.VMName,’ ‘, $Adapter.Name, ‘ ‘, $Adapter.IPAddresses[0])
}
}

The script includes two ForEach loops. The first of these loops progresses through a list of the VMs residing on the server. The second loop looks at each virtual network adapter within the VM that is currently being examined.

For each network adapter within each VM, the script displays the virtual machine name ($VM.VMName), the network adapter name ($Adapter.Name), and the IPv4 address ($Adapter.IPAddresses[0]).

In case you are wondering, IPAddresses contains multiple properties including an IPv4 address and an IPv6 address. Unfortunately, these properties are not named, which means that there is no easy way to tell PowerShell to show only the IPv4 address. Since the properties are arranged in an array structure, I was able to append [0] to $Adapter.IPAddresses, which tells PowerShell to show the item in the 0 position of the IPAddresses array. The zero position contains the first IP address on the list, which is typically the IPv4 address.

You can see this script in action in the figure below. It is worth noting that I made one small modification to the script shown in the figure. Rather than allowing the script to display all of the IP addresses for all of my virtual machines, I limited the script to displaying a single VM’s IP addresses by including the name of the VM (DC) in the very first line of code.

One limitation

The one big limitation to using my approach is that if a virtual machine has multiple IPv4 addresses assigned to a single virtual network adapter, then only the first of those addresses will be shown. As an alternative to displaying a specific array position, it might be possible to use string filtering techniques to filter out IPv6 addresses, leaving only the IPv4 addresses to be displayed.

Incidentally, it is far easier to find a VM’s IP address if you can actually log into the VM. From inside of the virtual machine, simply use the IPConfig command to see the IP addresses that the VM is using.

Featured image: Freerange Stock

Brien Posey

Brien Posey is a freelance technology author and speaker with over two decades of IT experience. Prior to going freelance, Brien was a CIO for a national chain of hospitals and healthcare facilities. He has also served as a network engineer for the United States Department of Defense at Fort Knox. In addition, Brien has worked as a network administrator for some of the largest insurance companies in America. To date, Brien has received Microsoft’s MVP award numerous times in categories including Windows Server, IIS, Exchange Server, and File Systems / Storage. You can visit Brien’s Website at: www.brienposey.com.

Share
Published by
Brien Posey

Recent Posts

Exchange 2010 upgrade: Migrate or export mail to PST and start fresh?

If you’re on Exchange 2010, you will have to upgrade soon. And while starting from scratch with a new 2016…

2 hours ago

How to repair PST files and import data back to Outlook or Office 365

If your business relies on Outlook, you can’t risk losing mailbox data because of PST files corruption. Here’s how to…

3 days ago

Container security rises to meet the challenges of container vulnerabilities

As container technology becomes ubiquitous, container security has become crucial. Here’s a look at some recent innovations in this growing…

3 days ago

Best of CES 2020: Products, innovations, and services

From flying Ubers to rolling robots, CES 2020 had it all — and then some. Here’s a look at some…

4 days ago

Hardening your technology infrastructure in preparation for a DDoS attack

By establishing these 11 appropriate controls beforehand, your organization will be better positioned to withstand and survive a DDoS attack.

4 days ago

Microsoft App-V as an application virtualization solution: Pros & cons

If your shop is considering using App-V as an application virtualization solution, read this article first and weigh the pros…

4 days ago