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
$ns = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$service = $ns.GetServiceDirectly("localhost")

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

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

In the above code sample, the $commandId variable represents the identifier of a custom command it is immutable, and will not change even after the configuration backup/restore. For information on how to get the identifier, see Get custom command identifier.

With parameters

To execute custom commands with parameters, you have to bind to them. Starting from Adaxes 2023.2, you can directly bind to custom commands via their immutable identifier, adm-CustomCommandId. In older versions, you can instead search for a custom command with the required identifier, and bind to the command using its ADS path.

 How to find a custom command by identifier and bind to it
[Reflection.Assembly]::LoadWithPartialName("Softerra.Adaxes.Adsi")

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

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

try
{
    # Bind to the 'Custom Commands' container
    $customCommandsPath = $service.Backend.GetConfigurationContainerPath("CustomCommands")
    $searcher = $service.OpenObject($customCommandsPath, $null, $null, 0)
    
    # Search custom command by ID
    $searcher.Criteria = New-AdmCriteria "adm-CustomCommand" {adm-CustomCommandId -eq $commandId}
    $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 = $service.OpenObject($commandPath, $null, $null, 0)
    }
}
finally
{
    # Release resources
    if ($searchResultIterator){ $searchResultIterator.Dispose() }
}

Text and checkbox parameters

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

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

# Bind to the custom command
$commandId = "{9DB88EC3-1241-4AB1-9612-C7C982BAA49F}"
$command = $service.OpenObject("Adaxes://<GUID=$commandId>", $null, $null, 0)

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

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

# Bind to the user
$userDN = "CN=John Smith,OU=My OU,DC=company,DC=com"
$user = $service.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
$ns = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$service = $ns.GetServiceDirectly("localhost")

# Bind to the custom command
$commandId = "{9DB88EC3-1241-4AB1-9612-C7C982BAA49F}"
$command = $service.OpenObject("Adaxes://<GUID=$commandId>", $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 = [String[]]@($itemOneId, $itemTwoId)
$arguments.SetParameterValue("param-checkboxListParameter", $checkedItemIds)

# Bind to the user
$userDN = "CN=John Smith,OU=My OU,DC=company,DC=com"
$user = $service.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
$ns = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$service = $ns.GetServiceDirectly("localhost")

# Bind to the custom command
$commandId = "{9DB88EC3-1241-4AB1-9612-C7C982BAA49F}"
$command = $service.OpenObject("Adaxes://<GUID=$commandId>", $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 = $service.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.

Object picker parameter

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

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

# Bind to the custom command
$commandId = "{9DB88EC3-1241-4AB1-9612-C7C982BAA49F}"
$command = $service.OpenObject("Adaxes://<GUID=$commandId>", $null, $null, 0)

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

# Specify parameter value
$objectDNs = @("CN=My Group,OU=Groups,DC=company,DC=com", "CN=My Group 2,OU=Groups,DC=company,DC=com")
$arguments.SetParameterValue("param-objectPickerParameter", $objectDNs)

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

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

See also