Private/Get-NameserverList.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
function Get-NameServerList {
    [OutputType([Collections.Generic.List[DnsClient.NameServer]])]
    [CmdletBinding()]
    param(
        [string[]]$NameServer,
        [switch]$IgnoreActive
    )

    # Since Powershell can't coerce an array of IP addresses into
    # the IReadonlyCollection[NameServer] list we need for the
    # QueryServer* methods in LookupClient and we also want to
    # allow users to use FQDNs for nameservers, we'll do some
    # extra work here to make things "just work"

    # Unless called with -IgnoreActive, this method should always
    # return a valid nameserver list that can be called using
    # LookupClient.QueryServer* methods.

    if (-not $NameServer -or $NameServer.Count -eq 0) {
        if ($IgnoreActive) {
            Write-Debug "Returning null nameservers"
            return $null
        }
        elseif ($script:ActiveNameServers) {
            Write-Debug "Returning ActiveNameServers"
            return ,$script:ActiveNameServers
        }
        else {
            Write-Debug "Returning default nameservers"
            return ,(Get-LookupClient).NameServers
        }
    }

    $nsList = [Collections.Generic.List[DnsClient.NameServer]]::new()

    foreach ($val in $NameServer) {

        # each value here should be an FQDN or IP
        # optionally followed by a ":PORT"

        if (-not $val) {
            Write-Warning "Skipping null nameserver value"
            continue
        }

        # split the nameserver from the port if it exists
        $nsString, $port = $val.Split(':')

        if ([String]::IsNullOrWhiteSpace($nsString)) {
            Write-Warning "Skipping empty nameserver value"
            continue
        }

        # if it's just an IP address, we're done
        if ($ip = $nsString -as [ipaddress]) {

            # WARNING: The [ipaddress] conversion will happily accept
            # integer values and convert them to an IP
            # (e.g. '1' -as [ipaddress] = 0.0.0.1)
            # Technically this is a "feature", but may bite people unaware.

            if ($port -and ($port -as [int]) -gt 0) {
                $ns = [DnsClient.NameServer]::new($ip, $port)
            } else {
                $ns = [DnsClient.NameServer]::new($ip)
            }
            $nsList.Add($ns)
            Write-Debug "Parsed nameserver as $ns"

            continue
        }

        # It's not an IP address, so we'll try and resolve it using the
        # default system resolver.
        $response = Resolve-Dns $nsString
        if ($response.Answers.Count -gt 0) {

            # If there are multiple answers, we're only going to use the first one
            # Time will tell if that comes back to bit us.
            if ($port -and ($port -as [int]) -gt 0) {
                $ns = [DnsClient.NameServer]::new($response.Answers[0].Address, $port)
            } else {
                $ns = [DnsClient.NameServer]::new($response.Answers[0].Address)
            }
            $nsList.Add($ns)
            Write-Debug "Parsed nameserver as $ns"

        } else {
            Write-Warning "Unable to resolve nameserver $nsString to an IP address. Skipping."
        }

    }

    # return what we've got if there is anything
    if ($nsList.Count -gt 0) {
        Write-Debug "Returning converted nameservers"
        return ,$nsList
    } elseif (-not $IgnoreActive) {
        Write-Debug "Returning default nameservers"
        return ,(Get-LookupClient).NameServers
    } else {
        Write-Debug "Returning null nameservers"
        return $null
    }

}