PowerShell inside BatchController
Requirements and restrictions to run PowerShell scripts inside the Batchcontroller:
- PowerShell version:
The PowerShell version on the system where the BatchController is running must be at least PowerShell 7.4.1
Within PowerShell the version is displayed by running:
$PSVersionTable - Execution Policy:
The execution policy need to be set to RemoteSigned with the scope of LocalMachine
This can be achieved by running the following command in PowerShell with Administrators rights:
set-executionpolicy remotesigned -Scope LocalMachine - MyInvocation:
Usage of "$MyInvocation" inside the scripts and Asysco script libraries must be removed, as not all properties are filled because of limits from Microsoft automation.
For example when you have the following code:
$ScriptPath = (Split-Path $MyInvocation.MyCommand.Path)
.$ScriptPath"\AmtSettings.ps1"
It must be changed to:
.".\AmtSettings.ps1"
The script has the folder it is located in, set as current folder by default.
Getting the script name is typically coded as follows:
$Base = Get-Item $myInvocation.ScriptName | select BaseName
return $Base.BaseName
Which can be changed to:
$AmtScriptName = "$env:AMTJOBNAME"
if (-not ([String]::IsNullOrEmpty($AmtScriptName))) { return $AmtScriptName
} - Exit statement:
Any "Exit" statement must also set a PowerShell variable "$Global:AmtExitCode" for the Batchcontroller to read it correctly:
Change from:
Exit $ExitCode
To:
$Global:AmtExitCode = $ExitCode Exit $ExitCode
For more information about execution policies, see execution policy on the Microsoft website.
When run inside the Batchcontroller, the environment variable "AMTJOBNAME" is set to the active script.