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
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.
 
        .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 serer itself. Faster but may be less accurate.
 
        .PARAMETER Silent
            Use this switch to disable any kind of verbose messages.
 
        .NOTES
            Tags: Network, Resolve
            Original Author: Klaas Vandenberghe ( @PowerDBAKlaas )
 
            Website: https://dbatools.io
            Copyright: (C) Chrissy LeMaire, clemaire@gmail.com
            License: GNU GPL v3 https://opensource.org/licenses/GPL-3.0
 
        .LINK
            https://dbatools.io/Resolve-DbaNetworkName
 
        .EXAMPLE
            Resolve-DbaNetworkName -ComputerName ServerA
 
            Returns a custom object displaying InputName, ComputerName, IPAddress, DNSHostName, Domain, FQDN for ServerA
 
        .EXAMPLE
            Resolve-DbaNetworkName -SqlInstance sql2016\sqlexpress
 
            Returns a custom object displaying InputName, ComputerName, IPAddress, DNSHostName, Domain, FQDN for the SQL instance sql2016\sqlexpress
 
        .EXAMPLE
            Resolve-DbaNetworkName -SqlInstance sql2016\sqlexpress, sql2014
 
            Returns a custom object displaying InputName, ComputerName, IPAddress, DNSHostName, Domain, FQDN for the SQL instance sql2016\sqlexpress and sql2014
 
            Get-DbaRegisteredServerName -SqlInstance sql2014 | Resolve-DbaNetworkName
 
            Returns a custom object displaying InputName, ComputerName, IPAddress, DNSHostName, Domain, FQDN for all SQL Servers returned by Get-DbaRegisteredServerName
    #>

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

    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 Verbose -Message "Resolving $Computer using .NET.Dns GetHostEntry"
                    $ipaddress = ([System.Net.Dns]::GetHostEntry($Computer)).AddressList[0].IPAddressToString
                    Write-Message -Level Verbose -Message "Resolving $ipaddress using .NET.Dns GetHostByAddress"
                    $fqdn = [System.Net.Dns]::GetHostByAddress($ipaddress).HostName
                }
                catch {
                    try {
                        Write-Message -Level Verbose -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 -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
                    Domain       = $fqdn.Replace("$hostname.", "")
                    DNSHostEntry = $fqdn
                    FQDN         = $fqdn
                }
                return
            }

            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 Verbose -Message "IP Address from $Computer is $ipaddress"
            }
            else {
                Write-Message -Level Verbose -Message "No IP Address returned from $Computer"
                Write-Message -Level Verbose -Message "Using .NET.Dns to resolve IP Address"
                return (Resolve-DbaNetworkName -ComputerName $Computer -Turbo)
            }

            if ($PSVersionTable.PSVersion.Major -gt 2) {
                Write-Message -Level Verbose -Message "Your PowerShell Version is $($PSVersionTable.PSVersion.Major)"
                try {
                    Write-Message -Level Verbose -Message "Getting computer information from $Computer"
                    if (Test-Bound "Credential") {
                        $conn = Get-DbaCmObject -ClassName win32_ComputerSystem -Computer $Computer -Credential $Credential -Silent
                    }
                    else {
                        $conn = Get-DbaCmObject -ClassName win32_ComputerSystem -Computer $Computer -Silent
                    }
                }
                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]

                        $conn = [PSCustomObject]@{
                            Name        = $Computer
                            DNSHostname = $hostname
                            Domain      = $fqdn.Replace("$hostname.", "")
                        }
                    }
                    catch {
                        Stop-Function -Message "No .NET.Dns information from $Computer" -InnerErrorRecord $_ -Continue
                    }
                }
            }

            try {
                Write-Message -Level Verbose -Message "Resolving $($conn.DNSHostname) using .NET.Dns GetHostEntry"
                $hostentry = ([System.Net.Dns]::GetHostEntry($conn.DNSHostname)).HostName
            }
            catch {
                Stop-Function -Message ".NET.Dns GetHostEntry failed for $($conn.DNSHostname)" -InnerErrorRecord $_
            }

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

            [PSCustomObject]@{
                InputName    = $OGComputer
                ComputerName = $conn.Name
                IPAddress    = $ipaddress
                DNSHostName  = $conn.DNSHostname
                Domain       = $conn.Domain
                DNSHostEntry = $hostentry
                FQDN         = $fqdn
            }
        }
    }
}