Skip to content

Instantly share code, notes, and snippets.

@IISResetMe
Created February 12, 2019 12:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IISResetMe/d5e417eb70b825fa4d0fcfb447c8e746 to your computer and use it in GitHub Desktop.
Save IISResetMe/d5e417eb70b825fa4d0fcfb447c8e746 to your computer and use it in GitHub Desktop.
Recursively dereference powershell instance members without actually resorting to recursion
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