Skip to content

Instantly share code, notes, and snippets.

@jessems
Last active October 30, 2023 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessems/5a5f78209c3cd24f93dbf24906f521ea to your computer and use it in GitHub Desktop.
Save jessems/5a5f78209c3cd24f93dbf24906f521ea to your computer and use it in GitHub Desktop.
PowershellProbe - Run PowerShell scripts programmatically (ServiceNow)
// Original: https://john-james-andersen.com/blog/service-now/powershell-probe-and-utility-for-servicenow.html
var PowershellProbe = Class.create();
PowershellProbe.prototype = {
initialize: function (mid, server) {
this.setMidServer(mid);
this.setPSServer(server);
},
_createEccOutputRecord: function () {
var ecc = new GlideRecord('ecc_queue');
ecc.initialize();
ecc.agent = this.mid;
ecc.topic = 'Powershell';
ecc.name = 'Windows - Powershell';
ecc.source = this.psServer;
ecc.queue = 'output';
ecc.state = 'ready';
ecc.payload = this._getPayloadString();
return ecc.insert();
},
_getPayloadString: function () {
var xmldoc = new XMLDocument('<parameters/>');
var el = xmldoc.createElement('parameter');
xmldoc.setCurrent(el);
xmldoc.setAttribute('name', 'skip_sensor');
xmldoc.setAttribute('value', 'true');
xmldoc.setCurrent(xmldoc.getDocumentElement());
el = xmldoc.createElement('parameter');
xmldoc.setCurrent(el);
xmldoc.setAttribute('name', 'probe_name');
xmldoc.setAttribute('value', 'Windows - Powershell');
xmldoc.setCurrent(xmldoc.getDocumentElement());
el = xmldoc.createElement('parameter');
xmldoc.setCurrent(el);
xmldoc.setAttribute('name', 'script.ps1');
xmldoc.setAttribute(
'value',
this.prettyScript ? this.prettyScript : this.scriptFileInvocation
);
return xmldoc.toString();
},
_getPayload: function (ecc) {
if (ecc.payload != '<see_attachment/>') {
return ecc.payload;
}
var sa = new Packages.com.glide.ui.SysAttachment();
var payload = sa.get(ecc, 'payload');
return payload;
},
_getResponse: function (eccQueueRecordId) {
var maxtime = gs.getProperty(
'com.snc.integration.powershellprobe.maxWaitTimeForEccQueue',
60
);
var counter = 0;
var found = false;
var eccResponse = new GlideRecord('ecc_queue');
eccResponse.addQuery('queue', 'input');
eccResponse.addQuery('response_to', eccQueueRecordId);
while (counter < maxtime && found == false) {
eccResponse.query();
if (eccResponse.next()) {
found = true;
}
gs.sleep(1000);
counter++;
}
var payload = this._getPayload(eccResponse);
var xmldoc = new XMLDocument(payload);
var retObj = new Object();
retObj.output = '';
retObj.error = '';
if (found) {
retObj.output = '' + this._getResponseOutputValue(xmldoc);
retObj.error = '' + this._getResponseErrorValue(xmldoc);
return retObj;
}
},
_getResponseErrorValue: function (xmldoc) {
var error = xmldoc.getNodeText('//results/result/error');
return error;
},
_getResponseOutputValue: function (xmldoc) {
var output = xmldoc.getNodeText('//results/result/output');
return output;
},
execute: function (waitForResponse) {
var recSysId = this._createEccOutputRecord();
if (waitForResponse == true) {
var response = this._getResponse(recSysId);
return response;
}
},
setMidServer: function (mid) {
this.mid = 'mid.server.' + mid;
},
setPSServer: function (server) {
this.psServer = server;
},
setScript: function (script) {
this.prettyScript = script;
this.encodedScript = GlideStringUtil.escapeHTML(script);
},
setScriptByFilePath: function (filePath, params) {
this.scriptFileInvocation = 'powershell "scripts\\' + filePath + '"';
if (gs.nil(params)) return;
// Add parameters to the script invocation
for (var key in params) {
this.scriptFileInvocation += ' -' + key + ' ' + params[key].toString();
}
},
type: 'PowershellProbe'
};
@jnovack
Copy link

jnovack commented Oct 30, 2023

If you don't care about the mid-server that's running the script (or only have 1), you can just populate it automatically.

var MIDSERVER = "";

var ecc_gr = new GlideRecord('ecc_agent');
ecc_gr.addQuery('status', 'Up');
ecc_gr.query();
if (ecc_gr.next()) {
    MIDSERVER = ecc_gr.getValue('name');
}
// don't forget to else/fail here.

var pb = new global.PowershellProbe(MIDSERVER, MIDSERVER)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment