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
$ns = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$service = $ns.GetServiceDirectly("localhost")
$searcher = $service.OpenObject("Adaxes://$containerDN", $null, $null, 0)
$searcher.Criteria = New-AdmCriteria "group" {name -contains "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 = $service.OpenObject($groupPath, $null, $null, 0)
$group.Add($userPath)
}
}
finally
{
# Release resources
$searchResultIterator.Dispose()
}
See also
- Writing ADSI scripts
- Server-side scripting
- How to build criteria
- IAdmDirectorySearcher
- IAdmSearchResultIterator
- IAdmSearchResult
- Online script repository