Jump to content

Scheduled Task already Exists


texan
 Share

Recommended Posts

I am using Run("schtasks /create /tn TaskName /s \\remotepc .... other schtasks flags", "", "@SW_HIDE") to add a scheduled task on a remote PC. This works fine when the TaskName does not currently exist, but, when the TaskName does exist it fails (as expected). If I manually try running the SchTasks command from a DOS window I get an error stating "The specified task name already exists in the system". I was expecting the RUN command to give me an error in this situation since the task failed but it returns a number instead.

Is there a way to get the RUN command to return an error in this situation or maybe you know a better way of going about this?

Link to comment
Share on other sites

Tested using RunWait. RunWait returns 0 and @error is 0 when the command is successful. RunWait returns 1 and @error is 0 when the command is not successful. The help file says RunWait should return 0 when there is a failure but I am getting the opposite. Documentation wrong by chance?

Link to comment
Share on other sites

  • Moderators

Success: Returns the exit code of the program that was run.

RunWait succeeded both times, your exit code was returned from the program itself.

You have to interpret it, eg... 0 = success, 1 = Failure in your specific case.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

#include <Constants.au3>
$foo = Run("SCHTASKS /Create /SC HOURLY /MO 5 /TN CalcTest /TR calc.exe /ST 12:00:00", "", @SW_HIDE, $STDOUT_CHILD)


Local $line
While 1
    $line &= StdoutRead($foo)
    If @error Then ExitLoop
Wend

MsgBox(0, "STDOUT read:", $line)

Link to comment
Share on other sites

actually, you could always query it first to see if the task exists.

#include <Constants.au3> $foo = Run("SCHTASKS /Query", "", @SW_HIDE, $STDOUT_CHILD) Local $line While 1 $line &= StdoutRead($foo) If @error Then ExitLoop Wend MsgBox(0, "STDOUT read:", $line) ;all output If StringInStr($line,"CalcTest") then Msgbox(0,"","CalcTest already exists") ; Bit crude but works in the basic form

You'd need to parse through the $line value to see if your task already existed.

Edit:

Even better if the task doesn't exist then the $line is blank....

#include <Constants.au3>

$foo = Run("SCHTASKS /Query /TN CalcTest", "", @SW_HIDE, $STDOUT_CHILD)

Local $line
While 1
    $line &= StdoutRead($foo)
    If @error Then ExitLoop
Wend

MsgBox(0, "STDOUT read:", $line)

CREATE, QUERY and DELETE example just to show the output

#include <Constants.au3>

$foo = Run("SCHTASKS /Create /SC HOURLY /MO 5 /TN CalcTest /TR calc.exe /ST 12:00:00", "", @SW_HIDE, $STDOUT_CHILD)

Local $line = ""
While 1
    $line &= StdoutRead($foo)
    If @error Then ExitLoop
Wend

MsgBox(0, "STDOUT read:", $line)



$foo = Run("SCHTASKS /QUERY /TN CalcTest", "", @SW_HIDE, $STDOUT_CHILD)

Local $line = ""
While 1
    $line &= StdoutRead($foo)
    If @error Then ExitLoop
Wend

MsgBox(0, "STDOUT read:", $line)



$foo = Run("SCHTASKS /DELETE /TN CalcTest /F", "", @SW_HIDE, $STDOUT_CHILD)

Local $line = ""
While 1
    $line &= StdoutRead($foo)
    If @error Then ExitLoop
Wend

MsgBox(0, "STDOUT read:", $line)
Edited by ChrisL
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...