6

#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            10.10.10.10                    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='10.10.10.10' AND ResolveAddressNames='true'" | Select-Object IPV4Address,ProtocolAddressResolved

IPV4Address   ProtocolAddressResolved
-----------   -----------------------
10.10.10.10   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.

Filed in: Columns, Tips and Tricks Tags: , , , ,

6 Responses to "#PSTip Resolving IP addresses with WMI"

  1. Martin Pugh says:

    Looks like you got bit by your editor, it changed the IP address into a HTTP tag!

    I’ve used this class before, even in my vbScript days, and it’s a great class. Very fast and easy to work with (as you can see above!)

  2. Martin Pugh says:

    LOL, and you’ve already fixed it! Ignore me 🙂

Leave a Reply

Submit Comment

© 2016 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: