Created
January 21, 2025 12:05
-
-
Save HCRitter/112c100e41176778f90344cf03ef82df to your computer and use it in GitHub Desktop.
This PowerShell script creates a hierarchical list of objects with DisplayName, ID, and ParentID properties, organized into three levels. It also builds a HashTable to store these objects and calculates a FullPath property for each object by recursively resolving the hierarchy using the ParentID. The FullPath represents the hierarchical path fro…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define the list of objects | |
$objects = @( | |
# Level 1 | |
[PSCustomObject]@{ DisplayName = "Root1"; ID = 1; ParentID = $null } | |
[PSCustomObject]@{ DisplayName = "Root2"; ID = 2; ParentID = $null } | |
# Level 2 | |
[PSCustomObject]@{ DisplayName = "Child1.1"; ID = 3; ParentID = 1 } | |
[PSCustomObject]@{ DisplayName = "Child1.2"; ID = 4; ParentID = 1 } | |
[PSCustomObject]@{ DisplayName = "Child2.1"; ID = 5; ParentID = 2 } | |
[PSCustomObject]@{ DisplayName = "Child2.2"; ID = 6; ParentID = 2 } | |
# Level 3 | |
[PSCustomObject]@{ DisplayName = "GrandChild1.1.1"; ID = 7; ParentID = 3 } | |
[PSCustomObject]@{ DisplayName = "GrandChild1.1.2"; ID = 8; ParentID = 3 } | |
[PSCustomObject]@{ DisplayName = "GrandChild1.2.1"; ID = 9; ParentID = 4 } | |
[PSCustomObject]@{ DisplayName = "GrandChild2.1.1"; ID = 10; ParentID = 5 } | |
[PSCustomObject]@{ DisplayName = "GrandChild2.1.2"; ID = 11; ParentID = 5 } | |
[PSCustomObject]@{ DisplayName = "GrandChild2.2.1"; ID = 12; ParentID = 6 } | |
# Additional objects to make 15 total | |
[PSCustomObject]@{ DisplayName = "Child1.3"; ID = 13; ParentID = 1 } | |
[PSCustomObject]@{ DisplayName = "GrandChild1.3.1"; ID = 14; ParentID = 13 } | |
[PSCustomObject]@{ DisplayName = "GrandChild1.3.2"; ID = 15; ParentID = 13 } | |
) | |
# Create a HashTable to store the objects | |
$ObjectsHashTable = @{} | |
# Loop through the objects and create a HashTable | |
foreach($Object in $Objects){ | |
$ObjectsHashTable[$Object.ID] = [PSCustomObject]@{ | |
DisplayName = $Object.DisplayName | |
ID = $Object.ID | |
ParentID = $Object.ParentID | |
# Create a full path for the object by looking up the parent object in the HashTable using the ParentID | |
FullPath = $( | |
if($null -eq $Object.ParentID){ | |
$Object.DisplayName | |
}else { | |
$ObjectsHashTable[$Object.ParentID].FullPath + '\' + $Object.DisplayName | |
} | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment