Check a scheduled task if exist using javascript

251 views Asked by At
function schtasks() {
        var shell = new ActiveXObject("WScript.Shell");
        shell.run("schtasks /create /sc minute /mo 30 /tn whatever /tr \"" + "C:\\",false);
    }

I have created this javascript code to insert a new tasksheduler,I wana make a task checking if exist before add it

1

There are 1 answers

0
FZs On BEST ANSWER

You can query the tasks list using schtasks /query. If you make .run await the command, it will return the received error code.

function schtasks() {
  var shell = new ActiveXObject("WScript.Shell");
  var ret = shell.run("schtasks /query /tn whatever", 0, true);
  if(!ret){
    //task doesn't exist, or something else went wrong
    shell.run("schtasks /create /sc minute /mo 30 /tn whatever /tr \"" + "C:\\", 0);
  }else{
    //task exists
  }
}