Multithreading 如何使用多线程脚本
我在上找到了这个多线程脚本 但我不知道如何使用它。特别是如何使用$ObjectList变量。我想用我写的这个ping脚本ping一个100000台机器的列表。。。但以目前的形式,它每分钟发出大约100台机器的声音。因此,完成这项工作需要16个多小时Multithreading 如何使用多线程脚本,multithreading,powershell,Multithreading,Powershell,我在上找到了这个多线程脚本 但我不知道如何使用它。特别是如何使用$ObjectList变量。我想用我写的这个ping脚本ping一个100000台机器的列表。。。但以目前的形式,它每分钟发出大约100台机器的声音。因此,完成这项工作需要16个多小时 $Computers = Get-Content -Path C:\Temp\Comps.txt foreach ($Computer in $Computers) { if (test-Connection -ComputerName
$Computers = Get-Content -Path C:\Temp\Comps.txt
foreach ($Computer in $Computers) {
if (test-Connection -ComputerName $Computer -Count 1) {
"$Computer is Pinging"
}
Else {
"$Computer is not Pinging"
}
}
关于如何将ping脚本与多线程脚本集成以产生更快的结果,有什么建议吗
我假设我将从ping脚本中删除$Computers变量,而在多线程脚本中使用$ObjectList变量。但是我所有的尝试都失败了。您可以使用[System.Net.NetworkInformation.ping]异步ping主机,无需使用运行空间。此外,它将ping主机一次,因此比测试连接快得多。下面是一批主机(约90台)的示例。我想一次装载100K不是一个好主意,可能会将其分成更小的批次,然后逐个装载
$hosts = "www.facebook.com,www.twitter.com,www.youtu.be,www.google.com,www.youtube.com,www.instagram.com,www.linkedin.com,www.pinterest.com,www.wordpress.com,www.blogspot.com,www.apple.com,www.adobe.com,www.tumblr.com,www.amazon.com,www.vimeo.com,www.flickr.com,www.microsoft.com,www.yahoo.com,www.godaddy.com,www.qq.com,www.vk.com,www.reddit.com,www.baidu.com,www.nytimes.com,www.buydomains.com,www.wp.com,www.statcounter.com,www.jimdo.com,www.blogger.com,www.github.com,www.weebly.com,www.soundcloud.com,www.myspace.com,www.addthis.com,www.theguardian.com,www.cnn.com,www.stumbleupon.com,www.gravatar.com,www.digg.com,www.addtoany.com,www.creativecommons.org,www.paypal.com,www.yelp.com,www.imdb.com,www.huffingtonpost.com,www.feedburner.com,www.issuu.com,www.wixsite.com,www.wix.com,www.dropbox.com,www.forbes.com,www.amazonaws.com,www.washingtonpost.com,www.bluehost.com,www.etsy.com,www.go.com,www.msn.com,www.wsj.com,www.weibo.com,www.fc2.com,www.eventbrite.com,www.parallels.com,www.ebay.com,www.livejournal.com,www.reuters.com,www.taobao.com,www.typepad.com,www.bloomberg.com,www.elegantthemes.com,www.eepurl.com,www.usatoday.com,www.about.com,www.medium.com,www.macromedia.com,www.xing.com,www.bing.com,www.time.com,www.tripadvisor.com,www.aol.com,www.constantcontact.com,www.latimes.com,www.list-manage.com,www.webs.com,www.opera.com,www.live.com,www.bandcamp.com,www.bbc.com,www.businessinsider.com,www.dailymotion.com,www.cpanel.com,www.disqus.com,www.sina.com.cn,www.spotify.com,www.wired.com,www.googleusercontent.com"
$hosts = $hosts -split ","
$tasks = @{}
foreach ($h in $hosts) { $tasks[$h] = [System.Net.NetworkInformation.Ping]::new().SendPingAsync($h)}
Write-Host "Waiting for batch is completed" -NoNewline
while($false -in $tasks.Values.IsCompleted) {sleep -Milliseconds 300; Write-Host "." -NoNewline}
$result = foreach($h in $hosts) {
$r = $tasks[$h].Result
[PSCustomObject]@{
host = $h
address = $r.Address.IPAddressToString
status = if($r.Address.IPAddressToString){$r.Status}else{"Failed"}
time = $r.RoundtripTime
bytes = $r.Buffer.Count
ttl = $r.Options.Ttl
}
}
$result | Format-Table -AutoSize
这个脚本,我从大纲中的链接和作者那里改编,对我来说有很好的表现。我对它进行了修改,不仅采用了一系列ip地址,还采用了一系列主机名。正因为如此,一些变量名的名称很糟糕,但这不是生产代码,所以我没有在下面的内容之外对其进行投资
function Global:Ping-IPRange {
<#
.SYNOPSIS
Sends ICMP echo request packets to a range of IPv4 addresses between two given addresses.
.DESCRIPTION
This function lets you sends ICMP echo request packets ("pings") to
a range of IPv4 addresses using an asynchronous method.
Therefore this technique is very fast but comes with a warning.
Ping sweeping a large subnet or network with many swithes may result in
a peak of broadcast traffic.
Use the -Interval parameter to adjust the time between each ping request.
For example, an interval of 60 milliseconds is suitable for wireless networks.
The RawOutput parameter switches the output to an unformated
[System.Net.NetworkInformation.PingReply[]].
.INPUTS
None
You cannot pipe input to this funcion.
.OUTPUTS
The function only returns output from successful pings.
Type: System.Net.NetworkInformation.PingReply
The RawOutput parameter switches the output to an unformated
[System.Net.NetworkInformation.PingReply[]].
.NOTES
Author : G.A.F.F. Jakobs
Created : August 30, 2014
Version : 6
.EXAMPLE
Ping-IPRange -StartAddress 192.168.1.1 -EndAddress 192.168.1.254 -Interval 20
IPAddress Bytes Ttl ResponseTime
--------- ----- --- ------------
192.168.1.41 32 64 371
192.168.1.57 32 128 0
192.168.1.64 32 128 1
192.168.1.63 32 64 88
192.168.1.254 32 64 0
In this example all the ip addresses between 192.168.1.1 and 192.168.1.254 are pinged using
a 20 millisecond interval between each request.
All the addresses that reply the ping request are listed.
.EXAMPLE
Ping-IPRange -HostName "host1"
Ping-IPRange -HostName @('host1', 'host2')
Ping-IPRange -HostName @('www.microsoft.com', 'www.google.com')
.LINK
http://gallery.technet.microsoft.com/Fast-asynchronous-ping-IP-d0a5cf0e
#>
[CmdletBinding(ConfirmImpact='Low')]
Param(
[parameter(Mandatory = $true, Position = 0, ParameterSetName='range')]
[System.Net.IPAddress]$StartAddress,
[parameter(Mandatory = $true, Position = 1, ParameterSetName='range')]
[System.Net.IPAddress]$EndAddress,
[parameter(Mandatory = $true, Position = 0, ParameterSetName='list')]
[string[]]$HostName,
[int]$Interval = 30,
[Switch]$RawOutput = $false
)
$timeout = 2000
function New-Range ($start, $end) {
$addrList = [System.Collections.ArrayList]::new()
[byte[]]$BySt = $start.GetAddressBytes()
[Array]::Reverse($BySt)
[byte[]]$ByEn = $end.GetAddressBytes()
[Array]::Reverse($ByEn)
$i1 = [System.BitConverter]::ToUInt32($BySt,0)
$i2 = [System.BitConverter]::ToUInt32($ByEn,0)
for($x = $i1;$x -le $i2;$x++){
$ip = ([System.Net.IPAddress]$x).GetAddressBytes()
[Array]::Reverse($ip)
$null = $addrList.Add([System.Net.IPAddress]::Parse($($ip -join '.')))
}
,$addrList
}
if ($HostName)
{
$IPrange = @($HostName)
}
else
{
[System.Collections.ArrayList]$IPrange = New-Range $StartAddress $EndAddress
}
$IpTotal = $IPrange.Count
Get-Event -SourceIdentifier "ID-Ping*" | Remove-Event
Get-EventSubscriber -SourceIdentifier "ID-Ping*" | Unregister-Event
$index = 0
foreach ($ip in $IPrange){
if ($HostName)
{
[string]$VarName = "Ping_" + $ip + "_" + [guid]::NewGuid().ToString()
}
else
{
[string]$VarName = "Ping_" + $ip.Address
}
New-Variable -Name $VarName -Value (New-Object System.Net.NetworkInformation.Ping)
Register-ObjectEvent -InputObject (Get-Variable $VarName -ValueOnly) -EventName PingCompleted -SourceIdentifier "ID-$VarName"
(Get-Variable $VarName -ValueOnly).SendAsync($ip,$timeout,$VarName)
try{
$pending = (Get-Event -SourceIdentifier "ID-Ping*").Count
}catch [System.InvalidOperationException]{
Write-Verbose "Ping-IPrange : InvalidOperationException" -Verbose
}
finally{
Remove-Variable $VarName
}
#$index = [array]::indexof($IPrange,$ip)
if ($HostName)
{
Write-Progress -Activity "Sending ping to" -Id 1 -status $ip -PercentComplete (($index / $IpTotal) * 100)
}
else
{
Write-Progress -Activity "Sending ping to" -Id 1 -status $ip.IPAddressToString -PercentComplete (($index / $IpTotal) * 100)
}
Write-Progress -Activity "ICMP requests pending" -Id 2 -ParentId 1 -Status ($index - $pending) -PercentComplete (($index - $pending)/$IpTotal * 100)
Start-Sleep -Milliseconds $Interval
$index++
}
Write-Progress -Activity "Done sending ping requests" -Id 1 -Status 'Waiting' -PercentComplete 100
While($pending -lt $IpTotal){
Wait-Event -SourceIdentifier "ID-Ping*" | Out-Null
Start-Sleep -Milliseconds 10
$pending = (Get-Event -SourceIdentifier "ID-Ping*").Count
Write-Progress -Activity "ICMP requests pending" -Id 2 -ParentId 1 -Status ($IpTotal - $pending) -PercentComplete (($IpTotal - $pending)/$IpTotal * 100)
}
if($RawOutput){
$Reply = Get-Event -SourceIdentifier "ID-Ping*" | ForEach {
If($_.SourceEventArgs.Reply.Status -eq "Success"){
$_.SourceEventArgs.Reply
}
Unregister-Event $_.SourceIdentifier
Remove-Event $_.SourceIdentifier
}
}else{
$events = Get-Event -SourceIdentifier "ID-Ping*"
$Reply = [System.Collections.ArrayList]::new()
$i = 0
foreach ($event in $events) {
If($event.SourceEventArgs.Reply.Status -eq "Success"){
if ($HostName)
{
$null = $Reply.Add(
[PSCustomObject]@{
"HostName" = $HostName[$i]
"IPAddress" = $event.SourceEventArgs.Reply.Address
"Bytes" = $event.SourceEventArgs.Reply.Buffer.Length
"Ttl" = $event.SourceEventArgs.Reply.Options.Ttl
"ResponseTime" = $event.SourceEventArgs.Reply.RoundtripTime
}
)
}
else
{
$null = $Reply.Add(
[PSCustomObject]@{
"IPAddress" = $event.SourceEventArgs.Reply.Address
"Bytes" = $event.SourceEventArgs.Reply.Buffer.Length
"Ttl" = $event.SourceEventArgs.Reply.Options.Ttl
"ResponseTime" = $event.SourceEventArgs.Reply.RoundtripTime
}
)
}
}
else{
$addr = ($event.SourceIdentifier -split '_')[1]
$ip = ((([System.Net.IPAddress]$addr).IPAddressToString).Split('.'))
[Array]::Reverse($ip)
$ip = $ip -join '.'
if ($HostName)
{
$null = $Reply.Add(
[PSCustomObject]@{
"HostName" = $HostName[$i]
"IPAddress" = '0.0.0.0'
"Bytes" = -1
"Ttl" = -1
"ResponseTime" = -1
}
)
}
else
{
$null = $Reply.Add(
[PSCustomObject]@{
"IPAddress" = $ip
"Bytes" = -1
"Ttl" = -1
"ResponseTime" = -1
}
)
}
}
Unregister-Event $event.SourceIdentifier
Remove-Event $event.SourceIdentifier
$i++
}
}
if($Reply -eq $Null){
Write-Verbose "Ping-IPrange : No ip address responded" -Verbose
}
return ,$Reply
}
函数全局:Ping IPRange{
[CmdletBinding(confirImpact='Low')]
Param(
[参数(必需=$true,位置=0,参数setName='range')]
[System.Net.IPAddress]$StartAddress,
[参数(必需=$true,位置=1,参数setName='range')]
[System.Net.IPAddress]$EndAddress,
[参数(必需=$true,位置=0,参数setName='list')]
[string[]]$HostName,
[int]$Interval=30,
[开关]$RawOutput=$false
)
$timeout=2000
函数新范围($start,$end){
$addrList=[System.Collections.ArrayList]::new()
[字节[]]$BySt=$start.GetAddressBytes()
[数组]::反向($BySt)
[byte[]]$ByEn=$end.GetAddressBytes()
[数组]::反向($ByEn)
$i1=[System.BitConverter]::ToUInt32($BySt,0)
$i2=[System.BitConverter]::ToUInt32($ByEn,0)
对于($x=$i1;$x-le$i2;$x++){
$ip=([System.Net.IPAddress]$x).GetAddressBytes()
[数组]::反向($ip)
$null=$addrList.Add([System.Net.IPAddress]::Parse($($ip-join'.'))
}
,$addrList
}
如果($HostName)
{
$IPrange=@($HostName)
}
其他的
{
[System.Collections.ArrayList]$IPrange=新范围$StartAddress$EndAddress
}
$IpTotal=$IPrange.Count
获取事件-源标识符“ID Ping*”|删除事件
Get-EventSubscriber-SourceIdentifier“ID Ping*”|注销事件
$index=0
foreach($IPrange中的ip){
如果($HostName)
{
[string]$VarName=“Ping”+$ip+“\+[guid]::NewGuid().ToString()
}
其他的
{
[string]$VarName=“Ping\ux”+$ip.Address
}
新变量-Name$VarName-Value(新对象System.Net.NetworkInformation.Ping)
注册ObjectEvent-InputObject(获取变量$VarName-ValueOnly)-EventName PingCompleted-SourceIdentifier“ID-$VarName”
(获取变量$VarName-ValueOnly).SendAsync($ip,$timeout,$VarName)
试一试{
$pending=(获取事件-SourceIdentifier“ID Ping*”)。计数
}catch[系统无效操作异常]{
详细编写“Ping IPrange:InvalidOperationException”-详细
}
最后{
删除变量$VarName
}
#$index=[array]::indexof($IPrange,$ip)
如果($HostName)
{
写入进度-活动“发送ping到”-Id 1-状态$ip-完成百分比(($index/$IpTotal)*100)
}
其他的
{
写入进度-活动“发送ping到”-Id 1-状态$ip.IPAddressToString-完成百分比(($index/$IpTotal)*100)
}
写入进度-活动“ICMP请求待定”-Id 2-父Id 1-状态($index-$pending)-完成百分比($index-$pending)/$IpTotal*100)
开始睡眠-毫秒$Interval
美元指数++
}
写入进度-活动“已完成发送ping请求”-Id 1-状态“正在等待”-完成百分比100
而($pending-lt$IpTotal){
等待事件-SourceIdentifier“ID Ping*”| Out Null
开始睡眠-10毫秒
$pending=(获取事件-SourceIdentifier“ID Ping*”)。计数
写入进度-活动“ICMP请求待定”-Id 2-父Id 1-状态($IpTotal-$pending)-完成百分比($IpTotal-$pending)/$IpTotal*100)
}
如果($RAW输出){
$Reply=Get事件-SourceIdentifier“ID Ping*”| ForEach{
If($\u0.SourceEventArgs.Reply.Status-eq“Success”){
$\.SourceEventArgs.Reply
}
取消注册事件$\ SourceIdentifier
删除事件$\源标识符
}
}否则{
$events=Get事件-SourceIdentifier“ID Ping*”
$Reply=[System.Collections.ArrayList]::new()
$i=0
foreach($events中的事件){
If($event.SourceEventArgs.Reply.Status-eq“Success”){
如果($HostName)
{
$null=$Reply.Add(
[PSCustomObject]@{
“主机名”=$HostName[$i]
“IPAddress”=$event.SourceEventArgs.Reply.Address
“字节”=$event.SourceEventArgs.Reply.Buffer.Length
“Ttl”=$event.SourceEventArgs.Reply.Options.Ttl
“ResponseTime”=$event.SourceEventArgs.Reply.RoundtripTime
}
)
}
其他的
{
function Global:Ping-IPRange {
<#
.SYNOPSIS
Sends ICMP echo request packets to a range of IPv4 addresses between two given addresses.
.DESCRIPTION
This function lets you sends ICMP echo request packets ("pings") to
a range of IPv4 addresses using an asynchronous method.
Therefore this technique is very fast but comes with a warning.
Ping sweeping a large subnet or network with many swithes may result in
a peak of broadcast traffic.
Use the -Interval parameter to adjust the time between each ping request.
For example, an interval of 60 milliseconds is suitable for wireless networks.
The RawOutput parameter switches the output to an unformated
[System.Net.NetworkInformation.PingReply[]].
.INPUTS
None
You cannot pipe input to this funcion.
.OUTPUTS
The function only returns output from successful pings.
Type: System.Net.NetworkInformation.PingReply
The RawOutput parameter switches the output to an unformated
[System.Net.NetworkInformation.PingReply[]].
.NOTES
Author : G.A.F.F. Jakobs
Created : August 30, 2014
Version : 6
.EXAMPLE
Ping-IPRange -StartAddress 192.168.1.1 -EndAddress 192.168.1.254 -Interval 20
IPAddress Bytes Ttl ResponseTime
--------- ----- --- ------------
192.168.1.41 32 64 371
192.168.1.57 32 128 0
192.168.1.64 32 128 1
192.168.1.63 32 64 88
192.168.1.254 32 64 0
In this example all the ip addresses between 192.168.1.1 and 192.168.1.254 are pinged using
a 20 millisecond interval between each request.
All the addresses that reply the ping request are listed.
.EXAMPLE
Ping-IPRange -HostName "host1"
Ping-IPRange -HostName @('host1', 'host2')
Ping-IPRange -HostName @('www.microsoft.com', 'www.google.com')
.LINK
http://gallery.technet.microsoft.com/Fast-asynchronous-ping-IP-d0a5cf0e
#>
[CmdletBinding(ConfirmImpact='Low')]
Param(
[parameter(Mandatory = $true, Position = 0, ParameterSetName='range')]
[System.Net.IPAddress]$StartAddress,
[parameter(Mandatory = $true, Position = 1, ParameterSetName='range')]
[System.Net.IPAddress]$EndAddress,
[parameter(Mandatory = $true, Position = 0, ParameterSetName='list')]
[string[]]$HostName,
[int]$Interval = 30,
[Switch]$RawOutput = $false
)
$timeout = 2000
function New-Range ($start, $end) {
$addrList = [System.Collections.ArrayList]::new()
[byte[]]$BySt = $start.GetAddressBytes()
[Array]::Reverse($BySt)
[byte[]]$ByEn = $end.GetAddressBytes()
[Array]::Reverse($ByEn)
$i1 = [System.BitConverter]::ToUInt32($BySt,0)
$i2 = [System.BitConverter]::ToUInt32($ByEn,0)
for($x = $i1;$x -le $i2;$x++){
$ip = ([System.Net.IPAddress]$x).GetAddressBytes()
[Array]::Reverse($ip)
$null = $addrList.Add([System.Net.IPAddress]::Parse($($ip -join '.')))
}
,$addrList
}
if ($HostName)
{
$IPrange = @($HostName)
}
else
{
[System.Collections.ArrayList]$IPrange = New-Range $StartAddress $EndAddress
}
$IpTotal = $IPrange.Count
Get-Event -SourceIdentifier "ID-Ping*" | Remove-Event
Get-EventSubscriber -SourceIdentifier "ID-Ping*" | Unregister-Event
$index = 0
foreach ($ip in $IPrange){
if ($HostName)
{
[string]$VarName = "Ping_" + $ip + "_" + [guid]::NewGuid().ToString()
}
else
{
[string]$VarName = "Ping_" + $ip.Address
}
New-Variable -Name $VarName -Value (New-Object System.Net.NetworkInformation.Ping)
Register-ObjectEvent -InputObject (Get-Variable $VarName -ValueOnly) -EventName PingCompleted -SourceIdentifier "ID-$VarName"
(Get-Variable $VarName -ValueOnly).SendAsync($ip,$timeout,$VarName)
try{
$pending = (Get-Event -SourceIdentifier "ID-Ping*").Count
}catch [System.InvalidOperationException]{
Write-Verbose "Ping-IPrange : InvalidOperationException" -Verbose
}
finally{
Remove-Variable $VarName
}
#$index = [array]::indexof($IPrange,$ip)
if ($HostName)
{
Write-Progress -Activity "Sending ping to" -Id 1 -status $ip -PercentComplete (($index / $IpTotal) * 100)
}
else
{
Write-Progress -Activity "Sending ping to" -Id 1 -status $ip.IPAddressToString -PercentComplete (($index / $IpTotal) * 100)
}
Write-Progress -Activity "ICMP requests pending" -Id 2 -ParentId 1 -Status ($index - $pending) -PercentComplete (($index - $pending)/$IpTotal * 100)
Start-Sleep -Milliseconds $Interval
$index++
}
Write-Progress -Activity "Done sending ping requests" -Id 1 -Status 'Waiting' -PercentComplete 100
While($pending -lt $IpTotal){
Wait-Event -SourceIdentifier "ID-Ping*" | Out-Null
Start-Sleep -Milliseconds 10
$pending = (Get-Event -SourceIdentifier "ID-Ping*").Count
Write-Progress -Activity "ICMP requests pending" -Id 2 -ParentId 1 -Status ($IpTotal - $pending) -PercentComplete (($IpTotal - $pending)/$IpTotal * 100)
}
if($RawOutput){
$Reply = Get-Event -SourceIdentifier "ID-Ping*" | ForEach {
If($_.SourceEventArgs.Reply.Status -eq "Success"){
$_.SourceEventArgs.Reply
}
Unregister-Event $_.SourceIdentifier
Remove-Event $_.SourceIdentifier
}
}else{
$events = Get-Event -SourceIdentifier "ID-Ping*"
$Reply = [System.Collections.ArrayList]::new()
$i = 0
foreach ($event in $events) {
If($event.SourceEventArgs.Reply.Status -eq "Success"){
if ($HostName)
{
$null = $Reply.Add(
[PSCustomObject]@{
"HostName" = $HostName[$i]
"IPAddress" = $event.SourceEventArgs.Reply.Address
"Bytes" = $event.SourceEventArgs.Reply.Buffer.Length
"Ttl" = $event.SourceEventArgs.Reply.Options.Ttl
"ResponseTime" = $event.SourceEventArgs.Reply.RoundtripTime
}
)
}
else
{
$null = $Reply.Add(
[PSCustomObject]@{
"IPAddress" = $event.SourceEventArgs.Reply.Address
"Bytes" = $event.SourceEventArgs.Reply.Buffer.Length
"Ttl" = $event.SourceEventArgs.Reply.Options.Ttl
"ResponseTime" = $event.SourceEventArgs.Reply.RoundtripTime
}
)
}
}
else{
$addr = ($event.SourceIdentifier -split '_')[1]
$ip = ((([System.Net.IPAddress]$addr).IPAddressToString).Split('.'))
[Array]::Reverse($ip)
$ip = $ip -join '.'
if ($HostName)
{
$null = $Reply.Add(
[PSCustomObject]@{
"HostName" = $HostName[$i]
"IPAddress" = '0.0.0.0'
"Bytes" = -1
"Ttl" = -1
"ResponseTime" = -1
}
)
}
else
{
$null = $Reply.Add(
[PSCustomObject]@{
"IPAddress" = $ip
"Bytes" = -1
"Ttl" = -1
"ResponseTime" = -1
}
)
}
}
Unregister-Event $event.SourceIdentifier
Remove-Event $event.SourceIdentifier
$i++
}
}
if($Reply -eq $Null){
Write-Verbose "Ping-IPrange : No ip address responded" -Verbose
}
return ,$Reply
}