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

Hi Chris,



Could you please help me to use getProbeResponse   function in business rule. I am not able to find any function or may be I am doing something wrong.



Also how can I differential the output, e.x. I can have multiple custom business rule which creates ecc record to execute the powershell and I need to take different action for each powershell output.



Regards,


N.Dubey


Can you please tell me the use of the line <parameter name="script.ps1" value=" "/></parameters>


Why and when do we use the script.ps1 parameter and where is it stored ?


Vineetha,


script.ps1 is essentially a placeholder for a PowerShell script file when you don't have a PowerShell script file. In my original post, I was attempting to send PowerShell code directly to the MID server through the ECC queue without using Orchestration or running a MID server script file.


An alternative might be to send a MID server script file like this:


<parameter name="MIDScriptFile" value="scripts\PowerShell\MyScripts\Query.ps1"/>


assuming that you had a PowerShell file called Query.ps1 saved as a MID server script file. You might also have to include additional parameters for arguments needing to be passed to the script, such as:


<parameter name="powershell_param_myfirstname" value="chad"/>


assuming that your PowerShell script was expecting an argument called "myfistname".


I hope this helps


Thank you so much for your explanation. I was struggling to understand this.



Kind Regards,


Vineetha


Hi,

 

Can you please tell me under what situation do we receive the following error when executing a powershell command via ecc queue on mid server:

 

Unable to find script(s) specified in parameters. Make sure the script(s) associated with probe are passed down properly.

 

I receive this error only when I create an entry manually in Ecc (by making a GlideRecord) and not when I use the above mentioned API.

Why is there this difference when in both cases the payload is the same ?

 

Appreciate your inputs.

 

Kind Regards,

Vineetha