DirectorySearcher

The DirectorySearcher class is used to perform directory search queries.

Namespace: Softerra.Adaxes.Adsi.Search

Implements: IAdmDirectorySearcher

This class can only be used in PowerShell scripts executed in business rules, custom commands, scheduled tasks, and reports.

Constructors

Methods

  • Method

  • Description

  • ExecuteSearch()

  • Performs a directory search and returns an instance of the IAdmSearchResultIterator interface that can be used to iterate through search results.

  • AddCriteria()

  • Adds the provided search criteria to the currently used criteria with the AND operator.

Properties

  • Property

  • Description

  • Pipelined

  • Gets a value indicating whether to hide search results that the searching user does not have permission to view.

  • SearchParameters

  • Gets or sets the parameters for the directory search.

  • BaseObjectPath

  • Gets or sets the path of the directory object from which the search begins.

Details

DirectorySearcher()

Initializes a new instance of the DirectorySearcher class.

DirectorySearcher()

ExecuteSearch()

Performs a directory search and returns an instance of the IAdmSearchResultIterator interface that can be used to iterate through search results.

public IAdmSearchResultIterator ExecuteSearch()

Exceptions

The InvalidOperationException is the most typical exception this method can throw. It can have several reasons.

Examples

The following code sample searches for users whose Department is Sales and exports their names and descriptions to a CSV file. The script can be executed as a part of a business rule, custom command, or scheduled task.

$csvFilePath = "\\SERVER\Share\Report.csv"

# Create an instance of the DirectorySearcher class.
$searcher = New-Object "Softerra.Adaxes.Adsi.Search.DirectorySearcher" $null, $false

# Search parameters

# Search in the OU targeted by a business rule, scheduled task, or custom command.
$searcher.SearchParameters.BaseObjectPath = $Context.TargetObject.AdsPath
$searcher.SearchParameters.SearchScope = "ADS_SCOPE_SUBTREE"

# Search for users from the sales department.
$searcher.SearchParameters.Criteria = New-AdmCriteria "user" {department -eq "Sales"}

# Retrieve only the name and description.
$searcher.SetPropertiesToLoad(@("name", "description"))

try
{
    # Execute search.
    $searchResultIterator = $searcher.ExecuteSearch()
    
    # Fetch results.
    $searchResults = $searchResultIterator.FetchAll()
    
    $csv = @()
    foreach ($searchResult in $searchResults)
    {
        $htable = @{
            Name = $searchResult.Properties["name"].Value;
            Description = $searchResult.Properties["description"].Value;
        }
        $userInfo = New-Object PSObject -Property $htable
        $csv += $userInfo
    }
    
    # Export to CSV.
    $csv | Export-Csv $csvFilePath -NoTypeInformation
}
finally
{
    # Release resources used by the search.
    $searchResultIterator.Dispose()
}

AddCriteria()

Adds the provided search criteria to the currently used criteria with the AND operator.

public void AddCriteria(Criteria criteria)

Parameters

  • criteria – specifies the criteria to add.

Examples

The following code sample refines the search criteria.

# Create an instance of the DirectorySearcher class.
$searcher = New-Object "Softerra.Adaxes.Adsi.Search.DirectorySearcher" $null, $false

# Specify the base criteria.
$searcher.SearchParameters.Criteria = New-AdmCriteria -Type "contact" # Contacts

# Narrow down the criteria.
$criteriaToAdd = New-AdmCriteria "contact" {mail -empty $false} # Contacts with email address
$searcher.AddCriteria($criteriaToAdd)

Pipelined

Gets a value indicating whether to hide search results that the searching user does not have permission to view.

  • If true, results will be returned according to the security roles assigned to the user.
  • If false, all results will be returned.
  • Type:
  • bool
  • Access:
  • Read-only

SearchParameters

Gets or sets the parameters for the directory search.


BaseObjectPath

Gets or sets the path of the directory object from which the search begins. The path must be a valid ADS path string.

  • Type:
  • string
  • Access:
  • Read/Write

Requirements

Minimum required version: 2023

See also