Skip to content

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
$admNS = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$admService = $admNS.GetServiceDirectly("localhost")

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

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

    # Move users to the Organizational Unit
    $targetOU = $admService.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