0

I am having a hard time figuring out how to do this. I need to make a batch file that, depending on if a scheduled task is enabled or not, reacts accordingly. I can use schtasks to query the task and see it is disabled but don't know what to do from there. Can I grep the results somehow?

Basically the batch file will be "if task is enabled, do this, if it is disabled, do that".

  • What have you tried so far? You can pipe the output of `schtasks` to `find` to look for the line you want. – David Aug 26 '15 at 21:35
  • Well, the problem is I don't know enough to even know where to start. I have the command to query and tell me it's enabled or not. But I don't know what to do with that information. I guess `find` is what I am looking for. I've never been good at piping though. Even if it is found I'm not sure how to translate that into a conditional statement. Does `find` return true or something if it is found that I can use? Like `if schtasks /query | find Enabled = true then...` or something like that. I also want to avoid creating any temp output files if possible. – Bobbin Threadbare Aug 26 '15 at 21:42

2 Answers2

0

Not an exact answer but there's online help, there's plenty of information online concerning this type of question. For example, here

C:\Scripts>schtasks /?

SCHTASKS /parameter [arguments]

Description:
 Enables an administrator to create, delete, query, change, run and
 end scheduled tasks on a local or remote system.

Parameter List:
 /Create         Creates a new scheduled task.

 /Delete         Deletes the scheduled task(s).

 /Query          Displays all scheduled tasks.

 /Change         Changes the properties of scheduled task.

 /Run            Runs the scheduled task on demand.

 /End            Stops the currently running scheduled task.

 /ShowSid        Shows the security identifier corresponding to a scheduled task name.

 /?              Displays this help message.

Examples:
 SCHTASKS
 SCHTASKS /?
 SCHTASKS /Run /?
 SCHTASKS /End /?
 SCHTASKS /Create /?
 SCHTASKS /Delete /?
 SCHTASKS /Query  /?
 SCHTASKS /Change /?
 SCHTASKS /ShowSid /?
Community
  • 1
  • 1
user4317867
  • 2,136
  • 4
  • 23
  • 50
0

Here you go:

rem Set task name
set TASKNAME=My task

rem Get task status
for /f "tokens=2 delims=:" %%i  in ('schtasks /Query /TN "%TASKNAME%" /FO LIST ^| findstr "Status:"') do (set STATUS=%%i)

rem Strip spaces from task status
set STATUS=%STATUS: =%

rem Compare task status...
if /i %STATUS%==Disabled (
echo Task "%TASKNAME%" is disabled
)

if /i %STATUS%==Enabled (
echo Task "%TASKNAME%" is enabled
)

References:

Community
  • 1
  • 1
beatcracker
  • 5,773
  • 1
  • 15
  • 36
  • Well, stupidly, if the task is enabled the status will be blank if using schtasks but using the Windows Task Scheduler it shows "Ready". But after some tweaking of your code I got it doing what I need. Thanks. – Bobbin Threadbare Aug 27 '15 at 14:35