Created
July 31, 2015 11:52
-
-
Save rchaganti/8d3cbe1e84bffbc6017b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Function Get-AvailableIPv4Address { | |
| param ( | |
| [System.Collections.ArrayList] $IPRange | |
| ) | |
| $MaxThreads = 30 | |
| $ScriptBlock = { | |
| Param ( | |
| [String]$IPAddress | |
| ) | |
| Test-Connection -ComputerName $IPAddress -Count 1 -Quiet | |
| } | |
| $RunspacePool = [RunspaceFactory]::CreateRunspacePool(1,$MaxThreads) | |
| $RunspacePool.Open() | |
| $Jobs = @() | |
| $IPRange | % { | |
| $Job = [powershell]::Create().AddScript($ScriptBlock).AddArgument($_) | |
| $Job.RunspacePool = $RunspacePool | |
| $Jobs += New-Object PSObject -Property @{ | |
| IPAddress = $_ | |
| Pipe = $Job | |
| Result = $Job.BeginInvoke() | |
| } | |
| } | |
| While ( $Jobs.Result.IsCompleted -contains $false) { | |
| Start-Sleep -Seconds 1 | |
| } | |
| $Results = @() | |
| ForEach ($Job in $Jobs) | |
| { | |
| if (-not $Job.Pipe.EndInvoke($Job.Result)) { | |
| $job.IPAddress | |
| } | |
| } | |
| } | |
| $IPRange = New-Object System.Collections.ArrayList | |
| 1 .. 254 | % { $IPRange.Add("192.168.10.$_") | Out-Null } | |
| $IPRange = @(Get-AvailableIPv4Address -IPRange $IPRange) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment