Error sending PowerShell directly to ECC queue

chadlockwood
Kilo Sage

I am creating a UI Action for our administrators that will allow remote restart of MID server services that go down. The UI Action will only appear on the MID server form if it is down. It will identify a MID server that is running to use and will send a PowerShell command directly to the ECC queue using this code:

function start_mid_service(){

  var start_host = get_stopped_hostname();

  var payload = '<parameters><parameter name="skip_sensor" value="true"/><parameter name="script.ps1" value="get-service -name sn_mid_server -computername ' + start_host + ' | set-service -status running"/></parameters>';

  // PowerShell directly!

  var ecc = new GlideRecord("ecc_queue");

  ecc.initialize();

  ecc.agent = "mid.server." + get_running_mid(get_stopped_host_domain());

  ecc.topic = "Powershell";

  ecc.name = "Windows - PowerShell";

  ecc.queue = "output";

  ecc.state = "ready";

  ecc.source = "127.0.0.1";

  ecc.payload = payload;

  ecc.insert();

  action.setRedirectURL(current);

}

function get_stopped_hostname(){

  return g_form.getValue('host_name');

}

function get_stopped_host_domain(){

  return g_form.getValue('win_domain');

}

function get_running_mid(domain){

  var gr = new GlideRecord('ecc_agent');

  gr.addQuery('status', 'Up');

  gr.addQuery('win_domain', domain);

  gr.query();

  if(gr.next()){

  return gr.name;

  }

}

If I include <parameter name="skip_sensor" value="true" /> I receive the error string com.service_now.mid.message_handlers.ProbeHandlingException: Error parsing XML parameters and the PowerShell does not run as expected. I have tried including <?xml version="1.0" encoding="UTF-8"?> at the beginning of the payload but still receive the ProbeHandlingException error.

However, if I do not include <parameter name="skip_sensor" value="true" /> I receive the error No sensors defined but the PowerShell code works as expected and restarts the MID server service on the remote machine. I would prefer to not see the error record in the ECC queue if it is actually successful. Is there another way to get this to skip the sensor?

Regards,

Chad

17 REPLIES 17

brian_quinn
ServiceNow Employee
ServiceNow Employee

Chad,



Have you tried moving the skip_sensor parameter after the script.ps1 parameter?   How about creating a custom javascript sensor that doesn't do anything, or just does a gs.log() to log that the restart was completed?



Thanks


Brian


killswitch1111
ServiceNow Employee
ServiceNow Employee

Chad,



It looks like your payload may be malformed.  



My suggestion would be to use the probe API, which will create all of that for you.



var probe = SncProbe.get("Windows - Powershell");


              probe.setName("Windows - Powershell");


              probe.setSource("127.0.0.1");


              probe.addParameter("script.ps1","get-service -name sn_mid_server -computername ' + start_host + ' | set-service -status running");


probe.addParameter("skip_sensor", true);


              probe.create("MID SERVER FULL SERVICE NOW NAME");



This should get the probe from discovery_probes, set the script you define and send it to the mid server.


Hi Chris,



What is the relevance of Source here.Does 127.0.0.1 means I am running the command on MID Server only?



What if I need to run the commend on other server with parameter?


Dubey,



So the source does mean where this powershell code is going to run.   If you put a different computer other than the local host, you (potentially) will get authentication errors or authorization errors trying to run remote powershell.   So if you wanted to run the script on a remote box, you could set the source to that, then setup the credentials for that box in the ServiceNow credentials table.   This would allow it to do a remote powershell session on the Source box that you want.  



Thanks,


Chris