@@ -263,6 +263,16 @@ Function Get-SQLConnectionTest
263
263
HelpMessage = 'SQL Server instance to connection to.')]
264
264
[string]$Instance,
265
265
266
+ [Parameter(Mandatory = $false,
267
+ ValueFromPipeline = $true,
268
+ ValueFromPipelineByPropertyName = $true,
269
+ HelpMessage = 'IP Address of SQL Server.')]
270
+ [string]$IPAddress,
271
+
272
+ [Parameter(Mandatory = $false,
273
+ HelpMessage = 'IP Address Range In CIDR Format to Audit.')]
274
+ [string]$IPRange,
275
+
266
276
[Parameter(Mandatory = $false,
267
277
HelpMessage = 'Connect using Dedicated Admin Connection.')]
268
278
[Switch]$DAC,
@@ -291,14 +301,43 @@ Function Get-SQLConnectionTest
291
301
292
302
Process
293
303
{
294
- # Parse computer name from the instance
295
- $ComputerName = Get-ComputerNameFromInstance -Instance $Instance
296
-
297
304
# Default connection to local default instance
298
305
if(-not $Instance)
299
306
{
300
307
$Instance = $env:COMPUTERNAME
301
308
}
309
+ # Split Demarkation Start ^
310
+ # Parse computer name from the instance
311
+ $ComputerName = Get-ComputerNameFromInstance -Instance $Instance
312
+
313
+ if($IPRange -and $IPAddress)
314
+ {
315
+ if ($IPAddress.Contains(","))
316
+ {
317
+ $ContainsValid = $false
318
+ foreach ($IP in $IPAddress.Split(","))
319
+ {
320
+ if($(Test-Subnet -cidr $IPRange -ip $IP))
321
+ {
322
+ $ContainsValid = $true
323
+ }
324
+ }
325
+ if (-not $ContainsValid)
326
+ {
327
+ Write-Warning "Skipping $ComputerName ($IPAddress)"
328
+ $null = $TblResults.Rows.Add("$ComputerName","$Instance",'Out of Scope')
329
+ return
330
+ }
331
+ }
332
+
333
+ if(-not $(Test-Subnet -cidr $IPRange -ip $IPAddress))
334
+ {
335
+ Write-Warning "Skipping $ComputerName ($IPAddress)"
336
+ $null = $TblResults.Rows.Add("$ComputerName","$Instance",'Out of Scope')
337
+ return
338
+ }
339
+ Write-Verbose "$ComputerName ($IPAddress)"
340
+ }
302
341
303
342
# Setup DAC string
304
343
if($DAC)
@@ -410,6 +449,16 @@ Function Get-SQLConnectionTestThreaded
410
449
HelpMessage = 'SQL Server instance to connection to.')]
411
450
[string]$Instance,
412
451
452
+ [Parameter(Mandatory = $false,
453
+ ValueFromPipeline = $true,
454
+ ValueFromPipelineByPropertyName = $true,
455
+ HelpMessage = 'IP Address of SQL Server.')]
456
+ [string]$IPAddress,
457
+
458
+ [Parameter(Mandatory = $false,
459
+ HelpMessage = 'IP Address Range In CIDR Format to Audit.')]
460
+ [string]$IPRange,
461
+
413
462
[Parameter(Mandatory = $false,
414
463
HelpMessage = 'Connect using Dedicated Admin Connection.')]
415
464
[Switch]$DAC,
@@ -452,10 +501,15 @@ Function Get-SQLConnectionTestThreaded
452
501
if($Instance)
453
502
{
454
503
$ProvideInstance = New-Object -TypeName PSObject -Property @{
455
- Instance = $Instance
504
+ Instance = $Instance;
456
505
}
457
506
}
458
507
508
+ if($Instance -and $IPAddress)
509
+ {
510
+ $ProvideInstance | Add-Member -Name "IPAddress" -Value $IPAddress
511
+ }
512
+
459
513
# Add instance to instance list
460
514
$PipelineItems = $PipelineItems + $ProvideInstance
461
515
}
@@ -472,10 +526,40 @@ Function Get-SQLConnectionTestThreaded
472
526
$MyScriptBlock = {
473
527
# Setup instance
474
528
$Instance = $_.Instance
529
+ $IPAddress = $_.IPAddress
475
530
476
531
# Parse computer name from the instance
477
532
$ComputerName = Get-ComputerNameFromInstance -Instance $Instance
478
533
534
+ if($IPRange -and $IPAddress)
535
+ {
536
+ if ($IPAddress.Contains(","))
537
+ {
538
+ $ContainsValid = $false
539
+ foreach ($IP in $IPAddress.Split(","))
540
+ {
541
+ if($(Test-Subnet -cidr $IPRange -ip $IP))
542
+ {
543
+ $ContainsValid = $true
544
+ }
545
+ }
546
+ if (-not $ContainsValid)
547
+ {
548
+ Write-Warning "Skipping $ComputerName ($IPAddress)"
549
+ $null = $TblResults.Rows.Add("$ComputerName","$Instance",'Out of Scope')
550
+ return
551
+ }
552
+ }
553
+
554
+ if(-not $(Test-Subnet -cidr $IPRange -ip $IPAddress))
555
+ {
556
+ Write-Warning "Skipping $ComputerName ($IPAddress)"
557
+ $null = $TblResults.Rows.Add("$ComputerName","$Instance",'Out of Scope')
558
+ return
559
+ }
560
+ Write-Verbose "$ComputerName ($IPAddress)"
561
+ }
562
+
479
563
# Setup DAC string
480
564
if($DAC)
481
565
{
@@ -15660,6 +15744,11 @@ Function Get-SQLInstanceDomain
15660
15744
HelpMessage = 'Performs UDP scan of servers managing SQL Server clusters.')]
15661
15745
[switch]$CheckMgmt,
15662
15746
15747
+ [Parameter(Mandatory = $false,
15748
+ ValueFromPipelineByPropertyName = $true,
15749
+ HelpMessage = 'Preforms a DNS lookup on the instance.')]
15750
+ [switch]$IncludeIP,
15751
+
15663
15752
[Parameter(Mandatory = $false,
15664
15753
ValueFromPipelineByPropertyName = $true,
15665
15754
HelpMessage = 'Timeout in seconds for UDP scans of management servers. Longer timeout = more accurate.')]
@@ -15680,6 +15769,10 @@ Function Get-SQLInstanceDomain
15680
15769
$null = $TblSQLServerSpns.Columns.Add('LastLogon')
15681
15770
$null = $TblSQLServerSpns.Columns.Add('Description')
15682
15771
15772
+ if($IncludeIP)
15773
+ {
15774
+ $null = $TblSQLServerSpns.Columns.Add('IPAddress')
15775
+ }
15683
15776
# Table for UDP scan results of management servers
15684
15777
}
15685
15778
@@ -15713,17 +15806,35 @@ Function Get-SQLInstanceDomain
15713
15806
15714
15807
$SpnServerInstance = $SpnServerInstance -replace 'MSSQLSvc/', ''
15715
15808
15716
- # Add SQL Server spn to table
15717
- $null = $TblSQLServerSpns.Rows.Add(
15718
- [string]$_.ComputerName,
15809
+ $TableRow = @([string]$_.ComputerName,
15719
15810
[string]$SpnServerInstance,
15720
15811
$_.UserSid,
15721
15812
[string]$_.User,
15722
15813
[string]$_.Usercn,
15723
15814
[string]$_.Service,
15724
15815
[string]$_.Spn,
15725
15816
$_.LastLogon,
15726
- [string]$_.Description)
15817
+ [string]$_.Description)
15818
+
15819
+ if($IncludeIP)
15820
+ {
15821
+ try
15822
+ {
15823
+ $IPAddress = [Net.DNS]::GetHostAddresses([String]$_.ComputerName).IPAddressToString
15824
+ if($IPAddress -is [Object[]])
15825
+ {
15826
+ $IPAddress = $IPAddress -join ", "
15827
+ }
15828
+ }
15829
+ catch
15830
+ {
15831
+ $IPAddress = "0.0.0.0"
15832
+ }
15833
+ $TableRow += $IPAddress
15834
+ }
15835
+
15836
+ # Add SQL Server spn to table
15837
+ $null = $TblSQLServerSpns.Rows.Add($TableRow)
15727
15838
}
15728
15839
15729
15840
# Enumerate SQL Server instances from management servers
@@ -25109,6 +25220,22 @@ function Invoke-Parallel
25109
25220
}
25110
25221
25111
25222
25223
+ # Source: http://www.padisetty.com/2014/05/powershell-bit-manipulation-and-network.html
25224
+ # Notes: Changed name from checkSubnet to Test-Subnet (Approved Verbs)
25225
+ function Test-Subnet ([string]$cidr, [string]$ip)
25226
+ {
25227
+ $network, [int]$subnetlen = $cidr.Split('/')
25228
+ $a = [uint32[]]$network.split('.')
25229
+ [uint32] $unetwork = ($a[0] -shl 24) + ($a[1] -shl 16) + ($a[2] -shl 8) + $a[3]
25230
+
25231
+ $mask = (-bnot [uint32]0) -shl (32 - $subnetlen)
25232
+
25233
+ $a = [uint32[]]$ip.split('.')
25234
+ [uint32] $uip = ($a[0] -shl 24) + ($a[1] -shl 16) + ($a[2] -shl 8) + $a[3]
25235
+
25236
+ $unetwork -eq ($mask -band $uip)
25237
+ }
25238
+
25112
25239
25113
25240
#endregion
25114
25241
@@ -25964,7 +26091,6 @@ Function Invoke-SQLDumpInfo
25964
26091
25965
26092
Write-Verbose -Message "$Instance - END"
25966
26093
}
25967
-
25968
26094
End
25969
26095
{
25970
26096
}
0 commit comments