Skip to content

Searching Organizational Units

The following code sample finds Organizational Units with empty Managed By property and outputs their names.

[Reflection.Assembly]::LoadWithPartialName("Softerra.Adaxes.Adsi")

$domainDN = "DC=domain,DC=com"

# Connect to the Adaxes service
$admNS = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$admService = $admNS.GetServiceDirectly("localhost")

$searcher = $admService.OpenObject("Adaxes://$domainDN", $NULL, $NULL, 0)

$searcher.SearchFilter = "(&(objectCategory=organizationalUnit)(!(managedBy=*)))"
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
try
{
    # Execute search
    $searchResultIterator = $searcher.ExecuteSearch()

    foreach ($searchResult in $searchResultIterator.FetchAll())
    {
        $ouPath = $searchResult.AdsPath
        $ou = $admService.OpenObject($ouPath, $NULL, $NULL, 0)
        Write-Host $ou.Name
    }
}
finally
{
    # Release resources
    $searchResultIterator.Dispose()
}
Import-Module Adaxes

$domainDN = "DC=domain,DC=com"

$organizationalUnits = Get-AdmOrganizationalUnit -Filter {managedby -notlike "*"} `
    -SearchBase $domainDN -Server "domain.com" `
    -SearchScope Subtree -AdaxesService localhost

foreach ($ou in $organizationalUnits)
{
    Write-Host $ou.Name
}

See also