Skip to end of metadata
Go to start of metadata
Icon

In version 7.2 and newer, Powershell scripts can be run natively by Men & Mice by starting the command with "powershell", "powershell.exe" or simply with the path to the .ps1 file. Powershell can then read the stdin with [Console]::In.ReadToEnd()

While the information below works for any version of Men & Mice, it is not necessary for version 7.2 or later

Problem:

Unfortunately, it is not possible to execute external or scheduled scripts written in Powershell directly. If Powershell is directly invoked, it will exit immediately without doing anything.

Solution:

It is possible to execute external or scheduled scripts written in Powershell indirectly by utilizing the visual basic script psExec.vbs, which in turn executes the Powershell script. 

External scripts:

External scripts receive xml on stdin. The xml contains information about the object that is changing and is necessary for the external scripts. psExec.vbs forwards the xml to the Powershell script by writing it to a temporary .xml file, which is then removed once the Powershell script is done. 

To execute the Powershell script, copy psExec.vbs and the Powershell script to the M&M Central data directory (usually C:\ProgramData\Men and Mice\Central. See this for alternate locations). Then use the following statement in the System Settings>External Commands

Where it is assumed that there exists an administrative user called "ScriptRunner", with a complex password. Every time the script is run, a new temporary password is generated for the ScriptRunner user, forwarded to the script as the arguments $u and $p (username and password). The forwarded credentials can then be used to invoke e.g. the SOAP API inside of the script.

The Powershell script must have the following parameters declaration (additional parameters can be added, but the three first must be retained)

myExternalScript.ps1

 

Scheduled scripts:

Scheduled scripts are different from external scripts in that they don't receive any xml on stdin. Therefore psExec.vbs needs to be triggered to not wait for the xml on stdin, which can be done by adding a trailing "n" at the end of the string, i.e.

 

The Powershell script now only requires two parameters for the username and temporary password

myScheduledScript.ps1
Icon

For testing purposes, scheduled scripts can be triggered by editing the schedule and setting the start date on a past date. This will trigger the script to run immediately.

 

 

 

  • No labels