Created
February 12, 2019 12:08
-
-
Save IISResetMe/d5e417eb70b825fa4d0fcfb447c8e746 to your computer and use it in GitHub Desktop.
Recursively dereference powershell instance members without actually resorting to recursion
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
function Resolve-MemberChain | |
{ | |
param( | |
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] | |
[psobject[]]$InputObject, | |
[Parameter(Mandatory = $true, Position = 0)] | |
[string[]]$MemberPath, | |
[Parameter(Mandatory = $false)] | |
[string]$Delimiter | |
) | |
begin { | |
if($PSBoundParameters.ContainsKey('Delimiter')){ | |
$MemberPath = $MemberPath.Split([string[]]@($Delimiter)) | |
} | |
} | |
process { | |
foreach($o in $InputObject){ | |
foreach($m in $MemberPath){ | |
$o = $o.$m | |
} | |
$o | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment