Skip to content

Searching groups

The following code sample find groups whose name contains Accounting and add a user to the groups.

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

$containerDN = "CN=Users,DC=domain,DC=com"
$userPath = "Adaxes://CN=John Smith,CN=Users,DC=domain,DC=com"

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

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

$searcher.SearchFilter = "(&(objectCategory=group)(name=*Accounting*))"
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
try
{
    # Execute search
    $searchResultIterator = $searcher.ExecuteSearch()

    # Add the user to groups
    foreach ($searchResult in $searchResultIterator.FetchAll())
    {
        $groupPath = $searchResult.AdsPath
        $group = $admService.OpenObject($groupPath, $NULL, $NULL, 0)
        $group.Add($userPath)
    }
}
finally
{
    # Release resources
    $searchResultIterator.Dispose()
}

See also