Get-PowerIPRange.ps1
<#
.SYNOPSIS Generates IPv4 address lists with the provided Start and End IPv4 address. Address lists are generated via string manipulation. .DESCRIPTION Function utilized by Test-PowerPing to generate IPv4 Address lists. .NOTES Author: Hunter Hirsch #> function Get-PowerIPRange { [CmdletBinding()] [OutputType([System.Collections.ArrayList])] Param ( [Parameter(Mandatory,ValueFromPipeline)] [string] $Start, [Parameter(Mandatory,ValueFromPipeline)] [string] $End, [Parameter(ValueFromPipeline)] [string[]] $Filter, [Parameter(ValueFromPipeline)] [int] $MT ) Begin { Try{ $starr = $Start.Split('.') $edarr = $End.split('.') $sbase = "" for (($i=0); $starr[$i] -eq $edarr[$i]; $i++){ [string]$nod = $starr[$i] if ($sbase -eq ""){ $sbase = $nod + '.' } Else{ $sbase = $sbase + $nod + '.' } } if ($sbase -ne ""){ $rbase = ($sbase -replace '\d','').Length } Else{ $rbase = '0' } [int]$ri=$starr[0] [int]$rj=$starr[1] [int]$rk=$starr[2] [int]$rl=$starr[3] [int]$eri=$edarr[0] [int]$erj=$edarr[1] [int]$erk=$edarr[2] [int]$erl=$edarr[3] [string]$adbal = ($eri - $ri),($erj - $rj),($erk - $rk),($erl - $rl) $adbal = $adbal.Replace(" ",'') if ($adbal[0] -eq '-' -or $adbal -match "^[0]{1,3}-"){ $starr = $endip.split('.') $edarr = $startip.split('.') [int]$ri=$starr[0] [int]$rj=$starr[1] [int]$rk=$starr[2] [int]$rl=$starr[3] [int]$eri=$edarr[0] [int]$erj=$edarr[1] [int]$erk=$edarr[2] [int]$erl=$edarr[3] } if ($ri -gt '255' -or $rj -gt '255' -or $rk -gt '255' -or $rl -gt '255' -or $eri -gt '255' -or $erj -gt '255' -or $erk -gt '255' -or $erl -gt '255' -or $ri -lt '0' -or $rj -lt '0' -or $rk -lt '0' -or $rl -lt '0' -or $eri -lt '0' -or $erj -lt '0' -or $erk -lt '0' -or $erl -lt '0'){ Write-Output "Octets in IPv4 addresses cannot exceed '255' or be less than '0'. Check Entry and Try Again." return } $MTP = $false if ($rbase -eq '0'){ [int]$jdiff = $erj +1 [int]$ijdiff = 256 - $rj [int]$idiff = $eri - $ri [int]$tdiff = $erj + $ijdiff +1 } Elseif($rbase -eq '1'){ [int]$jdiff = $erj -$rj +1 } if ($MT -and ($jdiff -gt '3' -or $idiff -gt '1' -or $tdiff -gt '3')){ $MTP = $true Get-PIPRMT return } if ($Filter -and !$MTP){ Filter ssoct { if ($soctl -notcontains $_){ $_ } } $soctl = new-object System.Collections.ArrayList foreach ($f in $Filter){ if ($f.length -le '3'){ $null = $soctl.Add("$f") } } if ($soctl.Count -gt '0'){ $rngf = 0..255 | ssoct } filter ssadr { $srem = '' foreach ($p in $sadrl){ if ($_ -like $p){ $srem = $_ } } if ($srem -ne $_){ $_ } } $sadrl = New-Object System.Collections.ArrayList foreach ($f in $Filter){ if ($f -like '*.*.*.*'){ $null = $sadrl.add("$f") } } } $address = [System.Collections.ArrayList]::new() } Catch{ $address = $null Throw $_.Exception.errorrecord } } Process{ Try{ if($MTP -eq $false){ [int]$lis=0 [int]$lie=0 if ($rbase -eq '3'){ if (!$soctl){ $rng = $rl..$erl } Else{ $rng = $rl..$erl | ssoct } if (!$sadrl){ $rng | ForEach-Object {$null = $address.Add("$sbase"+"$_")} } Else{ $rng | ForEach-Object {"$sbase"+"$_"} | ssadr | ForEach-Object {$null = $address.Add("$_")} } } Elseif ($rbase -eq '2'){ if(!$soctl){ $rng=$rl..255 } Else{ $rng=$rl..255 | ssoct } $sbase = "$sbase"+"$rk"+'.' if (!$sadrl){ $rng | ForEach-Object {$null = $address.Add("$sbase"+"$_")} } Else{ $rng | ForEach-Object {"$sbase"+"$_"} | ssadr | ForEach-Object {$null = $address.Add("$_")} } $rk++ $sbase = "$ri"+'.'+"$rj"+'.' if (!$soctl){ $rng = 0..255 } Else{ $rng = $rngf } get-adloop $sbase = "$eri"+'.'+"$erj"+'.'+"$erk"+'.' if (!$soctl){ $rng= 0..$erl } Else{ $rng = 0..$erl | ssoct } if (!$sadrl){ $rng | ForEach-Object {$null = $address.Add("$sbase"+"$_")} } Else{ $rng | ForEach-Object {"$sbase"+"$_"} | ssadr | ForEach-Object {$null = $address.Add("$_")} } } Elseif ($rbase -eq '1'){ if (!$soctl){ $rng = $rl..255 } Else{ $rng = $rl..255 | ssoct } $sbase = "$sbase"+"$rj"+'.'+"$rk"+'.' if (!$sadrl){ $rng | ForEach-Object {$null = $address.Add("$sbase"+"$_")} } Else{ $rng | ForEach-Object {"$sbase"+"$_"} | ssadr | ForEach-Object {$null = $address.Add("$_")} } $sbase = "$ri"+'.' if ($rk -lt 255){ $rk++ } Else{ $rk=0 $rj++ } if (!$soctl){ $rng = 0..255 } Else{ $rng = $rngf } get-adloop $sbase = "$eri"+'.'+"$erj"+'.'+"$erk"+'.' if (!$soctl){ $rng= 0..$erl } Else{ $rng = 0..$erl | ssoct } if (!$sadrl){ $rng | ForEach-Object {$null = $address.Add("$sbase"+"$_")} } Else{ $rng | ForEach-Object {"$sbase"+"$_"} | ssadr | ForEach-Object {$null = $address.Add("$_")} } } Elseif ($rbase -eq '0'){ $sbase = "$ri"+'.'+"$rj"+'.'+"$rk"+'.' if (!$soctl){ $rng= $rl..255 } Else{ $rng= $rl..255 | ssoct } if (!$sadrl){ $rng | ForEach-Object {$null = $address.Add("$sbase"+"$_")} } Else{ $rng | ForEach-Object {"$sbase"+"$_"} | ssadr | ForEach-Object {$null = $address.Add("$_")} } if ($rk -lt 255){ $rk++ } Elseif($rk -eq 255 -and $rj -lt 255){ $rk=0 $rj++ } Elseif($rk -eq 255 -and $rj -eq 255){ $rj=0 $rk=0 $ri++ } $rbase = '1' if (!$soctl){ $rng = 0..255 } Else{ $rng = $rngf } $erj = 255 $erk = 255 $erl = 255 while ($ri -lt $eri){ $sbase = "$ri"+'.' get-adloop $sbase = "$ri"+'.'+"$erj"+'.'+"$erk"+'.' if (!$sadrl){ $rng | ForEach-Object {$null = $address.Add("$sbase"+"$_")} } Else{ $rng | ForEach-Object {"$sbase"+"$_"} | ssadr | ForEach-Object {$null = $address.Add("$_")} } $rk=0 $rj=0 $ri++ } $sbase = "$ri"+'.' [int]$erj=$edarr[1] [int]$erk=$edarr[2] [int]$erl=$edarr[3] get-adloop $sbase = "$eri"+'.'+"$erj"+'.'+"$erk"+'.' if (!$soctl){ $rng= 0..$erl } Else{ $rng= 0..$erl | ssoct } if (!$sadrl){ $rng | ForEach-Object {$null = $address.Add("$sbase"+"$_")} } Else{ $rng | ForEach-Object {"$sbase"+"$_"} | ssadr | ForEach-Object {$null = $address.Add("$_")} } } } } Catch{ $address = $null Throw $_.Exception.errorrecord } } End { if (!($MTP)){ $address } if ($Filter){ $soctl = $null;$sadrl = $null } $address = $null;$rng = $null } } function get-adloop{ while ($lis -le $lie){ if ($rbase -eq '2'){ While ($rk -lt $erk){ if (!$sadrl){ $null = $address.AddRange(($rng | ForEach-Object {"$sbase"+"$rk"+'.'+"$_"})) } Else{ $rng | ForEach-Object {"$sbase"+"$rk"+'.'+"$_"} | ssadr | ForEach-Object {$null = $address.Add($_)} } $rk++ } $lis++ } Elseif ($rbase -eq '1'){ While ($rj -lt $erj){ $sbase = "$sbase"+"$rj"+'.' while ($rk -lt 255){ if (!$sadrl){ $null = $address.AddRange(($rng | ForEach-Object {"$sbase"+"$rk"+'.'+"$_"})) } Else{ $rng | ForEach-Object {"$sbase"+"$rk"+'.'+"$_"} | ssadr | ForEach-Object {$null = $address.Add($_)} } $rk++ } if (!$sadrl){ $null = $address.AddRange(($rng | ForEach-Object {"$sbase"+"$rk"+'.'+"$_"})) } Else{ $rng | ForEach-Object {"$sbase"+"$rk"+'.'+"$_"} | ssadr | ForEach-Object {$null = $address.Add($_)} } $rk = 0 $sbase = "$ri"+'.' $rj++ } $sbase = "$sbase"+"$rj"+'.' $rbase = '2' } } } function get-mtadloop{ while ($lis -le $lie){ if ($rbase -eq '2'){ While ($rk -lt $erk){ if (!$msadrl){ $null = $adhash.$rsc.AddRange(($rng | ForEach-Object {"$sbase"+"$rk"+'.'+"$_"})) } Else{ $rng | ForEach-Object {"$sbase"+"$rk"+'.'+"$_"} | ForEach-Object { $srem = '' foreach ($p in $msadrl){ if ($_ -like $p){ $srem = $_ } } if ($srem -ne $_){ $_ } } | ForEach-Object {$null = $adhash.$rsc.add($_)} } $rk++ } $lis++ } Elseif ($rbase -eq '1'){ While ($rj -lt $erj){ $sbase = "$sbase"+"$rj"+'.' while ($rk -lt 255){ if (!$msadrl){ $null = $adhash.$rsc.AddRange(($rng | ForEach-Object {"$sbase"+"$rk"+'.'+"$_"})) } Else{ $rng | ForEach-Object {"$sbase"+"$rk"+'.'+"$_"} | ForEach-Object { $srem = '' foreach ($p in $msadrl){ if ($_ -like $p){ $srem = $_ } } if ($srem -ne $_){ $_ } } | ForEach-Object {$null = $adhash.$rsc.add($_)} } $rk++ } if (!$msadrl){ $null = $adhash.$rsc.AddRange(($rng | ForEach-Object {"$sbase"+"$rk"+'.'+"$_"})) } Else{ $rng | ForEach-Object {"$sbase"+"$rk"+'.'+"$_"} | ForEach-Object { $srem = '' foreach ($p in $msadrl){ if ($_ -like $p){ $srem = $_ } } if ($srem -ne $_){ $_ } } | ForEach-Object {$null = $adhash.$rsc.add($_)} } $rk = 0 $sbase = "$ri"+'.' $rj++ } $sbase = "$sbase"+"$rj"+'.' $rbase = '2' } } } function Get-PIPRMT { Begin { Try{ $MTC = $false [int]$rii = $ri [int]$rji = ($rj + 1) [int]$rjj = ($rji + 2) if (($rbase -eq '0' -and $tdiff -gt '3') -or $idiff -gt '1'){ $rsranges = [System.Collections.ArrayList]::new() if (($tdiff -ge '2' -or $idiff -gt '1') -and $ijdiff -lt '2'){ $srng = ("$ri"+'.'+"$rj"+'.'+"$rk"+'.'+"$rl")+','+("$ri"+'.'+"255"+'.'+'255'+'.'+'255') $null = $rsranges.Add($srng) $rii++ } while ($rii -lt $eri){ if ($rii -eq $starr[0]){ $srng = ("$ri"+'.'+"$rj"+'.'+"$rk"+'.'+"$rl")+','+("$ri"+'.'+"$rji"+'.'+'255'+'.'+'255') $null = $rsranges.Add($srng) if ($rjj -le '255'){ while ($rjj -le '255'){ $rjiu = ($rji +1) $rjj = $rji + 2 if ($rjj -lt '255'){ $null = $rsranges.Add((("$ri"+'.'+"$rjiu"+'.'+'0'+'.'+'0')+','+("$ri"+'.'+"$rjj"+'.'+'255'+'.'+'255'))) $rji = $rjj } Elseif ($rjj -ge '255'){ $null = $rsranges.Add((("$ri"+'.'+"$rjiu"+'.'+'0'+'.'+'0')+','+("$ri"+'.'+"255"+'.'+"255"+'.'+"255"))) $rjj = '256' } } } Else{ $rjiu = ($rji +1) $erng = ("$ri"+'.'+"$rjiu"+'.'+'0'+'.'+'0')+','+("$ri"+'.'+"255"+'.'+"255"+'.'+"255") $null = $rsranges.Add($erng) } $rii ++ } if ($rii -gt $starr[0] -and $rii -lt $eri){ $rji = 1 $rjj = 3 $srng = ("$rii"+'.'+"0"+'.'+"0"+'.'+"0")+','+("$rii"+'.'+"$rji"+'.'+'255'+'.'+'255') $null = $rsranges.Add($srng) while ($rjj -le '255'){ $rjj = ($rji +4) $rjiu = ($rji +1) if ($rjj -lt '255'){ $null = $rsranges.Add((("$rii"+'.'+"$rjiu"+'.'+'0'+'.'+'0')+','+("$rii"+'.'+"$rjj"+'.'+'255'+'.'+'255'))) $rji = $rjj } Elseif ($rjj -ge '255'){ $null = $rsranges.Add((("$rii"+'.'+"$rjiu"+'.'+'0'+'.'+'0')+','+("$rii"+'.'+"255"+'.'+"255"+'.'+"255"))) $rjj = '256' } } $rii++ } } if ($erj -gt 2){ $rji = 1 $rjj = 3 $ek = '255' $el = '255' } Else{ $rji = $erj $ek = $erk $el = $erl } $srng = ("$rii"+'.'+"0"+'.'+"0"+'.'+"0")+','+("$rii"+'.'+"$rji"+'.'+"$ek"+'.'+"$el") $null = $rsranges.Add($srng) if ($rjj -lt $erj){ while ($rjj -lt $erj){ $rjj = ($rji +4) $rjiu = ($rji +1) if ($rjj -lt $erj){ $null = $rsranges.Add((("$rii"+'.'+"$rjiu"+'.'+'0'+'.'+'0')+','+("$rii"+'.'+"$rjj"+'.'+'255'+'.'+'255'))) $rji = $rjj } } } if ($erj -gt 2){ $rjiu = ($rji +1) $erng = ("$rii"+'.'+"$rjiu"+'.'+'0'+'.'+'0')+','+("$rii"+'.'+"$erj"+'.'+"$erk"+'.'+"$erl") $null = $rsranges.Add($erng) } } if ($rbase -eq '1' -and $jdiff -gt 3){ $srng = ("$rii"+'.'+"$rj"+'.'+"$rk"+'.'+"$rl")+','+("$ri"+'.'+"$rji"+'.'+'255'+'.'+'255') $rsranges = [System.Collections.ArrayList]::new() $null = $rsranges.Add($srng) if ($rjj -lt $erj){ while ($rjj -lt $erj){ $rjj = ($rji +2) $rjiu = ($rji +1) if ($rjj -lt $erj){ $null = $rsranges.Add((("$ri"+'.'+"$rjiu"+'.'+'0'+'.'+'0')+','+("$ri"+'.'+"$rjj"+'.'+'255'+'.'+'255'))) $rji = $rjj } } } if ($rjj -ge $erj){ $rjiu = ($rji +1) if ($rjiu -le $erj){ $erng = ("$ri"+'.'+"$rjiu"+'.'+'0'+'.'+'0')+','+("$ri"+'.'+"$erj"+'.'+"$erk"+'.'+"$erl") $null = $rsranges.Add($erng) } } } $adhash = [hashtable]::Synchronized(@{}) if ($Filter){ Filter ssoct { if ($soctl -notcontains $_){ $_ } } $soctl = new-object System.Collections.ArrayList foreach ($f in $Filter){ if ($f.length -le '3'){ $null = $soctl.Add("$f") } } if ($soctl.Count -gt '0'){ $rngf = 0..255 | ssoct } $msadrl = new-object System.Collections.ArrayList foreach ($f in $Filter){ if ($f.Length -gt '3'){ $null = $msadrl.Add($f) } } filter ssadr { $srem = '' foreach ($p in $msadrl){ if ($_ -like $p){ $srem = $_ } } if ($srem -ne $_){ $_ } } } if ($rsranges.Count -gt '3'){ $MTC = $true $loop = get-content function:\get-mtadloop $sloop = new-object System.Management.Automation.Runspaces.SessionStateFunctionEntry -ArgumentList 'get-mtadloop',$loop $iss = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault2() $iss.Commands.Add($sloop) $apool = [RunspaceFactory]::CreateRunspacePool(1,($MT+1),$iss,$Host) $apool.ApartmentState = "MTA" $apool.ThreadOptions = "ReuseThread" $apool.Open() $arunspaces = @() $ablock = { Param ($str,$ste,$ri,$rj,$rk,$erj,$erk,$rbase,$sbase,$rng,$rsc,$msadrl,$soctl,$adhash) [int]$rj = $str[1] [int]$rk = $str[2] [int]$erj = $ste[1] [int]$erk = $ste[2] [int]$ri = $str[0] $rbase = '1' $sbase = "$ri"+'.' get-mtadloop $sbase = $ste[0]+'.'+$ste[1]+'.'+$ste[2]+'.' if ($soctl.Count -eq '0'){ $rng = 0..[int]$ste[3] } Else{ $rng = 0..[int]$ste[3] | ForEach-Object {if ($soctl -notcontains $_){$_}} } if ($msadrl.Count -eq 0){ $null = $adhash.$rsc.AddRange(($rng | ForEach-Object{"$sbase"+"$_"})) } Else{ $rng | ForEach-Object {"$sbase"+"$_"} | ForEach-Object { $srem = '' foreach ($p in $msadrl){ if ($_ -like $p){ $srem = $_ } } if ($srem -ne $_){ $_ } } | ForEach-Object {$null = $adhash.$rsc.add($_)} } return } } } Catch{ if ($rsranges.Count -gt '3'){ $apool.Close() $apool.Dispose() } $rsranges = $null $adhash = $null Throw $_.Exception.errorrecord } } Process{ Try{ [int]$lis=0 [int]$lie=0 [int]$rsc = 0 $adhash.add($rsc,([System.Collections.ArrayList]::new())) $str = $rsranges[0].Split(',')[0].split('.') $ste = $rsranges[0].Split(',')[1].split('.') if (!$soctl){ $rng = [int]$str[3]..255 } Else{ $rng = [int]$str[3]..255 | ssoct } $sbase = $str[0]+'.'+$str[1]+'.'+$str[2]+'.' if (!$msadrl){ $rng | ForEach-Object {$null = $adhash.$rsc.add("$sbase"+"$_")} } Else{ $rng | ForEach-Object {"$sbase"+"$_"} | ssadr | ForEach-Object {$null = $adhash.$rsc.add($_)} } [int]$rj = $str[1] [int]$rk = $str[2] [int]$erj = $ste[1] [int]$erk = $ste[2] [int]$ri = $str[0] if ($rk -lt 255){ $rk++ } Else{ $rk=0 $rj++ } if (($str -join '.') -ne ($ste -join '.')){ if (!$soctl){ $rng = 0..255 } Else{ $rng = $rngf } $sbase = "$ri"+'.' $rbase = '1' get-mtadloop $sbase = $ste[0]+'.'+$ste[1]+'.'+$ste[2]+'.' if (!$soctl){ $rng = 0..[int]$ste[3] } Else{ $rng = 0..[int]$ste[3] | ssoct } if (!$msadrl){ $rng | ForEach-Object {$null = $adhash.$rsc.add("$sbase"+"$_")} } Else{ $rng | ForEach-Object {"$sbase"+"$_"} | ssadr | ForEach-Object {$null = $adhash.$rsc.add($_)} } } $rsc++ if ($rsranges.Count -gt '3'){ [int]$ei = $rsranges.count -2 foreach ($rg in $rsranges[1..$ei]){ $adhash.add($rsc,([System.Collections.ArrayList]::new())) $str = $rg.Split(',')[0].split('.') $ste = $rg.Split(',')[1].split('.') $runspace = [PowerShell]::Create() $null = $runspace.AddScript($ablock) $null = $runspace.AddArgument($str) $null = $runspace.AddArgument($ste) $null = $runspace.AddArgument($ri) $null = $runspace.AddArgument($rj) $null = $runspace.AddArgument($rk) $null = $runspace.AddArgument($erj) $null = $runspace.AddArgument($erk) $null = $runspace.AddArgument($rbase) $null = $runspace.AddArgument($sbase) $null = $runspace.AddArgument($rng) $null = $runspace.AddArgument($rsc) $null = $runspace.AddArgument($msadrl) $null = $runspace.AddArgument($soctl) $null = $runspace.AddArgument($adhash) $runspace.RunspacePool = $apool $arunspaces += [PSCustomObject]@{Pipe = $runspace; Status = $runspace.BeginInvoke()} $rsc++ } while ($arunspaces.Status.IsCompleted -contains $false) { $arunspaces.Where({$_.Status.IsCompleted -eq $true}) | ForEach-Object{ $_.Pipe.EndInvoke($_.Status) $_.Pipe.Dispose() $_.Pipe = $null $_.Status = $null $_ = $null } Start-Sleep -Milliseconds 100 } } Else{ $adhash.add($rsc,([System.Collections.ArrayList]::new())) $str = $rsranges[1].Split(',')[0].split('.') $ste = $rsranges[1].Split(',')[1].split('.') [int]$rj = $str[1] [int]$rk = $str[2] [int]$erj = $ste[1] [int]$erk = $ste[2] [int]$ri = $str[0] if (!$soctl){ $rng = 0..255 } Else{ $rng = $rngf } $sbase = $str[0]+'.' get-mtadloop $sbase = $ste[0]+'.'+$ste[1]+'.'+$ste[2]+'.' if (!$soctl){ $rng = 0..[int]$ste[3] } Else{ $rng = 0..[int]$ste[3] | ssoct } if (!$msadrl){ $rng | ForEach-Object {$null = $adhash.$rsc.add("$sbase"+"$_")} } Else{ $rng | ForEach-Object {"$sbase"+"$_"} | ssadr | ForEach-Object {$null = $adhash.$rsc.add($_)} } $rsc++ } if ($rsranges.count -gt 2){ $adhash.add($rsc,([System.Collections.ArrayList]::new())) [int]$ei = [int]$rsranges.count -1 $str = $rsranges[$ei].Split(',')[0].split('.') $ste = $rsranges[$ei].Split(',')[1].split('.') [int]$rj = $str[1] [int]$rk = $str[2] [int]$erj = $ste[1] [int]$erk = $ste[2] [int]$ri = $str[0] if (!$soctl){ $rng = 0..255 } Else{ $rng = $rngf } $sbase = $str[0]+'.' get-mtadloop $sbase = $ste[0]+'.'+$ste[1]+'.'+$ste[2]+'.' if (!$soctl){ $rng = 0..[int]$ste[3] } Else{ $rng = 0..[int]$ste[3] | ssoct } if (!$msadrl){ $rng | ForEach-Object {$null = $adhash.$rsc.add("$sbase"+"$_")} } Else{ $rng | ForEach-Object {"$sbase"+"$_"} | ssadr | ForEach-Object {$null = $adhash.$rsc.add($_)} } } } Catch{ $rsranges = $null $adhash = $null Throw $_.Exception.errorrecord } Finally{ if ($MTC){ $arunspaces = $null if (!$apool.IsDisposed){ $apool.Close() $apool.Dispose() } } } } End { Try{ $rng = 0..($adhash.count -1) $rng | ForEach-Object {$adhash.$_} } Catch{ Throw $_.Exception.errorrecord } Finally{ if ($MTC){ $apool = $null; $loop = $null; $sloop = $null; $soctl = $null; $msadrl = $null } $adhash = $null; $rsranges = $null; $ablock = $null } } } |