Trigger a windows task on a mid server

User485076
Tera Contributor

Hi,

Is it possible to trigger a windows task on a mid server from a script? Here is my script:

 

Unsure on the command and payload part. Any help would be greatly appreciated 

 


var midServerName = 'MID_SERVER_NAME';
var taskName = 'test task';

var midServer = new GlideRecord('ecc_agent');
midServer.addQuery('name', midServerName);
midServer.query();

if (midServer.next()) {
// Create a new ECC task record
var eccTask = new GlideRecord('ecc_queue');
eccTask.agent = midServer.sys_id; // Assign the MID Server's sys_id to the agent field
eccTask.queue = 'output';
eccTask.topic = 'Command';
eccTask.name = 'Trigger Scheduled Task';
eccTask.state = 'ready';

// Set the command payload to trigger the scheduled task
var command = 'cmd.exe /c schtasks /run /TN "' + taskName + '"';
eccTask.payload = command;

 

// Insert the ECC task record
eccTask.insert();

gs.info('ECC task created to trigger the scheduled task: ' + taskName);
} else {
gs.error('MID Server ' + midServerName + ' not found!');
}

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@User485076 

try this link which has an example

How to run a Batch File on a MID Server, using a MID Server Script File and 'Command' Topic ECC Queu... 

as per this update your script

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar 

Thank you - that helped me. Managed to trigger the task on the mid server.

The task creates an output file (.txt file) - is there a simple way to bring the contents of this file back into servicenow? I need to display the value of it

@User485076 

Glad to know.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar 

Wondering if you could help further.

 

Basically when I trigger the scheduled task on the mid server, it kicks off a powershell script which outputs some data to a .txt file. In the input record in the ECC queue, I get the message 'Success, attempted to run scheduled job' along with some other payload. Is it possible to bring back the data from the powershell script direct to the ecc queue here without having to ouput to a file?