代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Shell
】
Powershell并行循环
作者:
DDT
/ 发布于
2013/2/17
/
1550
function ForEach-Parallel { <# .SYNOPSIS A parallel ForEach that uses runspaces .PARAMETER ScriptBlock ScriptBlock to execute for each InputObject .PARAMETER ScriptFile Script file to execute for each InputObject .PARAMETER InputObject Object(s) to run script against in parallel .PARAMETER Throttle Maximum number of threads to run at one time. Default: 5 .PARAMETER Timeout Stop each thread after this many minutes. Default: 0 WARNING: This parameter should be used as a failsafe only Set it for roughly the entire duration you expect for all threads to complete .PARAMETER SleepTimer When looping through open threads, wait this many milliseconds before looping again. Default: 200 .EXAMPLE (0..50) | ForEach-Parallel -Throttle 4 { $_; sleep (Get-Random -Minimum 0 -Maximum 5) } } Send the number 0 through 50 to scriptblock. For each, display the number and then sleep for 0 to 5 seconds. Only execute 4 threads at a time. .EXAMPLE $servers | Foreach-Parallel -Throttle 20 -Timeout 60 -sleeptimer 200 -verbose -scriptFile C:\query.ps1 Run query.ps1 against each computer in $servers. Run 20 threads at a time, timeout a thread if it takes longer than 60 minutes to run, give verbose output. .FUNCTIONALITY PowerShell Language .NOTES Credit to Tome Tanasovski http://powertoe.wordpress.com/2012/05/03/foreach-parallel/ #> [cmdletbinding()] param( [Parameter(Mandatory=$false,position=0,ParameterSetName='ScriptBlock')] [System.Management.Automation.ScriptBlock]$ScriptBlock, [Parameter(Mandatory=$false,ParameterSetName='ScriptFile')] [ValidateScript({test-path $_ -pathtype leaf})] $scriptFile, [Parameter(Mandatory=$true,ValueFromPipeline=$true)] [PSObject]$InputObject, [int]$Throttle=5, [double]$sleepTimer = 200, [double]$Timeout = 0 ) BEGIN { #Build the scriptblock depending on the parameter used switch ($PSCmdlet.ParameterSetName){ 'ScriptBlock' {$ScriptBlock = $ExecutionContext.InvokeCommand.NewScriptBlock("param(`$_)`r`n" + $Scriptblock.ToString())} 'ScriptFile' {$scriptblock = [scriptblock]::Create($(get-content $scriptFile | out-string))} Default {Write-Error ("Must provide ScriptBlock or ScriptFile"); Return} } #Define the initial sessionstate, create the runspacepool Write-Verbose "Creating runspace pool with $Throttle threads" $sessionState = [system.management.automation.runspaces.initialsessionstate]::CreateDefault() $pool = [Runspacefactory]::CreateRunspacePool(1, $Throttle, $sessionState, $host) $pool.open() #array to hold details on each thread $threads = @() #If inputObject is bound get a total count and set bound to true $bound = $false if( $PSBoundParameters.ContainsKey("inputObject") ){ $bound = $true $totalCount = $inputObject.count } } PROCESS { $run = @' #For each pipeline object, create a new powershell instance, add to runspacepool $powershell = [powershell]::Create().addscript($scriptblock).addargument($InputObject) $powershell.runspacepool=$pool $startTime = get-date #add references to inputobject, instance, handle and startTime to threads array $threads += New-Object psobject -Property @{ Object = $inputObject; instance = $powershell; handle = $powershell.begininvoke(); startTime = $startTime } Write-Verbose "Added $inputobject to the runspacepool at $startTime" '@ #Run the here string. Put it in a foreach loop if it didn't come from the pipeline if($bound){ $run = $run -replace 'inputObject', 'object' foreach($object in $inputObject){ Invoke-Expression -command $run } } else{ Invoke-Expression -command $run } } END { $notdone = $true #Loop through threads. while ($notdone) { $notdone = $false for ($i=0; $i -lt $threads.count; $i++) { $thread = $threads[$i] if ($thread) { #If thread is complete, dispose of it. if ($thread.handle.iscompleted) { Write-verbose "Closing thread for $($thread.Object)" $thread.instance.endinvoke($thread.handle) $thread.instance.dispose() $threads[$i] = $null } #Thread exceeded maxruntime timeout threshold elseif( $Timeout -ne 0 -and ( (get-date) - $thread.startTime ).totalminutes -gt $Timeout ){ Write-Error "Closing thread for $($thread.Object): Thread exceeded $Timeout minute limit" -TargetObject $thread.inputObject $thread.instance.dispose() $threads[$i] = $null } #Thread is running, loop again! else { $notdone = $true } } } #Sleep for specified time before looping again Start-Sleep -Milliseconds $sleepTimer } } }
试试其它关键字
并行循环
同语言下
.
判断用户输入的是否为IP地址
.
根据web访问日志,封禁请求量异常的IP,如IP在半小时
.
iptables自动屏蔽访问网站频繁的IP
.
批量修改服务器用户密码
.
监控httpd的进程数,根据监控情况做相应处理
.
创建10个用户,并分别设置密码,密码要求10位且包含大
.
Expect实现SSH免交互执行命令
.
输入数字运行相应命令
.
用shell打印示例语句中字母数小于6的单词
.
扫描主机端口状态
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
DDT
贡献的其它代码
(
160
)
.
Oracle统计表的数据行和数据块信息
.
html标签闭合检测与修复
.
Powershell日期计算
.
Powershell的Base64编解码
.
Powershell并行循环
.
Powershell目录中搜索文本
.
Powershell枚举远程机器上的本地权限组
.
VBScript解析csv文件
.
快速排序之Powershell
.
批处理输出格式化时间字符串
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3