Script Repository


Run script in new PowerShell instance

November 16, 2017
2555

The following examples show how to launch a script from a Business Rule, Custom Command or Scheduled Task in a new PowerShell instance. Running a script in a separate instance allows you to use the latest version of PowerShell installed on the computer where Adaxes service runs, even if it is not yet supported by Adaxes. Also, you can use the approach to perform operations that take longer than the 10 minutes' limit applied by Adaxes to scripts executed on the server side.

Using Start-Process

The following example shows how to run a separate instance using the Start-Process cmdlet. This approach allows you to overcome the 10 minutes' limit set by Adaxes Service, however it does not allow retrieving any data from the external instance.

Parameters:

  • $waitTimeMilliseconds - specifies the time during which Adaxes will wait for the script to complete. It is recommended not to set a time exceeding the 10 minutes' limit applied by Adaxes to scripts executed by Business Rules, Custom Commands and Scheduled Tasks. If a script runs for more time than you specify, it will be completed, but the errors, warnings and other messages will not be added to the Execution Log;
  • $scriptBlock - specifies the PowerShell script that will be executed in the new PowerShell instance.
Edit Remove
PowerShell
$waitTimeMilliseconds = 9 * 60 * 1000 # TODO: modify me

# Script to execute in the new PowerShell instance
$scriptBlock = {
    Import-Module Adaxes

    $resource = "https://api.example.com/users" # TODO: modify me
    $userDN = "CN=John Doe,CN=Users,DC=example,DC=com" # TODO: modify me
    
    $user = Get-AdmUser -Identity $userDN -AdaxesService localhost | ConvertTo-Json
    Invoke-RestMethod -Method Post -Uri $resource -Body $user
} # TODO: modify me

# Start a new instance of Windows PowerShell and run the script
$powershellPath = "$env:windir\system32\windowspowershell\v1.0\powershell.exe"
$process = Start-Process $powershellPath -NoNewWindow -ArgumentList ("-ExecutionPolicy Bypass -noninteractive -noprofile " + $scriptBlock) -PassThru
$process.WaitForExit($waitTimeMilliseconds)

Using Invoke-Command

The following example shows how to run a separate instance using the Invoke-Command cmdlet. This approach allows you to retrieve data from the external instance, however the 10 minutes' limit set by Adaxes Service must be observed.

Note: Before using this approach, make sure that the computer where Adaxes Service is installed allows execution of remote PowerShell commands. For details, see About Remote Requirements.

Parameter:

  • $scriptBlock - specifies the PowerShell script that will be executed in the new PowerShell instance.

Edit Remove
PowerShell
# Script to execute in the new PowerShell instance
$scriptBlock = {
    Import-Module MyPsModule

    $myCmdletResult = Get-MyValue -User "%sAMAccountName%" -Initiator "%adm-InitiatorFullName%"

    return $myCmdletResult
}  # TODO: modify me

$result = Invoke-Command -ComputerName localhost -ScriptBlock $scriptBlock

if ($result -ne $NULL)
{
    $Context.LogMessage($result, "Information")
}

Comments ( 0 )
No results found.
Leave a comment