
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2021 03:02 PM
Hello All,
I am in middle to completing integration between ServiceNow and ASP.Net Web Service.
I already have powershell script which calls this ASP.net web service and returns the response based on parameters.
This powershell script has just 1 function which returns array of values based on input function parameter.
I want to call this powershell script from servicenow server scripts so that i can pase response from powershell script in servicenow.
How to call Powershell script in servicenow by passing function name and function parameters?
I tried below code to call this powershell script via ECC queue.But, this is not working.I am getting error response in ECC queue input payload after calling PS script using below code.
If you see below code, the PS script name is getData.ps1 and function in this PS script i want to call is Get-DiscountData.
Also, i need to pass parameter while calling function in this PS Script.
var employeeID = 'xxdd';
var ecc = new GlideRecord("ecc_queue");
ecc.initialize();//to create record
ecc.agent = "mid.server.xxxx";
ecc.source = gs.getUserName();
ecc.topic = "Command";
var value = "powershell scripts\\PowerShell\\getData.ps1; Get-DiscountData -Param1 " + employeeID;
ecc.payload = '<?xml version="1.0" encoding="UTF-8"?><parameters><parameter name="name" value="'+value+'"/><parameter name="skip_sensor" value="true"/></parameters>';
ecc.queue = "output";
ecc.state = "ready";
ecc.insert();
Please let me know way to call Powershell script with function and parameter from servicenow server scripting.
Flow Designer Powershell custom action is out of scope due to some reasons.
Regards,
Sachin
Solved! Go to Solution.
- Labels:
-
Integrations
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2021 12:51 PM
I figured below way to call PS script with function and parameters.
Below sample code can be used if anyone needs it.
gs.print(callMid());
function callMid(){
var pb = new PowershellProbe("xxx-mid-dev", "127.0.0.1");
var script ="";
script = "import-module c:\\scripts\\ccc\\ccc.psm1\n"+"GetByEmployeeID dev 8799"+ "\n";
pb.setScript(script);
pb.setMaxWait(300);
var response = pb.execute(true);
gs.print("response is " + response.output);
if (response.error != "null" && typeof response.error != "undefined")
{
return "ERROR: "+response.error;
}
return response.output;
}
Regards,
Sachin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2021 10:19 PM
What error are you receiving?
Can you confirm the folder path your script is stored in?
var value = "powershell scripts\\PowerShell\\getData.ps1; Get-DiscountData -Param1 " + employeeID;
I believe the default path for storing powershell scripts on the MID is "\scripts\PowerShell\". Possible to try your script like this?
var value = "scripts\PowerShell\getData.ps1; Get-DiscountData -Param1 " + employeeID;

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2021 12:12 AM
Hello Sachin,
I am not sure if you can call a function directly from ecc queue. But you can call the PS script and in the argument pass a function name along with any other values that you want to pass it.
Then in your PS you can check like this
#write-host "hello world"
function testing($str) {
Write-Host "The name is "$str
}
$str = $args[0];
if($str -eq "discount_data") {
testing($args[1])
}
powershell scripts\\PowerShell\\getData.ps1 "discount_data" employeeID;
Mark the comment as a correct answer and helpful once worked.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2021 12:51 PM
I figured below way to call PS script with function and parameters.
Below sample code can be used if anyone needs it.
gs.print(callMid());
function callMid(){
var pb = new PowershellProbe("xxx-mid-dev", "127.0.0.1");
var script ="";
script = "import-module c:\\scripts\\ccc\\ccc.psm1\n"+"GetByEmployeeID dev 8799"+ "\n";
pb.setScript(script);
pb.setMaxWait(300);
var response = pb.execute(true);
gs.print("response is " + response.output);
if (response.error != "null" && typeof response.error != "undefined")
{
return "ERROR: "+response.error;
}
return response.output;
}
Regards,
Sachin

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2021 02:25 AM
Ah okay. so you are calling PowershellProbe. Nice to know about this.