PowerShell Genie

Trace DNS resolution across configured resolvers

Script 1 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

The script asks each configured IPv4 DNS server directly. That removes the local resolver cache and server-selection ambiguity from the comparison.

The script

1PS> $name = 'app.contoso.com'
2PS> $servers = (Get-DnsClientServerAddress -AddressFamily IPv4).ServerAddresses | Sort-Object -Unique
3PS> foreach ($server in $servers) {
4PS> try {
5PS> $answer = Resolve-DnsName -Name $name -Server $server -Type A -ErrorAction Stop
6PS> [pscustomobject]@{ Server= $server;Status= 'Resolved';Address=($answer.IPAddress -join ', ') }
7PS> }
8PS> catch {
9PS> [pscustomobject]@{ Server= $server;Status= 'Failed';Address= $null;Error= $_.Exception.Message }
10PS> } }

Script reader

Inspect before you run

Inspection only

Inspect the script below. It does not run in this browser.

Inspection only — no command has run.

    Show reference output
    1 Server Status Address Error
    2 ------ ------ ------- -----
    3 10.20.30.10 Healthy 10.20.30.11
    Inspect this example’s structure

    Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.

    $name = 'app.contoso.com' $servers = (Get-DnsClientServerAddress -AddressFamily IPv4).ServerAddresses | Sort-Object -Unique foreach ($server in $servers) { try { $answer = Resolve-DnsName -Name $name -Server $server -Type A -ErrorAction Stop [pscustomobject]@{ Server= $server;Status= 'Resolved';Address=($answer.IPAddress -join ', ') } } catch { [pscustomobject]@{ Server= $server;Status= 'Failed';Address= $null;Error= $_.Exception.Message } } }
    1. Stage 1$namePositional values or no named parameters
    2. Stage 2Sort-Object-Unique

      How it works

      The script asks each configured IPv4 DNS server directly. That removes the local resolver cache and server-selection ambiguity from the comparison. Failures are captured as data so one dead resolver does not stop the rest of the test.

      Adapting it

      Change Type to AAAA, SRV or CNAME when the record type matters. Add Resolve-DnsName -DnsOnly when you specifically want to avoid fallback behavior.

      Primary documentation

      Sort-Object reference

      Continue
      Genie guideTrace DNS resolution across configured resolvers

      The script asks each configured IPv4 DNS server directly. That removes the local resolver cache and server-selection ambiguity from the comparison.