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.

Script repository

Execute custom command with name specified in user property

September 06, 2023 Views: 1279

The script executes a custom command for a user. The name of the command is taken from a property of the user. The script can be executed in a custom command, scheduled task or business rule triggering after an operation (e.g. After creating a user).

Parameters:

  • $propertyName- Specifies the LDAP name of the property from which the name of the custom command will be obtained.
Edit Remove
PowerShell
$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.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
Leave a comment
Loading...

Got questions?

Support Questions & Answers