2016-07-05 108 views
0

我試圖讓我的腳溼透PowerShell工作流和一些工作,我需要做的並行。問題了解Powershell工作流For Each

我沒有得到很遠,打我的第一個路障前,但我不明白我在做什麼錯在這裏:

$operations = ,("Item0", "Item1") 

ForEach ($operation in $operations) { 
    Write-Output "Item0: $($operation.Item(0)) Item1: $($operation.Item(1))" 
} 

workflow operationsWorkflow{ 
    Write-Output "Running Workflow" 
    $operations = ,("Item0", "Item1") 
    ForEach -Parallel ($operation in $operations) { 
     #Fails: Method invocation failed because [System.String] does not contain a method named 'Item'. 
     #Write-Output "Item0: $($operation.Item(0)) Item1: $($operation.Item(1))" 

     Write-Output "Item $operation" 
    } 
} 

operationsWorkflow 
+0

到目前爲止,我所能說的是,在這種情況下,逗號技巧並不適用於創建數組。我的第一個猜測是使用.NET'[System.Collections.Generic.List [string []]]'或類似的東西,而不是依靠PowerShell數組魔術。 – Eris

+0

注意:如果'$ operations'有多個數組項添加,即使註釋行也能正常工作。例如:'$ operations = @((「Item0」,「Item1」),(「ItemA」,「ItemB」))' – Eris

+0

我想要PowerShell,我只是總是感到困惑...... –

回答

0

問題解決了,感謝this excellent article on powershell arrays

現在,因爲它已經陣列,再次澆鑄不會導致嵌套的 第二級:

PS(66)> $ A = [數組] [數組] 1

PS(67)> $ A [0]

然而使用2-逗號確實巢陣列,因爲它是在陣列 施工操作:

PS(68) > $ A = ,, 1

PS(69)> $ A [0] [0]

鑑於此,這工作得很好:

workflow operationsWorkflow{ 
    Write-Output "Running Workflow" 
    $operations = ,,("Item0", "Item1") 
    ForEach -Parallel ($operation in $operations) { 
      Write-Output "Item0: $($operation.Item(0)) Item1: $($operation.Item(1))" 
    } 
} 

operationsWorkflow 

但是,如果第二個逗號的工作流程之外添加,然後會出現這樣的錯誤。所以這是一個workflowparallel特定問題和解決方法。