Search code examples
powershelldns

Powershell to bulk query external DNS servers for various type of records?


I have a list of Domains I own that require some more information.

The below Script working great for querying but via the Internal Windows DNS server.

How can this be modified to use External DNS servers like 1.1.1.1 or 8.8.8.8 ?

$outFile = 'C:\Temp\DnsRecords.csv'
$zones = @(
    'domain1.net'
    'domain2.com'
    'domain3.org'
)

$rrTypes = @('Soa', 'Mx', 'Txt','NS', 'A', 'CNAME')
$dnsServer = 'PRD-INT-DNS-VM1' #internal DNS server only

$zones | ForEach-Object {
    $zone = $_
    $zoneSOA = (Resolve-DnsName -Name $zone -Type SOA -ErrorAction SilentlyContinue).PrimaryServer
    $rrTypes | ForEach-Object {
        $rrType = $_
        Try {
            Get-DnsServerResourceRecord -Zonename $zone -RRType $_ -ComputerName $dnsServer -ErrorAction Stop |
            Select-Object -Property `
            @{n = 'ZoneName'; e = { $zone } },
            @{n = 'ZoneSOA'; e = { $zoneSOA } },
            @{n = 'RecordType'; e = { $rrType } },
            Hostname,
            TimeToLive,
            @{n = 'Data'; e = {
                    $rr = $_
                    Switch ($rr.RecordType) {
                        'A' { $rr.RecordData.IPv4Address.IPAddressToString }
                        'CNAME' { $rr.RecordData.HostnameAlias }
                        'NS'   { $rr.RecordData.NameServer }
                        'SOA'   { $rr.RecordData.PrimaryServer }
                        'SRV'   { $rr.RecordData.DomainName }
                        'MX'   { "$($rr.RecordData.MailExchange); Preference=[$($rr.RecordData.Preference)]" }
                        'PTR'   { $rr.RecordData.PtrDomainName }
                        'AAAA'   { $rr.RecordData.IPv6Address }
                        'TXT'   { $rr.RecordData.DescriptiveText }
                        default { "Unsupported Record Type" }
                    }
                }
            },
            Error
        }
        Catch {
            $_ | Select-Object -Property `
            @{n = 'ZoneName'; e = { $zone } },
            @{n = 'ZoneSOA'; e = { $zoneSOA } },
            @{n = 'RecordType'; e = { $rrType } },
            Hostname,
            TimeToLive,
            Data,
            @{n = 'Error'; e = { $_.Exception.Message } }
        }
    }
} | Export-Csv -NoTypeInformation -Path $outFile
ii $outFile

Something similar like using: https://toolbox.googleapps.com/apps/dig/


Solution

  • It seems you need to be authenticated to do it; see https://stackoverflow.com/a/31805109/1703887. I found that I could use Resolve-DnsName to query various records. E.g.:

    PS> Resolve-DnsName -Name google.com -Type MX
    Name                                     Type   TTL   Section
    
        NameExchange                              Preference
    ----                                     ----   ---   -------    ------------                              ----------
    google.com                               MX     300   Answer     smtp.google.com                           10
    
    Name       : smtp.google.com
    QueryType  : A
    TTL        : 300
    Section    : Additional
    IP4Address : 142.251.116.26
    
    
    Name       : smtp.google.com
    QueryType  : A
    TTL        : 300
    Section    : Additional
    IP4Address : 142.251.116.27