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.
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() }
}