Powershell数组到结构树

b4lqfgs4  于 2023-01-02  发布在  Shell
关注(0)|答案(1)|浏览(163)

我有一个数组,我想重新显示为一个结构层次:

Id  Name    ParentRoleId
00E6T000002G5sXUAS  APAC AE, Commercial 00E6T000002G3skUAC
00E6T000002G5scUAC  APAC AE, Enterprise 00E6T000002G3skUAC
00E0W000001urB6UAI  Account Executive, Industry Solutions   00E0W000001ur6pUAA
00E6T000002G2w7UAC  Commercial - Account Management Team 1  00E6T000002G2w2UAC
00E6T000002G61ZUAS  Commercial AE Roll-up   00E0W000002FqrtUAC
00E0W000001uhZBUAY  Commercial AE, Team 1   00E0W000001dhnOUAQ
00E6T000002G5s8UAC  Commercial AE, Team 2   00E0W000001uhZ6UAI
00E6T000002G5sIUAS  Commercial AE, Team 3   00E6T000002G5sDUAS
00E0W00000273AgUAI  Commercial East CSM Manager 00E0W000002G1qwUAC
00E6T000002G5zOUAS  Commercial East CSM Manager, Team 1 00E0W00000273AgUAI
00E0W00000273AlUAI  Commercial East CSM Rep 00E0W00000273AgUAI
00E6T000002G5zTUAS  Commercial East CSM, Team 1 00E6T000002G5zOUAS
00E0W000001dm1wUAA  Commercial - Account Management Team 2  00E6T000002G5tLUAS
00E0W0000023PkrUAE  Director, Mid-Market    00E0W000001yQAYUA2
00E0W000001dmP0UAI  Commercial Sales Engineer   00E0W000001dmOvUAI
00E0W000001dmOvUAI  Commercial Sales Engineer Manager   00E0W000001dio6UAA
00E0W000001dlGvUAI  Commercial West CSM Manager 00E0W000002G1qwUAC
00E6T000002G5xmUAC  Commercial West CSM Manager, Team 1 00E0W000001dlGvUAI
00E0W000001dlH0UAI  Commercial West CSM Rep 00E0W000001dlGvUAI
00E6T000002G5xwUAC  Commercial West CSM Rep, Team 1 00E6T000002G5xmUAC
00E6T000002G5FuUAK  CSM, EMEA   00E6T000002G5FpUAK
00E6T000002G5FpUAK  CSM Manager, EMEA   00E0W000002G1qwUAC
00Ed0000000ajHUEAY  Customer Success    00Ed0000000ZbFrEAK
00E6T000002G3I8UAK  Customer Growth Sales Engineer  00E6T000002G3I3UAK
00E6T000002G3I3UAK  Customer Growth Sales Engineer Manager  00E0W000001dio6UAA
00E0W000001dkxtUAA  Director, Commercial Account Management 00E0W000001uqtCUAQ
00E0W000001dhnOUAQ  Director, Commercial Team 1 - Rachel White  00E6T000002G61ZUAS
00E0W000001uhZ6UAI  Director, Commercial Team 2 - Joe DeFrance  00E6T000002G61ZUAS
00E6T000002G5sDUAS  Director, Commercial Team 3 - Maya Connet   00E6T000002G61ZUAS
00E6T000002G5smUAC  Director, EMEA Commercial   00E6T000001uuGJUAY
00E6T000002G5swUAC  Director, EMEA Enterprise   00E6T000001uuGJUAY
00E6T000002G2mRUAS  Director, Emerging Team 1 - Adam Wainwright 00E6T000002G61eUAC
00E6T000002G5sNUAS  Director, Emerging Team 2 - Kristen Meier   00E6T000002G61eUAC
00E0W000001ur9jUAA  Director, Enterprise Account Management 00E0W000001uqtCUAQ
00E0W000001yQAdUAM  Director, Enterprise Team 1 - Tatum Savage  00E0W000001yQAYUA2
00E6T000002G5tBUAS  Director, Enterprise Team 2 - Jason Plank   00E0W000001yQAYUA2
00E0W000001dkqnUAA  Director, Revenue Development   00E0W000001dioBUAQ
00E6T000002G2t8UAC  EMEA - Account Management   00E6T000002G61KUAS
00E6T000002G5srUAC  EMEA AE, Commercial 00E6T000002G5smUAC
00E6T000002G5t1UAC  EMEA AE, Enterprise 00E6T000002G5swUAC
00E6T000002G3IIUA0  EMEA Sales Engineer 00E6T000002G3IDUA0
00E6T000002G3IDUA0  EMEA Sales Engineer Manager 00E0W000001dio6UAA
00E6T000002G2DqUAK  EMEA Support    00E0W00000272uxUAA
00E6T000002G61eUAC  Emerging AE Roll-up 00E0W000002FqrtUAC
00E6T000002G2ntUAC  Emerging AE, Team 1 00E6T000002G2mRUAS
00E6T000002G5sSUAS  Emerging AE, Team 2 00E6T000002G5sNUAS
00E6T000002G2ymUAC  Enterprise - Account Management Team 1  00E6T000002G2yhUAC
00E6T000002G5t6UAC  Enterprise AE, Team 1   00E0W000001yQAdUAM
00E6T000002G5tGUAS  Enterprise AE, Team 2   00E6T000002G5tBUAS
00E0W000001uqtMUAQ  Enterprise - Account Management 00E0W000001ur9jUAA
00E0W00000273AWUAY  Enterprise East CSM Manager 00E0W000002G1qwUAC
00E0W00000273AbUAI  Enterprise East CSM Rep 00E0W00000273AWUAY
00Ed0000002JGNWEA4  Enterprise Sales Engineer   00E0W000001dmOqUAI
00E0W000001dmOqUAI  Enterprise Sales Engineer Manager   00E0W000001dio6UAA
00E0W000002G1qmUAC  Enterprise West CSM Manager 00E0W000002G1qwUAC
00E0W000002G1qrUAC  Enterprise West CSM Rep 00E0W000002G1qmUAC
00Ed0000000ZbFrEAK  Executive   00E0W000002B61KUAS
00E0W000001dioGUAQ  Finance 00Ed0000000ZbFrEAK
00E0W000001dio6UAA  Head of Sales Engineering   00Ed0000000ZbFrEAK
00E0W000001hzpnUAA  Implementation Reps 00Ed0000000ajHUEAY
00E6T000002G2DvUAK  India Support   00E6T000002G2E5UAK
00E6T000002G2E5UAK  India Support Manager   00E0W00000272uxUAA
00E0W000002Npc7UAC  InfoSec 00E0W000002B61KUAS
00E0W000001uVmIUAU  Integration 
00E6T000002G2w2UAC  Manager, Commercial Account Management  00E0W000001dkxtUAA
00E6T000002G5tLUAS  Manager, Commercial Account Management Team 2   00E0W000001dkxtUAA
00E6T000002G61KUAS  Manager, EMEA Account Management    00E0W000001dkxtUAA
00E6T000002G2yhUAC  Manager, Enterprise Account Management  00E0W000001ur9jUAA
00Ed0000002JGNMEA4  Marketing   00Ed0000000ZbFrEAK
00E6T000002G5tVUAS  Mid-Market - Account Manager    00E6T000002G61jUAC
00E0W000001uhtaUAA  Mid-Market AE   00E0W0000023PkrUAE
00E6T000002G61jUAC  Mid-Market AM Roll-up   00E0W000001dkxtUAA
00Ed0000002JGNREA4  Product Management  00Ed0000000ZbFrEAK
00E6T000002G5taUAC  RevDev Manager, ADM 00E0W000001dkqnUAA
00E6T000002G5G4UAK  RevDev Manager, EMEA    00E0W000001dkqnUAA
00E0W000001dkr2UAA  RevDev Manager, Emg / Comm  00E0W000001dkqnUAA
00E6T000002G5tzUAC  RevDev Manager, Industries / AMER Wingman   00E0W000001dkqnUAA
00E0W000001dkqsUAA  RevDev Manager, MM / Ent / India Wingman    00E0W000001dkqnUAA
00E6T000002G5tfUAC  RevDev Rep, ADM 00E6T000002G5taUAC
00E6T000001uuO8UAI  RevDev Rep, AMER Wingman    00E6T000002G5tzUAC
00E6T000002G7jIUAS  RevDev Rep, APAC    00E0W000001dkqsUAA
00E6T000002G5tuUAC  RevDev Rep, Commercial  00E0W000001dkr2UAA
00E6T000002G5tkUAC  RevDev Rep, EMEA Commercial 00E6T000002G5smUAC
00E6T000002G5tpUAC  RevDev Rep, EMEA Enterprise 00E6T000002G5G4UAK
00E0W000001dkr7UAA  RevDev Rep, Emerging    00E0W000001dkr2UAA
00E0W000001dkqxUAA  RevDev Rep, Enterprise  00E0W000001dkqsUAA
00E6T000002G7YyUAK  RevDev Rep, Executive Program   00E0W000001dkqnUAA
00E6T000001uuODUAY  RevDev Rep, India Wingman   00E0W000001dkqsUAA
00E6T000002G5u4UAC  RevDev Rep, Industries  00E6T000002G5tzUAC
00E6T000002G5u9UAC  RevDev Rep, Mid-Market  00E0W000001dkqsUAA
00Ed0000000ZbFwEAK  Sales Management    00Ed0000000ZbFrEAK
00E0W000002B61KUAS  Sales Operations/Admin  
00E6T000002G3INUA0  Solutions Engineering   00E0W000001dio6UAA
00E6T000002G74PUAS  Sr. Manager of Growth Strategy  00E0W000001dio6UAA
00E6T000002G2Y5UAK  Strategic CSM   00E0W000002G1r1UAC
00E0W000002G1r1UAC  Strategic CSM Team Lead 00E0W000002G1qwUAC
00E0W00000272uxUAA  Support Team    00Ed0000000ajHUEAY
00E6T000002G3TGUA0  SVP of Global Sales 00Ed0000000ZbFwEAK
00E6T000002G2E0UAK  US Support  00E0W00000272uxUAA
00E0W000001uqtCUAQ  VP, Account Management  00Ed0000000ZbFwEAK
00E0W000002FqrtUAC  VP Commercial and Emerging  00E6T000002G3TGUA0
00E0W000002G1qwUAC  VP Customer Success Management  00Ed0000000ajHUEAY
00E0W000001dioBUAQ  VP, Growth & Enablement 00Ed0000000ZbFrEAK
00E6T000001uuGJUAY  VP, International Sales 00E6T000002G3TGUA0
00E0W000001yQAYUA2  VP Mid-Market and Enterprise    00E6T000002G3TGUA0
00E6T000002G3skUAC  VP of Sales, APAC   00E6T000001uuGJUAY
00E0W000001ur6pUAA  VP, Verticals & Solutions   00E6T000002G3TGUA0
00E6T000001uuO3UAI  Wingman POD AE  00E6T000001uuNyUAI
00E6T000001uuNyUAI  Wingman POD Sales Management    00E0W000002FqrtUAC
00E6T000001uuSKUAY  Wingman-Side, Customer Success  00E0W000001uqtCUAQ
00E6T000001uu9NUAQ  Wingman-side Sales  00E6T000001uuNyUAI

层次结构的顶部是那些没有parentroleID的对象,我想循环遍历并获取子对象,然后显示如下:
姓名(id):

Integration (00E0W000001uVmIUAU)
Sales Operations/Admin (00E0W000002B61KUAS) 
  -Executive (00Ed0000000ZbFrEAK)
      -Finance (00E0W000001dioGUAQ)
      -Head of Sales Engineering (00E0W000001dio6UAA)
         -Enterprise Sales Engineer Manager (00E0W000001dmOqUAI)
         -Solutions Engineering (00E6T000002G3INUA0)
         -Sr. Manager of Growth Strategy... #...and so on

我创建了一个函数:

function GetChildren ($param)
    {
        Foreach ($x in $param) {
            ($hierarchyarray | where {$_.parentroleid -EQ $x}).id
        }
    }

并设置一个变量:$root = $层次数组|其中{$_.父代ID-EQ $null}
我可以通过使用$root.id调用函数来获取根的子对象,但我不确定如何递归地到达结构的底部

Thank you in advance
y53ybaqx

y53ybaqx1#

一种方法是:
为每个项目添加“Children”属性:

# add a "Children" property to each item
$data = $raw | % {
    write-output ([pscustomobject] [ordered] @{
        "Id"           = $_.Id
        "Name"         = $_.Name
        "ParentRoleId" = $_.ParentRoleId
        "Children"     = @()
    })
}

# populate the list of children for each item
foreach( $item in $data )
{
    if( $null -ne $item.ParentRoleId )
    {
        @($data | where-object { $_.Id -eq $item.ParentRoleId })[0].Children += $item;
    }
}

然后找到根节点并递归地遍历它们的子树:

$roots = $data | where-object { $null -eq $_.ParentRoleId }
foreach( $item in $roots )
{
    Write-Item $item 0
}

你需要这个函数来完成递归部分:

function Write-Item
{
    param( [pscustomobject] $Item, [int] $Depth )
    if( $Depth -eq 0 )
    {
        write-host (("  " * $Depth) + $Item.Name + " (" + $Item.Id + ")")
    }
    else
    {
        write-host (("  " * $Depth) + "-" + $Item.Name + " (" + $Item.Id + ")")
    }
    foreach( $child in $Item.Children )
    {
        Write-Item $child ($Depth+1)
    }
}

关键是每次向下移动一个子级别时,$Depth都将增加($Depth+1),这样输出就缩进了一个额外的级别。
使用示例数据,输出为:

Integration (00E0W000001uVmIUAU)
Sales Operations/Admin (00E0W000002B61KUAS)
  -Executive (00Ed0000000ZbFrEAK)
    -Customer Success (00Ed0000000ajHUEAY)
      -Implementation Reps (00E0W000001hzpnUAA)
      -Support Team (00E0W00000272uxUAA)
        -EMEA Support (00E6T000002G2DqUAK)
        -India Support Manager (00E6T000002G2E5UAK)
          -India Support (00E6T000002G2DvUAK)
        -US Support (00E6T000002G2E0UAK)
      -VP Customer Success Management (00E0W000002G1qwUAC)
        -Commercial East CSM Manager (00E0W00000273AgUAI)
          -Commercial East CSM Manager, Team 1 (00E6T000002G5zOUAS)
            -Commercial East CSM, Team 1 (00E6T000002G5zTUAS)
          -Commercial East CSM Rep (00E0W00000273AlUAI)
        -Commercial West CSM Manager (00E0W000001dlGvUAI)
          -Commercial West CSM Manager, Team 1 (00E6T000002G5xmUAC)
            -Commercial West CSM Rep, Team 1 (00E6T000002G5xwUAC)
          -Commercial West CSM Rep (00E0W000001dlH0UAI)
        -CSM Manager, EMEA (00E6T000002G5FpUAK)
          -CSM, EMEA (00E6T000002G5FuUAK)
        -Enterprise East CSM Manager (00E0W00000273AWUAY)
          -Enterprise East CSM Rep (00E0W00000273AbUAI)
        -Enterprise West CSM Manager (00E0W000002G1qmUAC)
          -Enterprise West CSM Rep (00E0W000002G1qrUAC)
        -Strategic CSM Team Lead (00E0W000002G1r1UAC)
          -Strategic CSM (00E6T000002G2Y5UAK)
    -Finance (00E0W000001dioGUAQ)
    -Head of Sales Engineering (00E0W000001dio6UAA)
      -Commercial Sales Engineer Manager (00E0W000001dmOvUAI)
        -Commercial Sales Engineer (00E0W000001dmP0UAI)
      -Customer Growth Sales Engineer Manager (00E6T000002G3I3UAK)
        -Customer Growth Sales Engineer (00E6T000002G3I8UAK)
      -EMEA Sales Engineer Manager (00E6T000002G3IDUA0)
        -EMEA Sales Engineer (00E6T000002G3IIUA0)
      -Enterprise Sales Engineer Manager (00E0W000001dmOqUAI)
        -Enterprise Sales Engineer (00Ed0000002JGNWEA4)
      -Solutions Engineering (00E6T000002G3INUA0)
      -Sr. Manager of Growth Strategy (00E6T000002G74PUAS)
    -Marketing (00Ed0000002JGNMEA4)
    -Product Management (00Ed0000002JGNREA4)
    -Sales Management (00Ed0000000ZbFwEAK)
      -SVP of Global Sales (00E6T000002G3TGUA0)
        -VP Commercial and Emerging (00E0W000002FqrtUAC)
          -Commercial AE Roll-up (00E6T000002G61ZUAS)
            -Director, Commercial Team 1 - Rachel White (00E0W000001dhnOUAQ)
              -Commercial AE, Team 1 (00E0W000001uhZBUAY)
            -Director, Commercial Team 2 - Joe DeFrance (00E0W000001uhZ6UAI)
              -Commercial AE, Team 2 (00E6T000002G5s8UAC)
            -Director, Commercial Team 3 - Maya Connet (00E6T000002G5sDUAS)
              -Commercial AE, Team 3 (00E6T000002G5sIUAS)
          -Emerging AE Roll-up (00E6T000002G61eUAC)
            -Director, Emerging Team 1 - Adam Wainwright (00E6T000002G2mRUAS)
              -Emerging AE, Team 1 (00E6T000002G2ntUAC)
            -Director, Emerging Team 2 - Kristen Meier (00E6T000002G5sNUAS)
              -Emerging AE, Team 2 (00E6T000002G5sSUAS)
          -Wingman POD Sales Management (00E6T000001uuNyUAI)
            -Wingman POD AE (00E6T000001uuO3UAI)
            -Wingman-side Sales (00E6T000001uu9NUAQ)
        -VP, International Sales (00E6T000001uuGJUAY)
          -Director, EMEA Commercial (00E6T000002G5smUAC)
            -EMEA AE, Commercial (00E6T000002G5srUAC)
            -RevDev Rep, EMEA Commercial (00E6T000002G5tkUAC)
          -Director, EMEA Enterprise (00E6T000002G5swUAC)
            -EMEA AE, Enterprise (00E6T000002G5t1UAC)
          -VP of Sales, APAC (00E6T000002G3skUAC)
            -APAC AE, Commercial (00E6T000002G5sXUAS)
            -APAC AE, Enterprise (00E6T000002G5scUAC)
        -VP Mid-Market and Enterprise (00E0W000001yQAYUA2)
          -Director, Mid-Market (00E0W0000023PkrUAE)
            -Mid-Market AE (00E0W000001uhtaUAA)
          -Director, Enterprise Team 1 - Tatum Savage (00E0W000001yQAdUAM)
            -Enterprise AE, Team 1 (00E6T000002G5t6UAC)
          -Director, Enterprise Team 2 - Jason Plank (00E6T000002G5tBUAS)
            -Enterprise AE, Team 2 (00E6T000002G5tGUAS)
        -VP, Verticals & Solutions (00E0W000001ur6pUAA)
          -Account Executive, Industry Solutions (00E0W000001urB6UAI)
      -VP, Account Management (00E0W000001uqtCUAQ)
        -Director, Commercial Account Management (00E0W000001dkxtUAA)
          -Manager, Commercial Account Management (00E6T000002G2w2UAC)
            -Commercial - Account Management Team 1 (00E6T000002G2w7UAC)
          -Manager, Commercial Account Management Team 2 (00E6T000002G5tLUAS)
            -Commercial - Account Management Team 2 (00E0W000001dm1wUAA)
          -Manager, EMEA Account Management (00E6T000002G61KUAS)
            -EMEA - Account Management (00E6T000002G2t8UAC)
          -Mid-Market AM Roll-up (00E6T000002G61jUAC)
            -Mid-Market - Account Manager (00E6T000002G5tVUAS)
        -Director, Enterprise Account Management (00E0W000001ur9jUAA)
          -Enterprise - Account Management (00E0W000001uqtMUAQ)
          -Manager, Enterprise Account Management (00E6T000002G2yhUAC)
            -Enterprise - Account Management Team 1 (00E6T000002G2ymUAC)
        -Wingman-Side, Customer Success (00E6T000001uuSKUAY)
    -VP, Growth & Enablement (00E0W000001dioBUAQ)
      -Director, Revenue Development (00E0W000001dkqnUAA)
        -RevDev Manager, ADM (00E6T000002G5taUAC)
          -RevDev Rep, ADM (00E6T000002G5tfUAC)
        -RevDev Manager, EMEA (00E6T000002G5G4UAK)
          -RevDev Rep, EMEA Enterprise (00E6T000002G5tpUAC)
        -RevDev Manager, Emg / Comm (00E0W000001dkr2UAA)
          -RevDev Rep, Commercial (00E6T000002G5tuUAC)
          -RevDev Rep, Emerging (00E0W000001dkr7UAA)
        -RevDev Manager, Industries / AMER Wingman (00E6T000002G5tzUAC)
          -RevDev Rep, AMER Wingman (00E6T000001uuO8UAI)
          -RevDev Rep, Industries (00E6T000002G5u4UAC)
        -RevDev Manager, MM / Ent / India Wingman (00E0W000001dkqsUAA)
          -RevDev Rep, APAC (00E6T000002G7jIUAS)
          -RevDev Rep, Enterprise (00E0W000001dkqxUAA)
          -RevDev Rep, India Wingman (00E6T000001uuODUAY)
          -RevDev Rep, Mid-Market (00E6T000002G5u9UAC)
        -RevDev Rep, Executive Program (00E6T000002G7YyUAK)
  -InfoSec (00E0W000002Npc7UAC)

相关问题