logo

NJP

Execute Powershell Script from ServiceNow

Import · Mar 24, 2020 · article

In this article, i will explain how you can call a powershell script from the ServiceNow instance.

Here is a sample powershell script which will print the name of the person that is selected in the RITM. A very basic example.

Powershell Script

#Variables

$name = $args[0]
$ritm = $args[1]
#when passing the argument, we are replacing spaces with -. So replace hyphen(-) with spaces back.

$DisplayName =  $name -replace "-"," "

Write-Host $ritm"-"
Write-host "Mentioned name is" $DisplayName

Now store this powershell script into the midserver folder in this path.

powershell scripts\\PowerShell\\myService.ps1

The script is created and stored, now we need to call this from ServiceNow.

Let us create 1 sample RITM where you mention the name that need to be printed in the powershell. The user will mention the Name in the input field and submit. Then SN shall take that name and pass it to the powershell script.

Write a Business Rule after insert on RITM table and call the powershell script.

Business Rule - Creates ECC Queue output entry

(function executeRule(current, previous /*null when async*/ ) {
  var ritm = current.number;
  var name = current.u_name;
  name = name.replaceAll(" ","-"); //replace spaces with hyphen(-)
  var ecc = new GlideRecord("ecc_queue");
  ecc.initialize();//to create record
  ecc.agent = "mid.server.YOUR_MIDSERVER_NAME"; 
  ecc.topic = "Command";
  var value = "powershell scripts\\PowerShell\\myService.ps1 "+name+ " " +ritm;
  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();
})(current, previous);

Once the RITM is created, the BR executes and trigger an entry in ECC Queue with the queue output and state Ready.

The output entry in ECC Queue means an communication from instance to Mid Server. When the Mid server process that entry, it changes the output entry state to processed and it creates another entry in the ECC Queue with queue as input and the output of the powershell script is stored in the payload field of the ECC Queue input entry.

Business Rule - Parse ECC Queue Input Entry

Create a BR on the ecc queue table after insert with conditions

Queue: Input

Name contains myService.ps1

state: Ready

and in the script, add this code.

(function executeRule(current, previous /*null when async*/ ) {
   // Add your code here. Fetch the ritm number from the powershell script output and update the status to closed complete.
   var name = gs.getXMLText(current.payload, "//stdout");
   data = name.split("-");
   var ritmNo = data[0].trim();
   var PS_output = data[1].trim();
   var ritm = new GlideRecord("sc_req_item");
   ritm.addQuery("number", ritmNo);
   ritm.query();
   if (ritm.next()) {
       //update request item to closed complete
       ritm.state = 3; //closed complete
       ritm.comments.setJournalEntry(PS_output); //store PS output here.
       ritm.update()
   }
})(current, previous);

Let me know if you have any questions in the comments.

Mark the article as helpful and bookmark if you found it useful.

View original source

https://www.servicenow.com/community/developer-articles/execute-powershell-script-from-servicenow/ta-p/2319634