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

# Bind to the custom command
$commandDN = "CN=My Command,CN=Custom Commands," + 
             "CN=Configuration Objects,CN=Adaxes Configuration,CN=Adaxes"
$command = $service.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", 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
$commandDN = "CN=My Command,CN=Custom Commands," + 
             "CN=Configuration Objects,CN=Adaxes Configuration,CN=Adaxes"
$command = $service.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 = $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
$commandDN = "CN=My Command,CN=Custom Commands," + 
             "CN=Configuration Objects,CN=Adaxes Configuration,CN=Adaxes"
$command = $service.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 = $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
$commandDN = "CN=My Command,CN=Custom Commands," + 
             "CN=Configuration Objects,CN=Adaxes Configuration,CN=Adaxes"
$command = $service.OpenObject("Adaxes://$commandDN", $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)

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

See also