-
-
Save IISResetMe/d43354102e862b2e9457849df626059b 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 hidden or 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 = @{} | |
$Objects |ForEach-Object { $ObjectsHashTable[$_.ID] = $_ } | |
# Add a script property that looks up it's own parent via the table | |
$Objects |Add-Member -MemberType ScriptProperty -Name FullPath -Value { | |
@( | |
if ($this.ParentID -and $ObjectsHashTable.Contains($this.ParentID)){ | |
$ObjectsHashTable[$this.ParentID].FullPath | |
} | |
$this.DisplayName | |
) -join '\' | |
} -SecondValue { throw [InvalidOperationException]::new('Hands off!') } -Force -PassThru |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment