How do I run a Powershell script in Control-M? - INCLUDES VIDEO
Knowledge Article
How do I run a Powershell script in Control-M? - INCLUDES VIDEO
This process will also explain how to get the return code / exit code from a Powershell script to be captured by Control-M/Agent
Control-M/Agent for UNIX and Microsoft Windows
Control-M/Agent for UNIX and Microsoft Windows
All versions of Control-M/Agent ; Windows
How do I run a Powershell script in Control-M?
Control-M does not have Powershell native support to call .ps1 scripts directly such as running yourscript.ps1 Scripts ultimately must be called as a .bat or .cmd script to invoke the powershell.exe command within.
Version 9.0.21:
See new functionality introduced in version 9.0.21 in the link below for OS Job Type. https://documents.bmc.com/supportu/9.0.21/en-US/Documentation/OS_Job_parameters.htm
Versions before 9.0.21 (these options are still possible with 9.0.21):
1) Create an OS type job
2) The What field can be set to Command, Script, or Embedded Script, but each will need to be configured as follows:
Type Command: (Recommended) Command = powershell.exe -file script.ps [parameters] For example: powershell -file C:\Scripts\Example_script.ps1 -drive C:\ -rootFolder Scripts\logs -fileType all
Type Script: File Path = C:\Path\To\Your\Script File Name: script.cmd Where script.cmd is a batch script you create, that calls the Powershell script with its full path, similar to the Command below in script.cmd powershell -file C:\Scripts\Example_script.ps1 -drive C:\ -rootFolder Scripts\logs -fileType all Note: The full path to ps1 cannot be omitted.
Type Embedded Script: Script contents: powershell.exe -file script.ps1 [parameters] File Name = scriptname.cmd NOTE: scriptname.cmd is not an actual script you create, but will be created behind the scenes as a wrapper script by Control-M/Agent with the Script contents above.
NOTE: Within the .ps1 Powershell script make sure the "Exit" command is the last to run so that it passes your final return code to Windows which will be captured by the Control-M/Agent Sample .ps1 script demonstrating this concept, which in this example will return a code of 5 to Control-M/Agent ================ If (Test-NetConnection invalidhostname -port 2345 -InformationLevel Quiet) { exit 0 } else { exit 5 } ================
Additional Information:
Depending on the Windows PATH environment variable, the full path to the powershell executable may be needed as well as to execute the script successfully
For example: C:\Windows\System32\WindowsPowerShell\v1.0\powershell -file C:\Scripts\Example_script.ps1
Work with the script developer to write the script and run it manually outside of Control-M so to ensure it will complete correctly, using the format above: powershell.exe -file script.ps [parameters]
The Control-M Support Team does not provide any support on scripting
The following video demonstrates this solution using Type Command:
The following RFE has been opened to request the possibility for the agent to submit powershell scripts without passing through the cmd.exe layer allowing more flexibility in preparing CTM jobs with embedded powershell scripts. CTM-3491 - Need ability to execute Powershell scripts also embedded in job without passing through the CMD.exe layer