6

When I run nslookup from a PowerShell script, I always get an error (which is output to the console) despite the fact the lookup succeeds:

PS C:\Windows\system32> $MyOutput = nslookup -q=SOA superuser.com
8.8.4.4 nslookup : Non-authoritative answer: At line:1 char:13
+ $MyOutput = nslookup -q=SOA superuser.com 8.8.4.4
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Non-authoritative answer::String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

This seems to be caused by the fact the answer is non-authoritative. Doing a lookup against an authoritative DNS server doesn't return an error.

In my attempts to find a solution myself, I found this SO answer. It suggests using the Resolve-DNSName command. Unfortunately that requires Windows 8.1/Server 2012 R2 and some of the systems my script will run on are Windows 7-era.

How can I prevent this error from being displayed?

Bonus points for explaining why PowerShell thinks an error has occurred!

I say Reinstate Monica
  • 25,487
  • 19
  • 95
  • 131
  • you can try using `[System.Net.Dns]::Resolve("superuser.com")` instead of `Resolve-DNSName` cmdlet. Regarding `nslookup`, interestingly it is not throwing an error in Posh 5, but directs the `Non-authoritative answer:` to the console (even though output is assinged to variable - just as in your example) – WeatherForecastingRat Sep 25 '17 at 21:08
  • 1
    you should be able to prevent it, if you set `$ErrorActionPreference = "SilentlyContinue"` at the beginning of your script. Keep in mind that this will surpress any error in the script from being displayed – SimonS Sep 26 '17 at 11:01
  • `$MyOutput = nslookup -q=SOA superuser.com 2>$null` should suffice as `nslookup` posts the `Non-authoritative answer:` message to stdERR. For proof, run `>NUL nslookup -q=SOA superuser.com` as well as `2>NUL nslookup -q=SOA superuser.com` from an open `cmd` window… – JosefZ Sep 27 '17 at 07:51
  • As the comments suggest, this varies per version of Powershell. If using v5, the `Non-authoritative answer:` line can be excluded via the `2>$null` redirect. If using a lesser version where an error is the output, you could modify the `$ErrorActionPreference`. Don't think `-ErrorAction` will be available here. There are other .NET alternatives, including `[System.Net.Dns]::GetHostAddresses("superuser.com")`. – root Sep 27 '17 at 14:36
  • There's also a PowerShell alternative man.... `Resolve-DNSName `—test it out when or if you ever get a chance and if it's applicable with your PS versions in your environment. [Resolve-DNSName](https://docs.microsoft.com/en-us/powershell/module/dnsclient/resolve-dnsname?view=winserver2012r2-ps) – Vomit IT - Chunky Mess Style Jan 13 '18 at 06:19
  • 1
    @ITSnuggles Some of the systems I am doing this on are Windows 7 and the version of PS with that cmdlet don't run on that OS. :( – I say Reinstate Monica Jan 13 '18 at 13:32

1 Answers1

3

Ignore the executable's error by redirecting to $null

Your executable is sending output to the STDERR stream. You can suppress it by redirecting it to the automatic $null variable:

nslookup.exe example.com 2>$null

Notes:

  • You must redirect to PowerShell's $null variable. PS won't let you do it the old school way (i.e. 2>nul).

  • Redirecting to $null is faster than using Out-Null


Explanation

NSLookup is sending a portion of its output to the STDERR stream. Whenever a Windows console application does this, PowerShell reports this as a NativeCommandError error.

In Command Prompt run nslookup -q=SOA superuser.com 1>nul 2>con to see what NSLookup is writing to STDERR:

Non-authoritative answer:

This is exactly what PowerShell returns in the first line of its error message:

nslookup : Non-authoritative answer:
At line:1 char:1
+ nslookup -q=ns example.com

Apparently NSLookup returns an error when its answer includes records from a non-authoritative name server. However in your case that doesn't seem to be a problem so you can ignore the error as described above.

I say Reinstate Monica
  • 25,487
  • 19
  • 95
  • 131