亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Powershell可以并行運行命令嗎?

Powershell可以并行運行命令嗎?

Powershell可以并行運行命令嗎?我有一個powershell腳本對一堆圖像進行一些批處理,我想做一些并行處理。Powershell似乎有一些后臺處理選項,如啟動作業,等待作業等,但我找到的并行工作的唯一好資源是編寫腳本文本并運行它們(PowerShell多線程)理想情況下,我喜歡類似于.net 4中的并行foreach的東西。有點像:foreach-parallel -threads 4 ($file in (Get-ChildItem $dir)){    .. Do Work}也許我會更好的只是下降到c#...
查看完整描述

3 回答

?
炎炎設計

TA貢獻1808條經驗 獲得超4個贊

您可以使用后臺作業在Powershell 2中執行并行作業。查看Start-Job和其他作業cmdlet。

# Loop through the server listGet-Content "ServerList.txt" | %{

  # Define what each job does
  $ScriptBlock = {
    param($pipelinePassIn) 
    Test-Path "\\$pipelinePassIn\c`$\Something"
    Start-Sleep 60
  }

  # Execute the jobs in parallel
  Start-Job $ScriptBlock -ArgumentList $_}Get-Job# Wait for it all to completeWhile (Get-Job -State "Running"){
  Start-Sleep 10}# Getting the information back from the jobsGet-Job | Receive-Job


查看完整回答
反對 回復 2019-08-16
?
LEATH

TA貢獻1936條經驗 獲得超7個贊

史蒂夫湯森的回答在理論上是正確的,但在實踐中卻沒有,正如@likwid指出的那樣。我修改后的代碼考慮了工作環境障礙 -默認情況下沒有任何障礙超越這個障礙!因此,自動$_變量可以在循環中使用,但不能直接在腳本塊中使用,因為它位于作業創建的單獨上下文中。

要將變量從父上下文傳遞到子上下文,請使用-ArgumentList參數on Start-Job發送它并param在腳本塊內部使用它來接收它。

cls# Send in two root directory names, one that exists and one that does not.# Should then get a "True" and a "False" result out the end."temp", "foo" | %{

  $ScriptBlock = {
    # accept the loop variable across the job-context barrier
    param($name) 
    # Show the loop variable has made it through!
    Write-Host "[processing '$name' inside the job]"
    # Execute a command
    Test-Path "\$name"
    # Just wait for a bit...
    Start-Sleep 5
  }

  # Show the loop variable here is correct
  Write-Host "processing $_..."

  # pass the loop variable across the job-context barrier
  Start-Job $ScriptBlock -ArgumentList $_}# Wait for all to completeWhile (Get-Job -State "Running") { Start-Sleep 2 }# Display output from all jobsGet-Job | Receive-Job# CleanupRemove-Job *

(我通常喜歡提供PowerShell文檔的參考作為支持證據,但是,唉,我的搜索沒有結果。如果您碰巧知道上下文分離的記錄,請在此發表評論以告訴我?。?/p>


查看完整回答
反對 回復 2019-08-16
  • 3 回答
  • 0 關注
  • 1011 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號