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