难以理解 Powershell 工作流程 ForEach
Trouble Understanding Powershell Workflow ForEach
我正在尝试使用 Powershell Workflows 和一些我需要并行完成的工作。
我没走多远就遇到了我的第一个障碍,但我不明白我做错了什么:
$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
问题已解决,感谢this excellent article on powershell arrays
Now, since it’s already an array, casting it again doesn’t result in a
second level of nesting:
PS (66) > $a = [array] [array] 1
PS (67) > $a[0]
1
However using 2 commas does nest the array since it is the array
construction operation:
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
特定问题和解决方法。
我正在尝试使用 Powershell Workflows 和一些我需要并行完成的工作。
我没走多远就遇到了我的第一个障碍,但我不明白我做错了什么:
$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
问题已解决,感谢this excellent article on powershell arrays
Now, since it’s already an array, casting it again doesn’t result in a second level of nesting:
PS (66) > $a = [array] [array] 1
PS (67) > $a[0]
1
However using 2 commas does nest the array since it is the array construction operation:
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
特定问题和解决方法。