首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Foreach -parallel对象

Foreach -parallel对象
EN

Stack Overflow用户
提问于 2016-06-17 12:42:58
回答 2查看 6.3K关注 0票数 4

最近,我们开始编写需要很长时间才能完成的脚本。因此,我们深入研究了PowerShell工作流。在阅读了一些文档之后,我了解了一些基本知识。但是,我似乎找不到为foreach -parallel语句中的每个单独项创建一个foreach -parallel的方法。

需要解释的代码:

代码语言:javascript
复制
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哈希表添加一个值。即使在尝试以下操作时,它仍然失败:

代码语言:javascript
复制
$WORKFLOW:Result += @{'Fruit' = 'Kiwi'}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-17 13:29:14

好的,给你,试过并测试:

代码语言:javascript
复制
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,并在整个工作流中重复使用它来访问作用域。

票数 2
EN

Stack Overflow用户

发布于 2016-06-17 13:02:02

您可以将$Result分配给Parallel块的输出,然后添加其他属性:

代码语言:javascript
复制
$Result = Parallel {
    Sequence {
        # Add a custom entry to the hashtable
        [Ordered]@{'Color' = 'Green'}                    
    }

    Sequence {
        # Add a custom entry to the hashtable
       [Ordered] @{'Fruit' = 'Kiwi'}
    }
}

# Generate a PSCustomObject to work with later on
$Result += [Ordered]@{
    Name  = $I
    Taste = 'Good'
    Price = 'Cheap'
}

# Generate a PSCustomObject to work with later on
[PSCustomObject]$Result
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37881875

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档