Today, a friend asked me how can I ping tens of thousands of machines in PowerShell? The default test-connection Although there is-computer this parameter, his way is in order to ping, all run down may have several hours.
For example, I need to spend 18 seconds to ping 40 servers, if thousands of words will be time-consuming.
measure-commnd-expression {get-adcomputer-filter {operatingsystem-like "*2012*"} | Foreach-object {test-connection-computername $_.name-count 1}}
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7E/92/wKiom1cErB7jvpi9AAAakf0JJIs452.png "title=" 0.PNG " alt= "Wkiom1cerb7jvpi9aaaakf0jjis452.png"/>
Prior to this, the use of multi-threaded beans is limited to understand that Invoke-command can operate on 30 objects at the same time, after some learning, and finally found that there are other advanced methods.
In PowerShell, there are probably two ways to use multithreading.
The first is a job that creates multiple backgrounds. This approach creates a background job through start-job or-asjob, then obtains the current task through the Get-job, obtains the result of completing the task through the Receive-job, and finally remove-job to free the memory. The disadvantage is obviously more troublesome. And once more to run a few jobs gray often gray card!
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7E/8F/wKioL1cEr4qx_OieAAB0fPHYMxI751.png "title=" 4.PNG " alt= "Wkiol1cer4qx_oieaab0fphymxi751.png"/>
The second way is to create multiple runspace, which works like Invoke-command, with each remote session bound to a runspace. We can create a runspace pool that specifies the maximum number of runspace that can be executed at the same time in this resource pool.
Beans mainly refer to the method of this blog, wrote a simple script.
http://thesurlyadmin.com/2013/02/11/multithreading-powershell-scripts/
$Throttle = 20 #threads # script block, sends an ICMP packet test to the specified computer, and saves the result in an object $ScriptBlock = { Param ( [string] $Computer ) $a =test-connection -computername $Computer -Count 1 $RunResult = New-Object PSObject -Property @{ ipv4adress= $a. ipv4address.ipaddresstostring computername= $Computer } Return $RunResult}# Create a resource pool, specifying how many runspace can be executed concurrently $runspacepool = [runspacefactory]::createrunspacepool (1, $Throttle) $ Runspacepool.open () $Jobs = @ () #获取Windows The information for the  2012 server, create a separate job for each server, the job performs ICMP tests, and save the result in a PS object (get-adcomputer -filter {operatingsystem -like "*2012*"}). name | % { #Start-sleep -seconds 1 $Job = [powershell]::create (). Addscript ($ScriptBlock). Addargument ($_) $Job .runspacepool = $RunspacePool $Jobs += new-object psobject -property @{ server = $_ Pipe = $Job result = $Job. BeginInvoke () }} #循环输出等待的信息 .... until all jobs are complete write-host "Waiting ..." -NoNewlineDo { Write-Host "." -NoNewline Start-Sleep -Seconds 1} While ( $ jobs.result.iscompleted -contains $false) write-host "all jobs completed!" #输出结果 $Results = @ () foreach ($Job in $Jobs) { $Results += $Job. Pipe.endinvoke ($Job. Result)} $Results
After about 5 seconds, the results came out. If you are interested, you can use the Measure-command command to test the effect of the number of different threads, according to my test, 30 processes executed at the same time only 4 seconds out of the results, and 2 simultaneous execution takes about 9 seconds to produce results.
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7E/92/wKiom1cEqO_A3RQsAAA4ExOCLcs103.png "title=" 1.PNG " alt= "Wkiom1ceqo_a3rqsaaa4exoclcs103.png"/>
Once you know the principle, you can further refine and abstract the script. This has already been done. Https://github.com/RamblingCookieMonster/Invoke-Parallel/blob/master/Invoke-Parallel/Invoke-Parallel.ps1
Download, unlock and dot source can be called directly after. Here are some examples for reference https://github.com/RamblingCookieMonster/Invoke-Parallel
According to gourd painting scoop, I want to call test-connection through him is also successful
Get-adcomputer-filter {operatingsystem-like "*2012*"} | Select-expandproperty name | Invoke-parallel-scriptblock {test-connection-computername $_-count 1}
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7E/92/wKiom1cEqqyA47xvAAAzB3SlH5Y854.png "title=" 2.PNG " alt= "Wkiom1ceqqya47xvaaazb3slh5y854.png"/>
Finally, there is a ready-made script on the web for concurrent test pings, and the principle is to invoke the Invoke-parallel function above, but he also added other functions to test whether the remote access ports such as RDP,WINRM,RPC are open, further extending the functionality. Can be downloaded directly from here
http://ramblingcookiemonster.github.io/Invoke-Ping/
Invoke-ping-computername (Get-adcomputer-filter {operatingsystem-like "*2012*"}). Name-detail RDP,rpc | Ft-wrap
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7E/8F/wKioL1cErtWwJ0l0AABCM4b7Lm8565.png "title=" 3.PNG " alt= "Wkiol1certwwj0l0aabcm4b7lm8565.png"/>
This article is from the "Mapo Tofu" blog, please be sure to keep this source http://beanxyz.blog.51cto.com/5570417/1760880
PowerShell Multi-Threading usage