Script Repository


Execute custom command with name specified in user property

February 25, 2021
362

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 Custom Command
$customCommandsContainerPath = $Context.GetWellKnownContainerPath("CustomCommands")
$searcher = $Context.BindToObject($customCommandsContainerPath)
$searcher.SearchFilter = "(&(objectClass=adm-CustomCommand)(name=$propertyValue))"
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.PageSize = 2
$searcher.VirtualRoot = $False

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 called $propertyValue.", "Warning")
        return
    }
    
    # Get Custom Command ID
    $customCommand = $Context.BindToObject($searchResults[0].AdsPath)
    $customCommandID = $customCommand.CommandID
    
    # Execute Custom Command for the user
    $user = $Context.BindToObjectByDNEx("%distinguishedName%", $True)
    $user.ExecuteCustomCommand($customCommandID)
}
finally
{
    # Release resources
    if ($searchResultIterator){ $searchResultIterator.Dispose() }
}

Comments ( 0 )
No results found.
Leave a comment