public/Get-RBACComponent.ps1

Function get-RBACComponent {
    [CmdletBinding(DefaultParameterSetName='None')]
    Param
    (
        [Parameter(ParameterSetName="Mock",Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline)]
        [Parameter(ParameterSetName="None", ValueFromPipelineByPropertyName, ValueFromPipeline)]
        [ArgumentCompleter( {
            param ( $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters )
            if ($fakeBoundParameters.containsKey('Component')) {
                (get-rbacComponent -org "$wordToComplete*" -component $fakeBoundParameters.component | sort-object -unique Org).org
            } else {
                (get-rbacOrg -org "$wordToComplete*").Org
            }
          })]
        [String]$Org,

        [Parameter(ParameterSetName="Mock",Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline)]
        [Parameter(ParameterSetName="None", ValueFromPipelineByPropertyName, ValueFromPipeline)]
        [ArgumentCompleter( {
            param ( $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters )
            if ($fakeBoundParameters.containsKey('Org')) {
                (get-rbacComponent -org $fakeBoundParameters.Org -component "$wordToComplete*"  | sort-object -unique Component).Component
            } else {
                (get-rbacComponent -component "$wordToComplete*").Component
            }
        })]
        [String]$Component,

        # Don't search AD-- mock what you would get.
        [Parameter(ParameterSetName="Mock",ValueFromPipelineByPropertyName, ValueFromPipeline)]
        [switch]$Mock,

        # Mock the org as well rather than searching AD.
        [Parameter(ParameterSetName="Mock",ValueFromPipelineByPropertyName, ValueFromPipeline)]
        [switch]$MockOrg,

        [Parameter(ParameterSetName="Mock",ValueFromPipelineByPropertyName, ValueFromPipeline)]
        [String]$Description="Mock Description",

        [switch]$Detailed
    )

    BEGIN {
      # $OrgPath = $Settings['OUPaths']['OrgsBase']
    }

    PROCESS {
        if ($PsItem.org) {
            $Org = $_.Org
        } elseif ($PsItem) {
            $Org = $_
        }
        if ($org) {
            $OrgList = get-rbacOrg -org $Org -mock:$mockOrg
        } else {
            $OrgList = Get-RBACOrg
        }

        if ($Component) {
            $filter = "name -like '$Component'"
        } Else {
            $filter = "*"
        }

        ForEach ($Item in $OrgList) {
            $SearchBase = "OU={0},{1}" -f $Settings['Names']['ComponentsOU'], $Item.distinguishedName
            $OrgName = $item.org
            $SearchParams = @{
                SearchBase = $SearchBase
                SearchScope = "OneLevel"
                Filter = $filter
            }
            $OUList = $(
                if ([bool]$mock) {
                    @{
                        Name = $Component
                        Description = $Description
                        DistinguishedName = "OU={0},OU={1},OU={2},{3}" -f $Component, $settings['Names']['ComponentsOU'], $org, $settings['OUPaths']['OrgsBase']
                    }
                } else {
                    get-adorganizationalUnit @SearchParams -properties description | foreach-object {
                        @{
                            Name = $_.name
                            DistinguishedName = $_.distinguishedName
                            Description = $_.description
                        }
                    }
                 }
             ) | foreach-object {
                $BaseDN = $_.distinguishedName
                $Children = $(
                    if ([bool]$mock) {
                        $ComponentTemplate['LDAPContainers']
                    } else {
                        get-adorganizationalUnit -searchBase $BaseDN @SearchParams -filter "*"| foreach-object {
                            @{
                                DistinguishedName = $_.distinguishedName
                                Description = $_.description
                            }
                        }
                    }
                ) | foreach-object {
                    [pscustomobject]$_
                } | resolve-rbacchildren -baseDN $baseDN
                [PSCustomObject]@{
                    Name = $_.name
                    Type = "Component"
                    Org = $orgName
                    Component = $_.name
                    Description = $_.Description
                    DistinguishedName = $_.distinguishedName
                    Path = split-ldappath -DistinguishedName $_.DistinguishedName -Parent
                    ObjectMidName = "{0}-{1}" -f $orgName,$_.name
                    Children = $Children
                    Parents = @{
                        Global = $(get-rbacGlobal)
                        Org = $(get-rbacOrg -org $orgName -detailed -mock:$mockOrg)
                    }
                }
                $thisComponent
            }
            if (-not [bool]$Detailed) {
                $OUList | select-object Component,Org,Description,DistinguishedName
            } else {
                $OUList
            }
        }
    }
}