Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $list = New-Object System.Collections.Generic.List[object](4)
- $maxRunspaces = 20
- $servers = Get-ADDomainController -Filter *
- $testPorts = @(389, 636, 3268, 3269)
- # Set up the runspace pool and powershell script
- $rsPool = [runspacefactory]::CreateRunspacePool(1, $maxRunspaces)
- $rsPool.ApartmentState = "STA"
- $rsPool.ThreadOptions = "ReuseThread"
- $rsPool.Open()
- $code = {
- param ($Server, $Port)
- Test-NetConnection -ComputerName $Server -Port $Port
- }
- # Add a runspace'd powershell instance for every server on each specified port
- foreach ($s in $servers) {
- foreach ($port in $testPorts) {
- $psInst = [powershell]::Create().AddScript($code).AddParameter("Server", $s.HostName).AddParameter("Port", $port)
- $psInst.RunspacePool = $rsPool
- $list.Add([pscustomobject]@{
- PowerShell = $psInst
- Runspace = $psInst.BeginInvoke()
- })
- }
- }
- $allResults = New-Object System.Collections.Generic.List[psobject]($servers.Count * 4)
- # As long as there is one job still left in the list, keep looping and checking for completed jobs.
- # Add the results from each job into a final list.
- while ($list.Count -gt 0) {
- for ($i = $list.Count - 1; $i -ge 0; $i--) {
- $job = $list[$i]
- if ($job.Runspace.IsCompleted) {
- $results = $job.PowerShell.EndInvoke($job.Runspace)
- $job.PowerShell.Dispose()
- [void] $list.Remove($job)
- if ($results.Count -gt 0) {
- $allResults.AddRange($results)
- }
- }
- }
- Start-Sleep -Milliseconds 200
- }
- $allResults
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement