public/Remove-RBACOrg.ps1

Function Remove-RBACOrg {
    [CmdletBinding(SupportsShouldProcess=$true)]
    Param
    (
        [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, ValueFromPipeline = $true)]
        [ValidateScript({[bool](get-rbacOrg -org $_)})]
        [ArgumentCompleter( {(get-rbacOrg).Org})]
        [String]$Org
    )
    Begin {
        
        $shouldProcess = @{
            Confirm = [bool]($ConfirmPreference -ne "none")
            Whatif = [bool]($WhatIfPreference.IsPresent)
        }
        $ComputerTargetPath = "OU={0},OU={1},{2}" -f $ComputersOU, $GlobalOUStruct.name, $GlobalOUStruct.path
    }

    Process {
        if ($PsItem.org) {
            $Org = $_.Org
        } elseif ($PsItem) {
            $Org = $_
        }

        if ($Org) {
            $OrgList = get-rbacOrg -org $org
        } else {
            $OrgList = get-rbacOrg
        }
        foreach ($orgObject in $orgList) {        
            write-Host ("Moving any computer objects out to Global @ {0}" -f $ComputerTargetPath )
                $EndpointList = get-RBACComponentEndpoints -org $orgObject.Org
                foreach ($endpoint in $endpointList) {
                    $MoveParams = @{
                        identity = $endpoint.distinguishedName
                        TargetPath = $ComputerTargetPath
                    }
                    $moveParams
                    move-adobject @MoveParams @shouldProcess
                }
                $RemovePath = $OrgObject.DistinguishedName
                if ($PSCmdlet.ShouldProcess($removePath,"Removing protection and deleting OU Subtree")) {
                    DeleteOUSubtreeWithConfirm -path $removePath @ShouldProcess
                }
        }
    }
}