Searching computers
The following code sample finds computer accounts whose name ends with SERVER and moves the computers to a specific organizational unit.
[Reflection.Assembly]::LoadWithPartialName("Softerra.Adaxes.Adsi")
$containerDN = "CN=Computers,DC=domain,DC=com"
$targetOUDN = "OU=TargetOU,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 "computer" {name -endsWith "SERVER"}
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
try
{
# Execute search
$searchResultIterator = $searcher.ExecuteSearch()
# Move users to the organizational unit
$targetOU = $service.OpenObject("Adaxes://$targetOUDN", $null, $null, 0)
foreach ($searchResult in $searchResultIterator.FetchAll())
{
$computerPath = $searchResult.AdsPath
$targetOU.MoveHere($computerPath, $null)
}
}
finally
{
# Release resources
$searchResultIterator.Dispose()
}
See also
- Writing ADSI scripts
- Server-side scripting
- How to build criteria
- IAdmDirectorySearcher
- IAdmSearchResultIterator
- IAdmSearchResult
- Online script repository