PS递归函数生成完整路径

问题描述 投票:0回答:1

从 API 我得到以下 PSobjects 数组

$testgroups

total device_groups
----- -------------
 1062 {@{id=-2ZPmENMTTSmfNxuGd2Emw; name=Server; description=; created=27/06/2024 10:07:39; modified=27/06/2024 10:07:39; parent_id=T5dNIARCR7OnuzW8IIAsWg...},

$testgroups.device_groups[0..2]

id          : -2ZPmENMTTSmfNxuGd2Emw
name        : Server
description : 
created     : 27/06/2024 10:07:39
modified    : 27/06/2024 10:07:39
parent_id   : T5dNIARCR7OnuzW8IIAsWg

id          : -6XA_2FOSZSw5Bk6HXLewQ
name        : SBC
description : 
created     : 27/06/2024 10:07:40
modified    : 27/06/2024 10:07:40
parent_id   : M8ZBfSjGQtamIAJZpVI7Cw

id          : -GaDYTJbSPaid64tWUDQ_A
name        : RT SCOM_GW
description : group for testing
created     : 27/06/2024 10:07:41
modified    : 27/06/2024 10:07:41
parent_id   : CbuM2_KDQ82BBPlS4lx_Mg

如您所见,有一个

parent_id
属性引用其父组。 “root”组的属性为空。 我正在绞尽脑汁地制作一个递归函数来生成每个组的完整路径,但没有设法让任何东西发挥作用。

理想情况下,新的“fullPath”属性的格式可能如下所示:

root\subgroup1\subgroup2
对于每个对象

powershell loops recursion object-graph
1个回答
0
投票

设法让它发挥作用。如果我的需求和解释不够清楚,请原谅。

目标是创建一个新属性(fullPathName),为每个组生成完整路径,查找每个父组,直到找到根组。例如,我们有 2 个 psobject,其中一个是另一个的“父对象”。

$testgroups.device_groups | Where-Object { $_.id -eq "F8S_MJ4tSPi-D_Eoofgo2w" -or $_.id -eq "Fmp5838YRsyElHM27PdZww" }}

id           : F8S_MJ4tSPi-D_Eoofgo2w
name         : subgroup 1
description  : 
created      : 04/07/2024 14:19:00
modified     : 04/07/2024 14:19:00
parent_id    : Fmp5838YRsyElHM27PdZww

id           : Fmp5838YRsyElHM27PdZww
name         : Aurelien policies tests
description  : 
created      : 04/07/2024 14:18:52
modified     : 04/07/2024 14:18:52
parent_id    : gBuOolHrTt64XdEmfRM60A

以下递归函数成功了

function Get-NameChain {
    param (
        [PSCustomObject]$CurrentGroup,
        [Array]$AllGroups,
        [String]$Chain = ""
    )
    # If the current group is root (no parent_id), prepend its name to the chain.
    if (-not $CurrentGroup.parent_id) {
        if ($Chain -eq "") {
            return $CurrentGroup.name # If chain is empty, it's the root group.
        } else {
            return $CurrentGroup.name + "\" + $Chain # Prepend root name to chain.
        }
    } else {
        # Find the parent group.
        $ParentGroup = $AllGroups | Where-Object { $_.id -eq $CurrentGroup.parent_id }
        if ($ParentGroup) {
            # If there's a parent, prepend the parent's name to the chain and recurse.
            $NewChain = if ($Chain -eq "") { $CurrentGroup.name } else { $CurrentGroup.name + "\" + $Chain }
            return Get-NameChain -CurrentGroup $ParentGroup -AllGroups $AllGroups -Chain $NewChain
        } else {
            # If no parent found (which shouldn't happen), return the current chain.
            return $Chain
        }
    }
}

以下命令循环遍历每组以使用递归函数

$testgroups.device_groups | ForEach-Object {
             $FullNameChain = Get-NameChain -CurrentGroup $_ -AllGroups $testgroups.device_groups -Chain ""
             $_ | Add-Member -NotePropertyName "fullPathName" -NotePropertyValue $FullNameChain -Force               
         }
© www.soinside.com 2019 - 2024. All rights reserved.