#PSTip Resolving IP addresses with WMI

When we need to resolve addresses, we usually use the System.Net.Dns .NET class methods.  In addition to the .NET method, we can also use WMI class–Win32_PingStatus–to achieve this. The Test-Connection cmdlet is great for checking if a host is up. You can pass a name or an IP address and send ICMP echo request packets (“pings”) to one or more computers. When you ping using a name, the result also includes its IP address.

PS> Test-Connection -ComputerName LOKI -Count 1

Source        Destination     IPV4Address      IPV6Address   Bytes    Time(ms)
------        -----------     -----------      -----------   -----    --------
SHAYPC        LOKI                      32       0

However, when you use an IP, you don’t get back the name. Test-Connection relies on the Win32_PingStatus class. There is an option available on the class properties to resolve the name. Once we set the ResolveAddressNames property to True, an attempt to resolve the address is made. If it succeeds,  the ProtocolAddressResolved property is populated with the name of the  target.

PS> Get-WmiObject Win32_PingStatus -Filter "Address='' AND ResolveAddressNames='true'" | Select-Object IPV4Address,ProtocolAddressResolved

IPV4Address   ProtocolAddressResolved
-----------   -----------------------   LOKI

It would be great if the Test-Connection cmdlet had a ResolveAddressNames switch parameter instead of having to craft a WMI request. Like the idea? Would you like to see this in future releases of PowerShell? Add your vote to this suggestion.

About the author: Shay Levy

Shay Levy is a Co-founder and editor of the PowerShell Magazine. He is a multiple-year recipient of the Microsoft MVP award, and a Microsoft Certified Trainer (MCT). Shay often covers PowerShell related topics on his blog and you can also follow him on Twitter at @ShayLevy

Related Posts