我试图通过Powershell工作流程和我需要并行完成的一些工作。
在我遇到第一个障碍之前,我没有走得太远,但我不明白我在这里做错了什么:
$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 :(得分:0)
问题解决了,感谢this excellent article on powershell arrays
现在,因为它已经是一个数组,再次投射它不会导致数组 第二级嵌套:
PS(66)> $ a = [array] [array] 1
PS(67)> $ A [0]
1
但是使用2个逗号会嵌套数组,因为它是数组 施工作业:
PS(68)> $ a = ,, 1
PS(69)> $ A [0] [0]
1
鉴于此,这很好用:
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
但是,如果在工作流程之外添加第二个逗号,则会出现错误。因此,这是workflow
或parallel
特定问题和解决方法。