Executing custom commands

The following code samples provide examples of how to execute custom commands with and without parameters.

No parameters

[Reflection.Assembly]::LoadWithPartialName("Softerra.Adaxes.Adsi")

# Connect to the Adaxes service
$admNS = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$admService = $admNS.GetServiceDirectly("localhost")

# Bind to the user
$userDN = "CN=John Smith,CN=Users,DC=company,DC=com"
$user = $admService.OpenObject("Adaxes://$userDN", $NULL, $NULL, 0)

# Execute custom command
$commandId = "{9DB88EC3-1241-4AB1-9612-C7C982BAA49F}"
$user.ExecuteCustomCommand($commandId)

In the above code sample, the $commandId variable represents the identifier of a custom command. For information on how to get the identifier, see Get custom command identifier.

Text and checkbox parameters

[Reflection.Assembly]::LoadWithPartialName("Softerra.Adaxes.Adsi")

# Connect to the Adaxes service
$admNS = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$admService = $admNS.GetServiceDirectly("localhost")

# Bind to the custom command
$commandDN = "CN=My Command,CN=Custom Commands," + 
             "CN=Configuration Objects,CN=Adaxes Configuration,CN=Adaxes"
$command = $admService.OpenObject("Adaxes://$commandDN", $NULL, $NULL, 0)

# Create custom command arguments
$arguments = $command.CreateArguments()

# Specify parameter values
$arguments.SetParameterValue("param-textParameter", "MyValue")
$arguments.SetParameterValue("param-checkboxParameter", $True)

# Bind to the user
$userDN = "CN=John Smith,OU=My OU,DC=company,DC=com"
$user = $admService.OpenObject("Adaxes://$userDN", $NULL, $NULL, 0)

# Execute the custom command
$user.ExecuteCustomCommand($command.CommandID, $arguments)

Checkbox list parameter

[Reflection.Assembly]::LoadWithPartialName("Softerra.Adaxes.Adsi")

# Connect to the Adaxes service
$admNS = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$admService = $admNS.GetServiceDirectly("localhost")

# Bind to the custom command
$commandDN = "CN=My Command,CN=Custom Commands," + 
             "CN=Configuration Objects,CN=Adaxes Configuration,CN=Adaxes"
$command = $admService.OpenObject("Adaxes://$commandDN", $NULL, $NULL, 0)

# Create custom command arguments
$arguments = $command.CreateArguments()

# Specify checked items for the parameter
$itemOneId = "ADAXESPARAMID:{96f07f9e-092b-4f8a-803c-e7f03601740b}"
$itemTwoId = "ADAXESPARAMID:{5ef7b316-7b56-4063-96bb-b235609ad8e5}"
$checkedItemIds = @($itemOneId, $itemTwoId)
$arguments.SetParameterValue("param-checkboxListParameter", $checkedItemIds)

# Bind to the user
$userDN = "CN=John Smith,OU=My OU,DC=company,DC=com"
$user = $admService.OpenObject("Adaxes://$userDN", $NULL, $NULL, 0)

# Execute the custom command
$user.ExecuteCustomCommand($command.CommandID, $arguments)

In the above code sample, the $itemOneId and $itemTwoId variables represent identifiers of checkboxes in the list. For information on how to get the identifiers, see Viewing the parameters of a custom command.

[Reflection.Assembly]::LoadWithPartialName("Softerra.Adaxes.Adsi")

# Connect to the Adaxes service
$admNS = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$admService = $admNS.GetServiceDirectly("localhost")

# Bind to the custom command
$commandDN = "CN=My Command,CN=Custom Commands," + 
             "CN=Configuration Objects,CN=Adaxes Configuration,CN=Adaxes"
$command = $admService.OpenObject("Adaxes://$commandDN", $NULL, $NULL, 0)

# Create custom command arguments
$arguments = $command.CreateArguments()

# Specify parameter value
$itemId = "ADAXESVALUEID:{db2744b0-ace2-4022-bfc2-53de5ec5f7c6}"
$arguments.SetParameterValue("param-dropDownListParameter", $itemId)

# Bind to the user
$userDN = "CN=John Smith,OU=My OU,DC=company,DC=com"
$user = $admService.OpenObject("Adaxes://$userDN", $NULL, $NULL, 0)

# Execute the custom command
$user.ExecuteCustomCommand($command.CommandID, $arguments)

In the above code sample, the $itemId variable represents the identifier of an item in the drop-down list. For information on how to get the identifier, see Viewing parameters of a custom command.

Note

To execute custom commands with parameters, the above code samples use the distinguished names (DNs) of the commands to bind to them. However, DNs of custom commands can change (e.g. when a command is renamed or moved to another container). As an alternative, you can bind to a custom command using its identifier that is immutable. For information on how to get the identifier, see Get custom command identifier.

 How
[Reflection.Assembly]::LoadWithPartialName("Softerra.Adaxes.Adsi")

# Connect to the Adaxes service
$admNS = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$admService = $admNS.GetServiceDirectly("localhost")

$commandId = "{9d1c5786-4dbf-4ae9-bbdc-1429be13f936}"

try
{
    # Bind to the 'Custom Commands' container
    $customCommandsPath = $admService.Backend.GetConfigurationContainerPath("CustomCommands")
    $searcher = $admService.OpenObject($customCommandsPath, $NULL, $NULL, 0)
    
    # Search custom command by ID
    $guidBytes = (New-Object "System.Guid" $commandId).ToByteArray()
    $filterPart = [Softerra.Adaxes.Ldap.FilterBuilder]::Create("adm-CustomCommandID", $guidBytes)
    $searcher.SearchFilter = "(&(objectClass=adm-CustomCommand)$filterPart)"
    $searcher.SearchScope = "ADS_SCOPE_SUBTREE"

    # Execute search
    $searchResultIterator = $searcher.ExecuteSearch()
    $searchResults = $searchResultIterator.FetchAll()

    if ($searchResults.Length -eq 0)
    {
        Write-Warning "Custom command with ID $commandId not found."
        return
    }
    else
    {
        # Bind to the custom command
        $commandPath = $searchResults[0].AdsPath
        $command = $admService.OpenObject($commandPath, $NULL, $NULL, 0)
    }
}
finally
{
    # Release resources
    if ($searchResultIterator){ $searchResultIterator.Dispose() }
}

See also