最近我们开始编写需要很长时间才能完成的脚本。因此我们深入研究了 PowerShell 工作流程。阅读一些文档后,我了解了基础知识。但是,我似乎找不到一种方法来创建[PSCustomObject]
对于一个内的每个单独的项目foreach -parallel
陈述。
一些代码来解释:
Workflow Test-Fruit {
foreach -parallel ($I in (0..1)) {
# Create a custom hashtable for this specific object
$Result = [Ordered]@{
Name = $I
Taste = 'Good'
Price = 'Cheap'
}
Parallel {
Sequence {
# Add a custom entry to the hashtable
$Result += @{'Color' = 'Green'}
}
Sequence {
# Add a custom entry to the hashtable
$Result += @{'Fruit' = 'Kiwi'}
}
}
# Generate a PSCustomObject to work with later on
[PSCustomObject]$Result
}
}
Test-Fruit
出错的部分是为$Result
哈希表中的Sequence
堵塞。即使尝试以下操作,它仍然失败:
$WORKFLOW:Result += @{'Fruit' = 'Kiwi'}
好吧,就这样,经过尝试和测试:
Workflow Test-Fruit {
foreach -parallel ($I in (0..1)) {
# Create a custom hashtable for this specific object
$WORKFLOW:Result = [Ordered]@{
Name = $I
Taste = 'Good'
Price = 'Cheap'
}
Parallel {
Sequence {
# Add a custom entry to the hashtable
$WORKFLOW:Result += @{'Color' = 'Green'}
}
Sequence {
# Add a custom entry to the hashtable
$WORKFLOW:Result += @{'Fruit' = 'Kiwi'}
}
}
# Generate a PSCustomObject to work with later on
[PSCustomObject]$WORKFLOW:Result
}
}
Test-Fruit
您应该将其定义为 $WORKFLOW:var 并在整个工作流程中重复使用以访问范围。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)