PowershellProbe - Run PowerShell scripts programmatically (ServiceNow)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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' | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you don't care about the mid-server that's running the script (or only have 1), you can just populate it automatically.