James O'Neill's Blog

August 21, 2007

NSLookup, WMI and Ping – three random things from the "I didn’t know that column"

Filed under: How to,Powershell — jamesone111 @ 11:00 pm

If you’re a regular reader you may know that I’ve been doing quite a lot of stuff with powershell of late, and Ive mentioned that I’m helping with some of the utilities for the the OCS resource Kit.

One of the questions I got asked was “Can we have a script to look up an SRV record”, I thought this was a bit odd because you can do this from the command line with NSLookup although everyone thinks of NS-Lookup as a tool that you user interactively. If you want to look up a record for a service (SRV) you simply invoke NSLookup with a switch and specify the name of the service, e.g.

     nslookup -q=SRV _sip._tls.microsoft.com

Next I was trying to test WMI remotely managing a machine and I could not get a connection to the relic which sits in my study running XP, after various searches and much head-scratching I found that if you get the error

    Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

It may be because the Windows firewall is blocking it. To unblock you use the NetSh. command as follows

    Netsh firewall set service RemoteAdmin 

Finally, what about pinging from a script – when I have trouble with a network connection I run IPConfig. That tells me if I have an IP address and what my DNS servers are. I was taught to ping the default gateway first, but I’ve always found going straight to DNS was easier.

In Powershell  I can get my IPConfig information with  Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE
and if I can get the DNS servers out of this information by piping the results to Select-Object -ExpandProperty DNSServerSearchOrder 
WMI has a PingStatus class so I can use it from Powershell by piping the list of servers into ForEach-Object -Process {Get-WmiObject -Class Win32_PingStatus -Filter (“Address='” + $_ + “‘”)}
and Finally I can display it nicely with Select-Object -Property Address,ResponseTime, TimeToLive

String them all togehter and I get

PS C:\Users\Jamesone> Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE |
>> Select-Object -ExpandProperty DNSServerSearchOrder |
>> ForEach-Object -Process {Get-WmiObject -Class Win32_PingStatus -Filter ("Address='" + $_ + "'")} |
>> Select-Object -Property Address,ResponseTime,timeToLive

Address                                         ResponseTime                              timeToLive
-------                                         ------------                              ----------                                             17                                     128                                             18                                     128

Technorati tags:

This post originally appeared on my technet blog.


Create a free website or blog at WordPress.com.

%d bloggers like this: