Script repository

Execute custom command on user with username identical to the target user

Updated on: Jan 18, 2026, Views: 4365

Miscellaneous

The script executes a custom command on a user with the same username as the target user, but located in another AD domain. To execute the script, create a business rule, custom command or scheduled task configured for the User object type.

Parameters

  • $commandID - the identifier of the custom command to execute. For information on how to get the identifier, see Get custom command identifier.
  • $resourceDomainDN - the distinguished name (DN) of the domain where the account to execute custom command on is located. For information on how to get the DN, see Get the DN of a directory object.
$commandID = "{9db88ec3-1241-4ab1-9612-c7c982baa49f}" # TODO: modify me
$resourceDomainDN = "DC=domain,DC=com" # TODO: modify me

# Find user with the same username in the resource domain.
$searcher = $Context.BindToObjectByDN($resourceDomainDN)
$searcher.Criteria = New-AdmCriteria "user" -Expression {sAMAccountName -eq "%username%"}
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
$searcher.SizeLimit = 1

try
{
    $searchResultIterator = $searcher.ExecuteSearch()
    $searchResults = $searchResultIterator.FetchAll()
    
    if ($searchResults.Count -eq 0)
    {
        $Context.LogMessage("Cannot find a user with username '%username%' in the resource domain", "Warning")
        return
    }
    
    # Run the custom command on the user.
    $user = $Context.BindToObjectEx($searchResults[0].AdsPath, $True)
    $user.ExecuteCustomCommand($commandID, $null)
}
finally
{
    # Release resources
    $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.