functions/Resolve-DbaNetworkName.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
function Resolve-DbaNetworkName {
    <#
        .SYNOPSIS
            Returns information about the network connection of the target computer including NetBIOS name, IP Address, domain name and fully qualified domain name (FQDN).
 
        .DESCRIPTION
            Retrieves the IPAddress, ComputerName from one computer.
            The object can be used to take action against its name or IPAddress.
 
            First ICMP is used to test the connection, and get the connected IPAddress.
 
            Multiple protocols (e.g. WMI, CIM, etc) are attempted before giving up.
 
            Important: Remember that FQDN doesn't always match "ComputerName dot Domain" as AD intends.
                There are network setup (google "disjoint domain") where AD and DNS do not match.
                "Full computer name" (as reported by sysdm.cpl) is the only match between the two,
                and it matches the "DNSHostName" property of the computer object stored in AD.
                This means that the notation of FQDN that matches "ComputerName dot Domain" is incorrect
                in those scenarios.
                In other words, the "suffix" of the FQDN CAN be different from the AD Domain.
 
                This cmdlet has been providing good results since its inception but for lack of useful
                names some doubts may arise.
                Let this clear the doubts:
                - InputName: whatever has been passed in
                - ComputerName: hostname only
                - IPAddress: IP Address
                - DNSHostName: hostname only, coming strictly from DNS (as reported from the calling computer)
                - DNSDomain: domain only, coming strictly from DNS (as reported from the calling computer)
                - Domain: domain only, coming strictly from AD (i.e. the domain the ComputerName is joined to)
                - DNSHostEntry: Fully name as returned by DNS [System.Net.Dns]::GetHostEntry
                - FQDN: "legacy" notation of ComputerName "dot" Domain (coming from AD)
                - FullComputerName: Full name as configured from within the Computer (i.e. the only secure match between AD and DNS)
 
            So, if you need to use something, go with FullComputerName, always, as it is the most correct in every scenario.
 
        .PARAMETER ComputerName
            The Server that you're connecting to.
            This can be the name of a computer, a SMO object, an IP address or a SQL Instance.
 
        .PARAMETER Credential
            Credential object used to connect to the SQL Server as a different user
 
        .PARAMETER Turbo
            Resolves without accessing the server itself. Faster but may be less accurate because it relies on DNS only,
            so it may fail spectacularly for disjoin-domain setups. Also, everyone has its own DNS (i.e. results may vary
            changing the computer where the function runs)
 
        .PARAMETER EnableException
            By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
            This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
            Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
 
        .NOTES
            Tags: Network, Resolve
            Author: Klaas Vandenberghe ( @PowerDBAKlaas )
            Editor: niphlod
 
            Website: https://dbatools.io
            Copyright: (C) Chrissy LeMaire, clemaire@gmail.com
            License: MIT https://opensource.org/licenses/MIT
 
        .LINK
            https://dbatools.io/Resolve-DbaNetworkName
 
        .EXAMPLE
            Resolve-DbaNetworkName -ComputerName ServerA
 
            Returns a custom object displaying InputName, ComputerName, IPAddress, DNSHostName, DNSDomain, Domain, DNSHostEntry, FQDN, DNSHostEntry for ServerA
 
        .EXAMPLE
            Resolve-DbaNetworkName -SqlInstance sql2016\sqlexpress
 
            Returns a custom object displaying InputName, ComputerName, IPAddress, DNSHostName, DNSDomain, Domain, DNSHostEntry, FQDN, DNSHostEntry for the SQL instance sql2016\sqlexpress
 
        .EXAMPLE
            Resolve-DbaNetworkName -SqlInstance sql2016\sqlexpress, sql2014
 
            Returns a custom object displaying InputName, ComputerName, IPAddress, DNSHostName, DNSDomain, Domain, DNSHostEntry, FQDN, DNSHostEntry for the SQL instance sql2016\sqlexpress and sql2014
 
        .EXAMPLE
            Get-DbaRegisteredServer -SqlInstance sql2014 | Resolve-DbaNetworkName
 
            Returns a custom object displaying InputName, ComputerName, IPAddress, DNSHostName, Domain, FQDN for all SQL Servers returned by Get-DbaRegisteredServer
    #>

    [CmdletBinding()]
    param (
        [parameter(ValueFromPipeline)]
        [Alias('cn', 'host', 'ServerInstance', 'Server', 'SqlInstance')]
        [DbaInstanceParameter[]]$ComputerName = $env:COMPUTERNAME,
        [PSCredential] $Credential,
        [Alias('FastParrot')]
        [switch]$Turbo,
        [switch][Alias('Silent')]$EnableException
    )

    process {
        foreach ($Computer in $ComputerName) {
            $conn = $ipaddress = $null

            $OGComputer = $Computer

            if ($Computer.IsLocalhost) {
                $Computer = $env:COMPUTERNAME
            }
            else {
                $Computer = $Computer.ComputerName
            }

            if ($Turbo) {
                try {
                    Write-Message -Level VeryVerbose -Message "Resolving $Computer using .NET.Dns GetHostEntry"
                    $ipaddress = ([System.Net.Dns]::GetHostEntry($Computer)).AddressList[0].IPAddressToString
                    Write-Message -Level VeryVerbose -Message "Resolving $ipaddress using .NET.Dns GetHostByAddress"
                    $fqdn = [System.Net.Dns]::GetHostByAddress($ipaddress).HostName
                }
                catch {
                    try {
                        Write-Message -Level VeryVerbose -Message "Resolving $Computer and IP using .NET.Dns GetHostEntry"
                        $resolved = [System.Net.Dns]::GetHostEntry($Computer)
                        $ipaddress = $resolved.AddressList[0].IPAddressToString
                        $fqdn = $resolved.HostName
                    }
                    catch {
                        Stop-Function -Message "DNS name not found" -Continue -InnerErrorRecord $_
                    }
                }

                if ($fqdn -notmatch "\.") {
                    if ($computer.ComputerName -match "\.") {
                        $dnsdomain = $computer.ComputerName.Substring($computer.ComputerName.IndexOf(".") + 1)
                        $fqdn = "$resolved.$dnsdomain"
                    }
                    else {
                        $dnsdomain = "$env:USERDNSDOMAIN".ToLower()
                        if ($dnsdomain -match "\.") {
                            $fqdn = "$fqdn.$dnsdomain"
                        }
                    }
                }

                $hostname = $fqdn.Split(".")[0]

                [PSCustomObject]@{
                    InputName        = $OGComputer
                    ComputerName     = $hostname.ToUpper()
                    IPAddress        = $ipaddress
                    DNSHostname      = $hostname
                    DNSDomain        = $fqdn.Replace("$hostname.", "")
                    Domain           = $fqdn.Replace("$hostname.", "")
                    DNSHostEntry     = $fqdn
                    FQDN             = $fqdn
                    FullComputerName = $fqdn
                }

            }
            else {

                Write-Message -Level Verbose -Message "Connecting to $Computer"

                try {
                    $ipaddress = ((Test-Connection -ComputerName $Computer -Count 1 -ErrorAction Stop).Ipv4Address).IPAddressToString
                }
                catch {
                    try {
                        if ($env:USERDNSDOMAIN) {
                            $ipaddress = ((Test-Connection -ComputerName "$Computer.$env:USERDNSDOMAIN" -Count 1 -ErrorAction SilentlyContinue).Ipv4Address).IPAddressToString
                            $Computer = "$Computer.$env:USERDNSDOMAIN"
                        }
                    }
                    catch {
                        $Computer = $OGComputer
                        $ipaddress = ([System.Net.Dns]::GetHostEntry($Computer)).AddressList[0].IPAddressToString
                    }
                }

                if ($ipaddress) {
                    Write-Message -Level VeryVerbose -Message "IP Address from $Computer is $ipaddress"
                }
                else {
                    Write-Message -Level VeryVerbose -Message "No IP Address returned from $Computer"
                    Write-Message -Level VeryVerbose -Message "Using .NET.Dns to resolve IP Address"
                    return (Resolve-DbaNetworkName -ComputerName $Computer -Turbo)
                }

                if ($PSVersionTable.PSVersion.Major -gt 2) {
                    Write-Message -Level System -Message "Your PowerShell Version is $($PSVersionTable.PSVersion.Major)"
                    try {
                        try {
                            # if an alias (CNAME) is passed we should try to connect to the A name via CIM or WinRM
                            $ComputerNameIP = ([System.Net.Dns]::GetHostEntry($Computer)).AddressList[0].IPAddressToString
                            $RemoteComputer = [System.Net.Dns]::GetHostByAddress($ComputerNameIP).HostName
                        }
                        catch {
                            $RemoteComputer = $Computer
                        }
                        Write-Message -Level VeryVerbose -Message "Getting computer information from $RemoteComputer"
                        $ScBlock = {
                            $IPGProps = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()
                            return [pscustomobject]@{
                                'DNSDomain' = $IPGProps.DomainName
                            }
                        }
                        if (Test-Bound "Credential") {
                            $conn = Get-DbaCmObject -ClassName win32_ComputerSystem -Computer $RemoteComputer -Credential $Credential -EnableException
                            $DNSSuffix = Invoke-Command2 -Computer $RemoteComputer -ScriptBlock $ScBlock -Credential $Credential -ErrorAction Stop
                        }
                        else {
                            $conn = Get-DbaCmObject -ClassName win32_ComputerSystem -Computer $RemoteComputer -EnableException
                            $DNSSuffix = Invoke-Command2 -Computer $RemoteComputer -ScriptBlock $ScBlock -ErrorAction Stop
                        }
                    }
                    catch {
                        Write-Message -Level Verbose -Message "Unable to get computer information from $Computer"
                    }

                    if (!$conn) {
                        Write-Message -Level Verbose -Message "No WMI/CIM from $Computer. Getting HostName via .NET.Dns"
                        try {
                            $fqdn = ([System.Net.Dns]::GetHostEntry($Computer)).HostName
                            $hostname = $fqdn.Split(".")[0]
                            $suffix = $fqdn.Replace("$hostname.", "")
                            if ($hostname -eq $fqdn) {
                                $suffix = ""
                            }
                            $conn = [PSCustomObject]@{
                                Name        = $Computer
                                DNSHostname = $hostname
                                Domain      = $suffix
                            }
                            $DNSSuffix = [PSCustomObject]@{
                                DNSDomain = $suffix
                            }
                        }
                        catch {
                            Stop-Function -Message "No .NET.Dns information from $Computer" -InnerErrorRecord $_ -Continue
                        }
                    }
                }
                if ($DNSSuffix.DNSDomain.Length -eq 0) {
                    $FullComputerName = $conn.DNSHostname
                }
                else {
                    $FullComputerName = $conn.DNSHostname + "." + $DNSSuffix.DNSDomain
                }
                try {
                    Write-Message -Level VeryVerbose -Message "Resolving $FullComputerName using .NET.Dns GetHostEntry"
                    $hostentry = ([System.Net.Dns]::GetHostEntry($FullComputerName)).HostName
                }
                catch {
                    Stop-Function -Message ".NET.Dns GetHostEntry failed for $FullComputerName" -InnerErrorRecord $_
                }

                $fqdn = "$($conn.DNSHostname).$($conn.Domain)"
                if ($fqdn -eq ".") {
                    Write-Message -Level VeryVerbose -Message "No full FQDN found. Setting to null"
                    $fqdn = $null
                }
                if ($FullComputerName -eq ".") {
                    Write-Message -Level VeryVerbose -Message "No DNS FQDN found. Setting to null"
                    $FullComputerName = $null
                }

                if ($FullComputerName -ne "." -and $FullComputerName -notmatch "\." -and $conn.Domain -match "\.") {
                    $d = $conn.Domain
                    $FullComputerName = "$FullComputerName.$d"
                }

                [PSCustomObject]@{
                    InputName        = $OGComputer
                    ComputerName     = $conn.Name
                    IPAddress        = $ipaddress
                    DNSHostName      = $conn.DNSHostname
                    DNSDomain        = $DNSSuffix.DNSDomain
                    Domain           = $conn.Domain
                    DNSHostEntry     = $hostentry
                    FQDN             = $fqdn.TrimEnd(".")
                    FullComputerName = $FullComputerName
                }
            }
        }
    }
}