Introduction

It has been no secret that you can do amazing feats of integration when coupling ServiceNow with a local MID Server. MID Servers give us the ability to launch remote commands, load custom JAR files, etc.

One little-known fact about ServiceNow MID-Servers is that they can also leverage PowerShell v2.0 on compatible Microsoft Windows devices. This has started to gain traction in the Runbook Automation product for ServiceNow, but you can also leverage this power for your custom integrations to third party Windows products that have API’s that are compatible with PowerShell.

I have created a Powershell library, and an accompanying utility, that can be uploaded to your ServiceNow instance to make your life easier when it comes to either PowerShell integrations or Runbook development with PowerShell workflow steps.

Pre-Requisites

You must have a MID Server installed on a Microsoft Windows machine with PowerShell v. 2.0 setup. Also, if you have Enhanced Windows Security enabled, you must unblock the Powershell modules in the MID Server. See this link: Unblocking the MID Server Powershell Modules

You must also set “Powershell” as a capability on the MID Server.

For remote powershell execution (executing powershell scripts on a Windows computer that is NOT the one hosting the MID Server) you will need to have the Runbook Automation product from ServiceNow and set up the credentials for the remote device.

PowershellProbe Library

The Update Set provided here will create a Script Include called “PowershellProbe”. This library allows you to quickly and easily generate PowerShell commands through the MID Server.

PowerShellProbe( midserver, psServer )
The Class Constructor

  • midserver (optional) – The name of the MID Server that will process the command
  • psServer (optional) – The IP Address of the computer that the Powershell Script will execute against. The localhost ip address (127.0.0.1) indicates that the Powershell script will run on the same machine as the MID Server. If you have Runbook Automation enabled on your instance, you can execute the powershell script on a remote computer. The remote computer must be a Windows devices in the domain that trusts the Windows box hosting the MID Server. Runbook Automation allows you to store Windows Credentials that can be used to authenticate against the remote machine to execute the desired script.

setPSServer( psServer )
Sets the PowerShell device IP Address. This is typically used if you did not specify the address in the constructor when you instantiated the class.

  • psServer – The IP Address to the windows device that will execute the Powershell Script

setMIDServer( midserver )
Sets the name of the MID Server that will process the Powershell Request. This method is typically used if you do not specify the MID Server in the constructor when you instantiated the class.

  • midserver – Name of the MID Server (eg. WinMid1)

setScript( script )
Stores the PowerShell script that will be executed remotely

  • script – The script string

setMaxWait( seconds )
Set the maximum number of seconds that you will wait for a response from the MID Server on this request before you move on. If you don’t call this method, the default max wait time is 60 seconds.

  • seconds – Maximum number of seconds to wait for a response from the MID Server on this request (if you choose to wait at all).

execute( waitForResponse )
Sends the request to the ECC Queue for processing. If you choose to wait for the reponse, the method will return an object that contains an “output” variable and and “error” variable. The output variable will contain the text from the Stdout buffer. The error variable will contain the text from the StdErr buffer.

  • waitForResponse (boolean) – Optional. The value of true will hold the script until the MID Server has responded, or until the maxWait period has expired.

Code Example

Here is a snippet of code that we can use to test our setup:

1
2
3
4
5
6
7
8
9
10
var pb = new PowershellProbe("EC2WINMID1", "127.0.0.1");

var script = "$myVarName = 'John Andersen'\n" +
             "Write-Host $myVarName\n";

pb.setScript(script);

var response = pb.execute(true);
gs.log("OUTPUT: " + response.output);
gs.log("ERROR: " + response.error);

When executed, we get the following response:

*** Script: OUTPUT: John Andersen
*** Script: ERROR: null

The Powershell Utility

Sometimes we need to test out our PowerShell scripts before we actually put them into code. Additionally, it is often common that the person developing the ServiceNow scripts does not have easy access to the Windows device that will be executing the commands. This update set provides a GUI Utility that allows you to experiment with Powershell scripts against the target machine from within your ServiceNow instance.

To get to the utility, simply browse to: “MID Server->PowerShell Utility”.

Once you get there, you will be present with the GUI PowerShell utility. Enter in a valid, running, MID Server. Then type in the remote Windows device IP Address that will execute your PowerShell script. The IP Address of 127.0.0.1 tells the MID Server to execute the script on the same box that is hosting the MID Server.

Once you type in your script, click the “Submit” button. The utility will wait for the response to complete and then it will pop up a dialog box with the result and any errors.

The default Max Wait time for the utility remains at the default time for the PowershellProbe library. However, you can change the default max wait time by setting the following System Property:

com.snc.integration.powershellprobe.maxWaitTime

Provide an integer value that represents the maximum number of seconds that the probe will wait for a response from the MID Server.

Download the Update Set

This file is not supported nor authored by Service-now.com. Please
use at your own risk.