Skip to content

Instantly share code, notes, and snippets.

@rchaganti
Created July 31, 2015 11:52
Show Gist options
  • Select an option

  • Save rchaganti/8d3cbe1e84bffbc6017b to your computer and use it in GitHub Desktop.

Select an option

Save rchaganti/8d3cbe1e84bffbc6017b to your computer and use it in GitHub Desktop.
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