How to run powershell MID Server Script Files

Bendezium
Tera Expert

I have a MID Server Script File, myScript.ps1. I'm trying to execute this from Background Script. The javascript will eventually be moved into a Scheduled Job or a Script Action. 

I can execute this no problem in powershell probe utility. I can also use javascript to make a powershell probe and execute arbitrary powershell commands no problem. What I can't seem to figure out is how to execute a synced MID server script file by name. Bonus points if you know how to pass in params.

This works:

var midservername = 'myMid'

try {
    var pb = new PowershellProbe(midservername, "127.0.0.1");
    var script = "Get-Date"
    pb.setScript(script);
    var response = pb.execute(true);
    gs.log(JSON.stringify(response));
} catch (e) {
    gs.log('ERROR running Powershell Probe:')
    gs.log(e)
}

//OUTPUT
//{"output":"Monday, May 10, 2021 6:48:28 PM","error":"null"}

Referencing a named MID file fails:

var midservername = 'myMid'

try {
    var pb = new PowershellProbe(midservername, "127.0.0.1");
    var script = "myScript.ps1"
    pb.setScript(script);
    var response = pb.execute(true);
    gs.log(JSON.stringify(response));
} catch (e) {
    gs.log('ERROR running Powershell Probe:')
    gs.log(e)
}

{"output":"null","error":"The term 'myScript.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.HRESULT: [-2146233087]"}

I have read the John Andersen link but it doesn't dive into how to run script files. Anybody know how to do this?

1 ACCEPTED SOLUTION

Bendezium
Tera Expert

Solved this one on my own, there were a number of problems.

  1. Set the Parent directory of the script file correctly.
  2. Use ".\" to begin searching the current working directory.
  3. Escape all \ with a \\

So final script was:

var midservername = 'myMid'

try {
    var pb = new PowershellProbe(midservername, "127.0.0.1");
    var script = ".\\scripts\\PowerShell\\myScript.ps1"
    pb.setScript(script);
    var response = pb.execute(true);
    gs.log(JSON.stringify(response));
} catch (e) {
    gs.log('ERROR running Powershell Probe:')
    gs.log(e)
}

{"output":"Hello World","error":"null"}

View solution in original post

4 REPLIES 4

Maik Skoddow
Tera Patron
Tera Patron

Hi @Bendezium ,

you can find the answer at an article by another community user: https://community.servicenow.com/community?id=community_article&sys_id=0cf18778dbb348d4f7fca851ca961...

Kind regards
Maik

If my answer replied your question please mark appropriate response as correct so that the question will appear as resolved for other users who may have a similar question in the future.

This was helpful Maik, but did not solve my problem and also took quite a bit of effort to make this work as script that is run ad-hoc instead of based off business rules.

Bendezium
Tera Expert

Solved this one on my own, there were a number of problems.

  1. Set the Parent directory of the script file correctly.
  2. Use ".\" to begin searching the current working directory.
  3. Escape all \ with a \\

So final script was:

var midservername = 'myMid'

try {
    var pb = new PowershellProbe(midservername, "127.0.0.1");
    var script = ".\\scripts\\PowerShell\\myScript.ps1"
    pb.setScript(script);
    var response = pb.execute(true);
    gs.log(JSON.stringify(response));
} catch (e) {
    gs.log('ERROR running Powershell Probe:')
    gs.log(e)
}

{"output":"Hello World","error":"null"}


Hi,

Please mark your own answer as correct answer as this issue is resolved with steps you have provided in the response.
This will help other community member to follow correct guidelines.

I appreciate your efforts to resolve this issue by yourself and sharing the resolution with us.

Thank you
Prasad