#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.
Share on: