Implementing PowerShell Step in ServiceNow


Among the variety of Tasks, which can be accomplished in a default ServiceNow-Instance there is one, which might be very useful for integration with existing Microsoft infrastructure, and specifically a possibility to start any applications in Customer's internal network through MID Server and get data from these application.

Although technical support of this feature is present, it is being activated only after purchase of Integration Hub Plugin. For some Use Cases it might be justified, as Integration Hub deploys many useful scripts and Flow Actions. But what if you already have some specific PowerShell script and just want to call it out of ServiceNow Flows without additional Plugins?

There is a solution for this case, and it is called ecc_queue. This table contains actions to be executed on MID Server side and it could be used to invoke PowerShell scripts as well. First of all, you have to install and configure MID Server. Please refer to ServiceNow documentation, how to achieve this.

To use PowerShell from within a Flow, save a script in some folder on MID Server and grant access to MID User to this folder. Third step would be to create a new Flow Action. In a simple case one Javascript step is needed. For this step  you could configure an input parameter, if you need one. In below example it is a string, named someParameter. Then use a script below to parameterize ecc_queue.

(function execute(inputs, outputs) {
    var ecc = new GlideRecord('ecc_queue');
    ecc.initialize();
    ecc.agent = 'mid.server.mymid';
    ecc.topic = 'Command';
    var value = 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -command "C:\\scripts\\my-script.ps1 '+
        '\\"' + inputs.someParameter + '\\" ' +
        '" ';
    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();
})(inputs, outputs);

 

Basically this script instructs MID Server to execute a PowerShell command. The output of this command will be saved in ecc_queue as well. You can read this output on the next step, as shown below:

    outputs.error = "timeout";
    var steps = 10;
    while (steps > 0) {
        gs.sleep(3000);
        var answer = new GlideRecord('ecc_queue');
        answer.addQuery("response_to", ecc.sys_id);
        answer.query();
        if (answer.next()) {
            var payload = answer.payload;
            if (payload) {
                var xmlDoc = new XMLDocument2();
                xmlDoc.parseXML(payload);
                var error = '' + xmlDoc.getNodeText("//results/result/stderr");
                if(!error) {
                    gs.log("Success", "Powershell");
                    outputs.error = "";
                }
                else {
                    gs.log("Error: " + error, "Powershell");
                    outputs.error = error;
                }
                break;
            }
            else {
                gs.log("No payload", "Powershell");
            }
        }
        steps--;
    }
 

This code snippet checks, whether PowerShell command has generated some error messages and passes these messages to Flow. If no output or error handling is necessary, it can be omitted.

So, this was it! Have fun with ServiceNow and PowerShell!

Bild von Uladzimir Astapchyk
Uladzimir Astapchyk Uladzimir Astapchyk ist seit 2016 bei HanseVision und seit 2000 als Softwareentwickler tätig und dabei zuständig für die Entwicklung von individuellen Lösungen und Integrationsprojekte in Microsoft-Umfeld. In diesem Bereich analysiert er Anforderungen, implementiert Komponenten zusammen mit seinen Kolleg:innen und den Kund:innen. Alle Artikel des Autors

Ähnliche Blog-Artikel

Mit unserem HanseVision Update sind Sie immer gut informiert über alle Themen rund um moderne Zusammenarbeit, kluge Köpfe, Lösungen und Tools, Referenzen und Aktionen.

Jetzt zum Newsletter anmelden
Updates & Aktionen
Versand alle 4-6 Wochen
Trends & aktuelle Entwicklungen