We use cookies to improve your experience.
By your continued use of this site you accept such use.
For more details please see our privacy policy and cookies policy.

Script Repository

Run script in new PowerShell instance

May 12, 2023 Views: 23872

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.
  • $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
Leave a comment
Loading...

Got questions?

Support Questions & Answers