Question

What I'm trying to do

Query a DNS record using the System.Net.Dns class in Powershell using a specific Nameserver.

Problem

I can't find the method or property to do this.

Example Code

[System.Net.Dns]::Resolve("stackoverflow.com")

HostName                                Aliases                                 AddressList
--------                                -------                                 -----------
stackoverflow.com                       {}                                      {198.252.206.140}

But this resolves with the nameservers listed in my interface configuration.

How can I specify a nameserver to use instead?

Was it helpful?

Solution

Use an external .NET library that supports querying an alternate DNS server. For example, check out JH Software Simple DNS.

http://www.simpledns.com/dns-client-lib.aspx

PowerShell now offers a Resolve-DnsName command with a -Server parameter.

Resolve-DnsName -Server 8.8.4.4 -Name trevorsullivan.net

OTHER TIPS

If you don't want to use third party library, you can use the good old nslookup.exe

$client="10.110.10.10"
$ns="10.20.1.10"
(nslookup $client $ns |sls address |select -last 1).toString().split(":")[1].trim()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top