Script repository

Set manager based on property value

Updated on: Jan 18, 2026, Views: 2396

Managers and subordinates

The script finds a user according to the property values mapping and sets the user as the manager of the target account. To execute the script, create a business rule, custom command or scheduled task configured for the User object type.

Parameters

  • $propertyName - the name of the property whose values will be used to find the manager for the target account.
  • $valueMap - maps target user property values with the values of the managers.
  • $pipelined - set to $true to pass the update through Adaxes pipeline to create log records, apply business rules, security roles, etc. Set to $false to perform the update directly in AD (Adaxes functionality will not be applied).
$propertyName = "title" # TODO: modify me
$valueMap = @{
    "IT" = "Director of IT"
    "Support" = "Director of Support"
} # TODO: modify me
$pipelined = $True # TODO: modify me

# Get user property value
try
{
    $propertyValue = $Context.TargetObject.Get($propertyName)
}
catch
{
    $Context.LogMessage("Property $propertyName is not specified for user %fullname%.", "Warning")
    return
}

# Get manager property value
foreach ($item in $valueMap.GetEnumerator())
{
    if ($item.Name -ne $propertyValue)
    {
        continue
    }
    
    $managerValue = $item.Value
    break    
}

if ([System.String]::IsNullOrEmpty($managerValue))
{
    $Context.LogMessage("No manager value is specified for value $propertyValue.", "Warning")
    return
}

# Search parameters
$searcher = $Context.TargetObject
$searcher.Criteria = New-AdmCriteria "user" -Expression {$propertyName -eq $managerValue}
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.SizeLimit = 2
$searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
$searcher.VirtualRoot = $True

try
{
    # Execute search
    $searchResultIterator = $searcher.ExecuteSearch()
    $searchResults = $searchResultIterator.FetchAll()
    
    if ($searchResults.Length -eq 0)
    {
        $Context.LogMessage("Could not find user with $propertyName equal $managerValue.", "Warning")
        return
    }
    elseif ($searchResults.Length -eq 2)
    {
        $Context.LogMessage("Found more than one user with $propertyName equal $managerValue.", "Warning")
        return
    }
    
    # Get manager DN
    $managerDN = $searchResults[0].GetPropertyByName("distinguishedName").Values[0]
    
    # Update target user
    $user = $Context.BindToObjectByDNEx("%distinguishedName%", $pipelined)
    $user.Put("manager", $managerDN)
    $user.SetInfo()
}
finally
{
    # Release resources
    if ($searchResultIterator){ $searchResultIterator.Dispose() }
}

Comments 0

You must be signed in to comment.

    Got questions?

    Support Questions & Answers

    We use cookies to improve your experience.
    By your continued use of this site you accept such use.
    For more details please see our privacy policy and cookies policy.