One nice feature of a DSC configuration is that all resources support specifying a og体育于取决于 property that ensures that the resources that it depends on are ran first. Every once in a while, I find myself wanting to use that feature in other scripts. I created a module called og体育于取决于 to do that for me.

指数

基本方案

假设我正在从数据文件中创建一些用户组。每个项目都调整组名称和组的成员。这是JSON可能看起来的基本样本。

    [
        {
            "Name":"Group1"
        },
        {
            "Name":"Group2",
            "Members":"Group1"
        }
    ]

If I process this list in order, I would create Group1 first and then create Group2 with Group1 as a member. You can see how processing this in order is important.

如果我们翻转订单怎么办?

    [
        {
            "Name":"Group2",
            "Members":"Group1"
        },
        {
            "Name":"Group1"
        }
    ]

When we go to create Group2 with Group1 as a member before creating Group1 then I would expect our script to have an error.

我们可以手动将列表保持顺序。但是这个列表可能会增长到非常大的甚至数据驱动的地方,我们没有办法。

决心og体育顺序

这是哪里 og体育于取决于 提供解决方案。它将占据一份项目列表,允许您定义它们相互og体育的方式,然后按顺序对项目进行排序。

I’m going to add a few more records and save them into $groups.

    $groups = @'
    [
        {
            "Name":"Group4",
            "Members":["Group3","Group2"]
        },
        {
            "Name":"Group3",
            "Members":"Group1"
        },
        {
            "Name":"Group2",
            "Members":"Group1"
        },
        {
            "Name":"Group1"
        }
    ]
    '@ | ConvertFrom-Json

Now we get to sort it with 决心og体育顺序.

    PS\> $groups | 决心og体育顺序 -Key {$_.Name} -DependsOn {$_.Members} |
                   Select-Object Name, Members

    Name   Members
    ----   -------
    Group1
    Group3 Group1
    Group2 Group1
    Group4 {Group3, Group2}

It processed the groups from top to bottom. When it identified that Group4 depended on Group3 and Group2, it made sure they were in the list ahead of Group4. It then checked Group3 to see that it depended on Group1. So it made sure that Group1 was in the list ahead of Group3. By the time it got to Group2, Group1 was already in the list so it was not added a 2nd time.

You may have noticed that Group3 and Group2 could have been processed in any order as long as Group1 was processed first. The secondary sort is based on order of discovery. Either by walking dependencies or processing the list. I just happened to discover Group3 before Group2.

参数

Taking a closer look at the 决心og体育顺序, we will see 2 important parameters. The Key defines how to to identify the object. The og体育于取决于 then defines how to to identify what your object depends on. So it needs to be able to match the og体育于取决于 values to the Key values. Both of these parameters are scriptblocks for maximum flexibility.

以下是一个第二个例子,使用和数组的Hashtables而不是对象:

    $familyTree = [ordered]@(
        @{Name='Girl';     og体育于取决于 ='Dad','Mom'}
        @{Name='Mom';      og体育于取决于 ='Dad'}
        @{Name='Dad';      og体育于取决于 ='Grandpa','Grandma'}
        @{Name='Grandpa';  og体育于取决于 =$null}
        @{Name='Grandma';  og体育于取决于 ='Grandpa'}
        @{Name='Boy';      og体育于取决于 ='Dad','Mom'}
    )

    $familyTree |
        决心og体育顺序 -Key {$_.name} -DependsOn {$_. og体育于取决于 } |
        ForEach-Object {$_.name}

这将产生此列表:

    Grandpa
    Grandma
    Dad
    Mom
    Girl
    Boy

og体育别名

The alias for 决心og体育顺序 is og体育于取决于 . I thing the name is a little long so I personally prefer the alias for this one.

    $familyTree |
         og体育于取决于  -Key {$_.name} -DependsOn {$_. og体育于取决于 } 

价值图

我的第一个例子预计数据有足够的信息来阻止它是自己的og体育性。我们还能够引用其他数据集或调用其他命令作为该映射进程的一部分。

这是我有一个例子,其中我想要在哈希表中使用外部og体育映射来订购的字符串列表。

    $list = @(
        'Girl'
        'Mom'
        'Grandpa'
        'Dad'
        'Grandma'
        'Boy'
    )

    $map = [ordered]@{
        'Girl'='Dad','Mom'
        'Mom'='Dad'
        'Grandpa' = $null
        'Dad'='Grandpa','Grandma'
        'Grandma'='Grandpa'
        'Boy'='Dad','Mom'
    }

    $list | 决心og体育顺序 -DependsOn {$map[$_]}

如果未指定键,则它将每个对象占据它的值。当您有一个字符串列表时,那就是使用的值。

我在这里使用了一个哈希特,但它很容易呼吁数据库或重置端点以进行og体育信息。

安装depedson.

该模块已发布到Psgallery。

    Install-Module  og体育于取决于  -Scope CurrentUser

关于og体育声

这是一个支持PowerShell核心的混合模块,并使用该模块 电源外壳 标准库。这也是第一个开源模块 loanddepot. 。我希望能够看到更多我们的模块和DSC资源,就像这样开放。

如果您为此模块找到了很好的用途,请告诉我。