The CreatorCon Call for Content is officially open! Get started here.

powershell inside script include

sammarcow
Mega Expert

I am having trouble finding examples of executing PowerShell inside a script include, returning powershell script ouput.   We have the orchestration package but that seems to only allow for what we need within the context of a workflow.     An ideal simple example would contain a variable that is passed from the script include into the powershell, and append some text so we know we receive output from the script.  

Powershell test.ps1 located in Mid Server | Script Files:

$parmPS = $env:SNC_fromScriptInclude ; #this is assigned from caller of PS, the script include  

Write-Host "$parmPS modified inside PS";  

Script Include:

var fromScriptInclude = "script include variable";

var output= //Execute script test.ps1 command needed - should pass a variable named "fromScriptInclude", may be several lines to send output from ECC and receive corresponding input from ECC

gs.log('output from script was ' + output','testPSInsideScriptInclude');

Correct log output should read "script include variable modified inside PS".

Message was edited by: William Sammarco fixed typo.

Message was edited by: William Sammarco added clarity to logging message

1 ACCEPTED SOLUTION

TrevorK
Kilo Sage

I have to give credit to John James Anderson - his blog explains it perfectly and is exactly what you want. And saves me the trouble of typing it out: PowerShell Probe and Utility for ServiceNow-John Andersen



We use something similar to this to have our instance talk to the MID Server to execute powershell commands via script.


View solution in original post

4 REPLIES 4

TrevorK
Kilo Sage

I have to give credit to John James Anderson - his blog explains it perfectly and is exactly what you want. And saves me the trouble of typing it out: PowerShell Probe and Utility for ServiceNow-John Andersen



We use something similar to this to have our instance talk to the MID Server to execute powershell commands via script.


Thank you for linking this.   I parted ways with this tool last week because the link was down but it has since been restored.         I will be posting an alternate solution which does not require this tool shortly.      


The script include that is with the tool does all the work - you are able to just call it in your code like this:


var mid = "MidServer1Development";


var server = "127.0.0.1";


var script = 'invoke-sqlcmd -Query "' + query_string + '" -ServerInstance "RMSDB" -Database "RMSDB"';


var pb = new PowershellProbe(mid, server);


pb.setScript(script);


var response = pb.execute(true);


Thank you, after reviewing the payloads from the update set I realized my plan was pretty much to recreate this work provided... so I will likely just use this!   Will post a followup and select answer once work is verified complete.   Thank you!