The CreatorCon Call for Content is officially open! Get started here.

How to pass parameters to a mid-server script files

Uma2
Tera Expert

Hi Team,

  I want to pass parameters into mid-server power shell script files.Can anyone help me how we can pass.

In below script i need to pass app_id value for the url.

find_real_file.png

Thanks,

Uma.

1 ACCEPTED SOLUTION

Mike Patel
Tera Sage

I think this is final. I tested it and works for me.

Scheduled Job

var ecc = new GlideRecord("ecc_queue");
ecc.initialize();
ecc.agent = "mid.server.XXXX";
ecc.topic = "Powershell";
ecc.name = "Windows - Powershell";
ecc.source = "10.XXXX";
ecc.queue = "output";
ecc.state = "ready";
ecc.payload = this._getPayloadString();
ecc.insert();

function _getPayloadString() {
    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", "MIDScriptFile");

    xmldoc.setAttribute("value", _getScriptFilePath('e517b060db36d010d97f50aadc96191a'));
	
	xmldoc.setCurrent(xmldoc.getDocumentElement());
    el = xmldoc.createElement("parameter");
    xmldoc.setCurrent(el);
    xmldoc.setAttribute("name", "powershell_param_appid");
    xmldoc.setAttribute("value", '10555');

    return xmldoc.toString();
}

function _getScriptFilePath(file_id) {
    var scriptPath = '';
    var gr = new GlideRecord('ecc_agent_script_file');
    if (gr.get(file_id)) {
        var path = '';
        while (!gs.nil(gr)) {
            path = gs.nil(path) ? gr.name : gr.name + '\\' + path;
            gr = gr.parent;
        }

        scriptPath = 'scripts\\' + path;
    }

    return scriptPath;
}
$requestVersion = "vcode_request_version_1"

$method = 'GET'
$id = "$appid"
$urlBase = "xxx.xxxx.com"

$urlPath = "/api/5.0/getappinfo.do?app_id=$id"
$url="https://xxx.xxx.com/api/5.0/getappinfo.do?app_id=$id"

View solution in original post

29 REPLIES 29

Hi Mike,

  <?xml version="1.0" encoding="UTF-8"?>
<parameters>
<parameter name="skip_sensor" value="true"/>
<parameter name="probe_name" value="Windows - Powershell"/>
<parameter name="MIDScriptFile" value="scripts\PowerShell\Test_Veracode.ps1"/>
<parameter name="appid" value="226551"/>
</parameters>

 

Sudhanshu Talw1
Tera Guru

Hi,

Following link will help:

https://hi.service-now.com/kb_view.do?sysparm_article=KB0547043

 

Thanks

Sudhanshu

Hitoshi Ozawa
Giga Sage
Giga Sage

I thought the standard way was to set "Topic" to "Command" and to set "Name" to "powershell <arguments>" or to set "Payload" to script location

e.g.

https://community.servicenow.com/community?id=community_question&sys_id=5f034fa1dbd8dbc01dcaf3231f96...

https://community.servicenow.com/community?id=community_article&sys_id=0cf18778dbb348d4f7fca851ca961...

Mike Patel
Tera Sage

I think this is final. I tested it and works for me.

Scheduled Job

var ecc = new GlideRecord("ecc_queue");
ecc.initialize();
ecc.agent = "mid.server.XXXX";
ecc.topic = "Powershell";
ecc.name = "Windows - Powershell";
ecc.source = "10.XXXX";
ecc.queue = "output";
ecc.state = "ready";
ecc.payload = this._getPayloadString();
ecc.insert();

function _getPayloadString() {
    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", "MIDScriptFile");

    xmldoc.setAttribute("value", _getScriptFilePath('e517b060db36d010d97f50aadc96191a'));
	
	xmldoc.setCurrent(xmldoc.getDocumentElement());
    el = xmldoc.createElement("parameter");
    xmldoc.setCurrent(el);
    xmldoc.setAttribute("name", "powershell_param_appid");
    xmldoc.setAttribute("value", '10555');

    return xmldoc.toString();
}

function _getScriptFilePath(file_id) {
    var scriptPath = '';
    var gr = new GlideRecord('ecc_agent_script_file');
    if (gr.get(file_id)) {
        var path = '';
        while (!gs.nil(gr)) {
            path = gs.nil(path) ? gr.name : gr.name + '\\' + path;
            gr = gr.parent;
        }

        scriptPath = 'scripts\\' + path;
    }

    return scriptPath;
}
$requestVersion = "vcode_request_version_1"

$method = 'GET'
$id = "$appid"
$urlBase = "xxx.xxxx.com"

$urlPath = "/api/5.0/getappinfo.do?app_id=$id"
$url="https://xxx.xxx.com/api/5.0/getappinfo.do?app_id=$id"

Hi Mike,

   Thank you so much for the support.

It's working.Thank you!!!

 

Thanks,

Uma.