{"id":108,"date":"2023-01-23T14:39:09","date_gmt":"2023-01-23T13:39:09","guid":{"rendered":"https:\/\/sys-code-alpha.com\/?p=108"},"modified":"2023-01-23T14:39:09","modified_gmt":"2023-01-23T13:39:09","slug":"get-ad-ou-distingushied-name-quickly","status":"publish","type":"post","link":"https:\/\/sys-code-alpha.com\/?p=108","title":{"rendered":"Get AD OU Distingushied Name quickly"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>\r\nFunction Get-OUDistinguishedName{\r\n\r\n    &lt;#\r\n    .SYNOPSIS\r\n    Returns DN name for every OU that matches OUName parameter.\r\n\r\n    .DESCRIPTION\r\n    Returns DN name for every OU that matches OUName parameter.\r\n\r\n    .EXAMPLE\r\n    PS> Get-OUDistinguishedName -OUName OU_2\r\n    OU=OU_2,DC=test,DC=local\r\n\r\n    #>\r\n\r\n    Param(\r\n        &#91;string&#91;]]$OUName,\r\n        &#91;switch]$Recurse,\r\n        &#91;string]$DomainController,\r\n        &#91;psobject]$Credential\r\n    )\r\n\r\n    BEGIN{}\r\n    PROCESS{\r\n\r\n        foreach($OU in $OUName){\r\n\r\n            IF($DomainController){\r\n                IF($Recurse){\r\n                    $OU1 = (Get-ADOrganizationalUnit -Server $DomainController -Cred $Credential -Filter {name -like $OU}).DistinguishedName\r\n                    $OU1 | %{ (Get-ADOrganizationalUnit -Server $DomainController -Cred $Credential -LDAPFilter '(name=*)' -SearchBase $_ -SearchScope Subtree).DistinguishedName }\r\n                    }ELSE{\r\n                    (Get-ADOrganizationalUnit -Server $DomainController -Cred $Credential -Filter {name -like $OU}).DistinguishedName\r\n                    }\r\n\r\n            }ELSE{\r\n                IF($Recurse){\r\n                    $OU1 = (Get-ADOrganizationalUnit -Filter {name -like $OU}).DistinguishedName\r\n                    $OU1 | %{ (Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase $_ -SearchScope Subtree).DistinguishedName }\r\n                    }ELSE{\r\n                    (Get-ADOrganizationalUnit -Filter {name -like $OU}).DistinguishedName\r\n                    }\r\n            }\r\n        }        \r\n\r\n    }\r\n    END{}\r\n\r\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=\/wp\/v2\/posts\/108"}],"collection":[{"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=108"}],"version-history":[{"count":1,"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=\/wp\/v2\/posts\/108\/revisions"}],"predecessor-version":[{"id":109,"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=\/wp\/v2\/posts\/108\/revisions\/109"}],"wp:attachment":[{"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}