Skip to content

Commit 981e637

Browse files
AlexanderAlexander
authored andcommitted
Added Range Restriction to Get-SQLConnectionTest
1 parent d5b1f1e commit 981e637

1 file changed

Lines changed: 53 additions & 1 deletion

File tree

PowerUpSQL.ps1

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,16 @@ Function Get-SQLConnectionTest
263263
HelpMessage = 'SQL Server instance to connection to.')]
264264
[string]$Instance,
265265

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 to Audit.')]
274+
[string]$IPRange,
275+
266276
[Parameter(Mandatory = $false,
267277
HelpMessage = 'Connect using Dedicated Admin Connection.')]
268278
[Switch]$DAC,
@@ -294,6 +304,33 @@ Function Get-SQLConnectionTest
294304
# Parse computer name from the instance
295305
$ComputerName = Get-ComputerNameFromInstance -Instance $Instance
296306

307+
if($IPRange -and $IPAddress)
308+
{
309+
if ($IPAddress.Contains(","))
310+
{
311+
$ContainsValid = $false
312+
foreach ($IP in $IPAddress.Split(","))
313+
{
314+
if($(Test-Subnet -cidr $IPRange -ip $IP))
315+
{
316+
$ContainsValid = $true
317+
}
318+
}
319+
if (-not $ContainsValid)
320+
{
321+
Write-Warning "Skipping $ComputerName ($IPAddress)"
322+
return
323+
}
324+
}
325+
326+
if(-not $(Test-Subnet -cidr $IPRange -ip $IPAddress))
327+
{
328+
Write-Warning "Skipping $ComputerName ($IPAddress)"
329+
return
330+
}
331+
Write-Verbose "$ComputerName ($IPAddress)"
332+
}
333+
297334
# Default connection to local default instance
298335
if(-not $Instance)
299336
{
@@ -24686,6 +24723,22 @@ function Invoke-Parallel
2468624723
}
2468724724

2468824725

24726+
# Source: http://www.padisetty.com/2014/05/powershell-bit-manipulation-and-network.html
24727+
# Notes: Changed name from checkSubnet to Test-Subnet (Approved Verbs)
24728+
function Test-Subnet ([string]$cidr, [string]$ip)
24729+
{
24730+
$network, [int]$subnetlen = $cidr.Split('/')
24731+
$a = [uint32[]]$network.split('.')
24732+
[uint32] $unetwork = ($a[0] -shl 24) + ($a[1] -shl 16) + ($a[2] -shl 8) + $a[3]
24733+
24734+
$mask = (-bnot [uint32]0) -shl (32 - $subnetlen)
24735+
24736+
$a = [uint32[]]$ip.split('.')
24737+
[uint32] $uip = ($a[0] -shl 24) + ($a[1] -shl 16) + ($a[2] -shl 8) + $a[3]
24738+
24739+
$unetwork -eq ($mask -band $uip)
24740+
}
24741+
2468924742

2469024743
#endregion
2469124744

@@ -25541,7 +25594,6 @@ Function Invoke-SQLDumpInfo
2554125594

2554225595
Write-Verbose -Message "$Instance - END"
2554325596
}
25544-
2554525597
End
2554625598
{
2554725599
}

0 commit comments

Comments
 (0)