Script repository

Execute custom command with name specified in user property

Updated on: Jan 18, 2026, Views: 2989

Miscellaneous

The script executes a custom command for a user. The name of the command is taken from a property of the user. To execute the script, create a business rule, custom command or scheduled task configured for the User object type.

In the script, the $propertyName variable specifies the name of the property storing the custom command name.

$propertyName = "title" # TODO: modify me

# Get property value.
try
{
    $propertyValue = $Context.TargetObject.Get($propertyName)
}
catch
{
    $Context.LogMessage("The property $propertyName is not specified.", "Information")
    return
}

# Search parameters
$customCommandsContainerPath = $Context.GetWellKnownContainerPath("CustomCommands")
$searcher = $Context.BindToObject($customCommandsContainerPath)
$searcher.Criteria = New-AdmCriteria "adm-CustomCommand" -Expression {name -eq $propertyValue}
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
$searcher.SizeLimit = 2

try
{
    # Execute search
    $searchResultIterator = $searcher.ExecuteSearch()
    $searchResults = $searchResultIterator.FetchAll()
    
    if ($searchResults.Length -eq 0)
    {
        $Context.LogMessage("Custom command $propertyValue not found.", "Warning")
        return
    }
    
    if ($searchResults.Length -ge 2)
    {
        $Context.LogMessage("Found more than one custom command named $propertyValue.", "Warning")
        return
    }
    
    # Get custom command identifier.
    $customCommand = $Context.BindToObject($searchResults[0].AdsPath)
    $customCommandID = $customCommand.CommandID
    
    # Execute custom command.
    $user = $Context.BindToObjectByDNEx("%distinguishedName%", $True)
    $user.ExecuteCustomCommand($customCommandID, $null)
}
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.