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
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.
 
      If your local Powershell version is not higher than 2, WMI is tried to get the computername.
      If not, CIM is used, first via WinRM, and if not successful, via DCOM.
 
      .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.
 
      .NOTES
      Author: Klaas Vandenberghe ( @PowerDBAKlaas )
 
      dbatools PowerShell module (https://dbatools.io)
      Copyright (C) 2016 Chrissy LeMaire
      This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
      This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
      You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
 
      .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 -SqlServer sql2016\sqlexpress
 
      Returns a custom object displaying InputName, ComputerName, IPAddress, DNSHostName, Domain, FQDN for the SQL instance sql2016\sqlexpress
  
      .EXAMPLE
      Resolve-DbaNetworkName -SqlServer sql2016\sqlexpress, sql2014
 
      Returns a custom object displaying InputName, ComputerName, IPAddress, DNSHostName, Domain, FQDN for the SQL instance sql2016\sqlexpress and sql2014
 
      Get-SqlRegisteredServerName -SqlServer sql2014 | Resolve-DbaNetworkName
  
      Returns a custom object displaying InputName, ComputerName, IPAddress, DNSHostName, Domain, FQDN for all SQL Servers returned by Get-SqlRegisteredServerName
  #>

[CmdletBinding()]
param (
[parameter(ValueFromPipeline)]
[Alias('cn', 'host', 'ServerInstance', 'Server', 'SqlServer')]
[object[]]$ComputerName = $env:COMPUTERNAME,
    [PSCredential] [System.Management.Automation.CredentialAttribute()]$Credential,
[Alias('FastParrot')]
[switch]$Turbo
)
BEGIN
{
$functionName = (Get-PSCallstack)[0].Command
}
PROCESS
{
foreach ($Computer in $ComputerName)
{
$conn = $ipaddress = $CIMsession = $null

if ($Computer.GetType() -eq [Microsoft.SqlServer.Management.Smo.Server])
{
$Computer = $Computer.NetName
}

$OGComputer = $Computer

if ($Computer -eq 'localhost' -or $Computer -eq '.')
{
$Computer = $env:COMPUTERNAME
}

$Computer = $Computer.Split('\')[0]

if ($Turbo)
{
try
{
Write-Verbose "$functionName - Resolving $Computer using .NET.Dns GetHostEntry"
$ipaddress = ([System.Net.Dns]::GetHostEntry($Computer)).AddressList[0].IPAddressToString
Write-Verbose "$functionName - Resolving $ipaddress using .NET.Dns GetHostByAddress"
$fqdn = [System.Net.Dns]::GetHostByAddress($ipaddress).HostName
}
catch
{
try
{
Write-Verbose "$functionName - Resolving $Computer and IP using .NET.Dns GetHostEntry"
$resolved = [System.Net.Dns]::GetHostEntry($Computer)
$ipaddress = $resolved.AddressList[0].IPAddressToString
$fqdn = $resolved.HostName
}
catch
{
Write-Warning "$functionName - DNS name not found"
continue
}
}

if ($fqdn -notmatch "\.")
{
$dnsdomain = $env:USERDNSDOMAIN.ToLower()
$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-Verbose "$functionName - Connecting to $Computer"

try
{
$ipaddress = ((Test-Connection -ComputerName $Computer -Count 1 -ErrorAction Stop).Ipv4Address).IPAddressToString
}
catch
{
try
{
$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-Verbose "$functionName - IP Address from $Computer is $ipaddress"
}
else
{
Write-Warning "$functionName - No IP Address returned from $Computer"
Write-Verbose "$functionName - Using .NET.Dns to resolve IP Address"
return (Resolve-DbaNetworkName -ComputerName $Computer -Turbo)
}

if ($host.Version.Major -gt 2)
{
Write-Verbose "$functionName - Your PowerShell Version is $($host.Version.Major)"
try
{
Write-Verbose "$functionName - Getting computer information from $Computer via CIM (WSMan)"
if ($Credential)
{
$CIMsession = New-CimSession -ComputerName $Computer -ErrorAction SilentlyContinue -Credential $Credential
$conn = Get-CimInstance -Query "Select * FROM Win32_computersystem" -CimSession $CIMsession -ErrorAction SilentlyContinue
}
else
{
$conn = Get-CimInstance -Query "Select * FROM Win32_computersystem" -ComputerName $Computer -ErrorAction SilentlyContinue
}
}
catch
{
Write-Verbose "$functionName - No WSMan connection to $Computer"
}
if (!$conn)
{
try
{
Write-Verbose "$functionName - Getting computer information from $Computer via CIM (DCOM)"
$sessionoption = New-CimSessionOption -Protocol DCOM
if ($Credential)
{
$CIMsession = New-CimSession -ComputerName $Computer -SessionOption $sessionoption -ErrorAction SilentlyContinue -Credential $Credential

}
else
{
$CIMsession = New-CimSession -ComputerName $Computer -SessionOption $sessionoption -ErrorAction SilentlyContinue
}

$conn = Get-CimInstance -Query "Select * FROM Win32_computersystem" -CimSession $CIMsession
}
catch
{
Write-Warning "$functionName - No DCOM connection for CIM to $Computer"
}
}

if (!$conn)
{
Write-Verbose "$functionName - No 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
{
Write-Warning "$functionName - No .NET.Dns information from $Computer"
continue
}
}
}


try
{
Write-Verbose "$functionName - Resolving $($conn.DNSHostname) using .NET.Dns GetHostEntry"
$hostentry = ([System.Net.Dns]::GetHostEntry($conn.DNSHostname)).HostName
}
catch
{
Write-Warning "$functionName - .NET.Dns GetHostEntry failed for $($conn.DNSHostname)"
}

$fqdn = "$($conn.DNSHostname).$($conn.Domain)"
if ($fqdn -eq ".")
{
Write-Verbose "$functionName - 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
}
}
}
}