0

From a batch file I am trying to:

  1. Capture the task name and enabled/disabled state of all scheduled tasks in Windows 10 x64

  2. Export these to a second batch file with TASKNAME and SCHEDULED_TASK_STATE populated in the following command for each task:

    schtasks /Change /TN "\TASKNAME" /SCHEDULED_TASK_STATE

The idea is for this second batch file to serve as a one-click backup/restore of the enabled/disabled state of all existing tasks. This way, the cumbersome process of selectively enabling/disabling tasks one-by-one via a GUI tool (e.g., Task Scheduler, Autoruns, taskschedulerview-x64, etc.) can be easily undone (or redone).

For the source batch file, the command

SCHTASKS /Query /FO LIST /v

... will retrieve a list of all tasks and the two values I am interested in -- 'TaskName' and 'Scheduled Task State.'

With info gleaned from other stackoverflow-ers, below is as far as I have gotten using a temp files approach, but it's only half done and I'm in way over my head :)

    echo

:: Get list and parameters of all tasks; find values for TaskName; save values to temp file 1

    SCHTASKS /Query /FO LIST /v| findstr /r /C:"TaskName: " >"%USERPROFILE%\Desktop\1-SCHTASKS_LIST_QUERY_NAMES.TXT"

:: Re-get list and parameters of all tasks; find values for Scheduled Task State; save values to temp file 2

    SCHTASKS /Query /FO LIST /v| findstr /r /C:"Scheduled Task State: " >"%USERPROFILE%\Desktop\2-SCHTASKS_LIST_QUERY_STATES.TXT"

:: Set delimiter/assign variable to TaskName values in temp file 1; populate schtasks/change command with value; (missing steps to get/populate task state); save to target batch file

    for /f "tokens=2 delims=: " %%n in (%USERPROFILE%\Desktop\1-SCHTASKS_LIST_QUERY_NAMES.TXT) do @echo schtasks /Change /TN "%%n">>"%USERPROFILE%\Desktop\BACKUPS\BACKEDUP_scheduled_tasks_state.bat"

    pause

Rather than the above, I would prefer if the source batch file:

  1. Executes the command SCHTASKS /Query /FO LIST /v

  2. Does all processing/parsing in-place, i.e., assign variables to the two values I need for each task - 'TaskName' and 'Scheduled Task State' -- without creating temp files, and

  3. Inserts these values to 'schtasks /Change' commands for all tasks and export to the target batch file.

Any help will be much appreciated.

pazdel
  • 9
  • 1
  • 4

2 Answers2

0

As you need only taskname and status, you don't need /v (verbose)
schtasks will then output taskname, next run time and status.
Get a format of csv instead of list, so each task is on only one line (much easier to parse).
Use find to exclude the header lines.
use a for loop around to get the desired tokens (1=taskname, 3=status) and set them into variables.

@echo off
setlocal enabledelayedexpansion
set n=0
for /f "tokens=1,3 delims=," %%a in ('schtasks /query /fo csv^|find "\"') do (
  set /a n+=1
  set name[!n!]=%%~na
  set state[!n!]=%%~b
)
echo ----- %n% tasks found -----
rem set name
rem set state
set /a num=%random% %% %n% +1
echo example: !name[%num%]! has status of !state[%num%]!

delayed expansion is used, because variables (n) are changed and used within a code block.

Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Thank you Stephan for your efforts and explanation. I will respond in an answer to provide more details. – pazdel May 06 '17 at 04:33
0

In regards to @Stephan's answer below....

COMMENTS

The task 'Status' value (as used by Stephan) is a little different than 'Scheduled Task State.' In order to export the command 'schtasks /Change /TN' in the backup batch file I'm trying to create, I need the 'Scheduled Task State' at the end of the line.

The task state is only available with verbose results; as such I've changed line 4 of Stephan's code to be:

:: Added /v (verbose); changed tokens to "2" (for task name in CSV) and "12" (for task state in CSV)

for /f "tokens=2,12 delims=," %%a in ('schtasks /query /fo csv /v^|find "\"') do (

PENDING

I am still unsure of how to append Stephan's modified code in order to:

  1. Reference his variables to populate the command 'schtasks /Change /TN "\TASKNAME" /SCHEDULED_TASK_STATE,' and

  2. Export a batch file with this command for every scheduled task in the current system (as a backup/restore file). Example of how this exported batch file would look:

schtasks /Change /TN "GoogleUpdateTaskMachineUA" /DISABLE

schtasks /Change /TN "GoogleUpdateTaskMachineCore" /DISABLE

schtasks /Change /TN "DropboxUpdateTaskMachineCore" /DISABLE

schtasks /Change /TN "DropboxUpdateTaskMachineUA" /DISABLE  

schtasks /Change /TN "\Microsoft\Windows\Application Experience\ProgramDataUpdater" /DISABLE

...but including all tasks on the current system.

***** UPDATE *****

I've realized another issue: the command to get task states uses the language "Enabled" or "Disabled" (with "d" at the end); but the command to set a task's state needs the language "ENABLE" or "DISABLE" (neither with "d" at the end). So, the plot thickens: how to also delete the unnecessary end-of-value "d" when outputting to the target batch file?

pazdel
  • 9
  • 1
  • 4
  • gets even worse: 1) `schtasks` uses local language. On my system it's "Aktiviert" and "Deaktiviert". 2) Some task names contain commas and quotes, so `tokens=12` isn't reliable!. You should add the `powershell` tag to your question. My own PowerShell knowledge is low, but I'm rather sure, it can handle this much better. – Stephan May 08 '17 at 13:30
  • Thanks again for trying Stephan. I will look into using Powershell or maybe using the temp file approach I was trying to avoid. The process would be: 1) Query TaskName and save all to Test A; 2) Query task state and save to File B; 3) In Text File B, find and replace "enabled" with "enable," and find and replace "disabled" with "disable;" 4) Concatenate the values in File A and File B into the command "schtasks /Change /TN "FILE-A_VALUE" /FILE-B_VALUE" and save to File-C.bat. Then in file I'd have exactly what I want in File-C. – pazdel May 09 '17 at 03:16
  • 1/2: keep in mind, both queries won't run at the same time. If a task gets added or deleted in that time slot (Task scheduler setting "If the task is not scheduled to run again, delete it"), the whole resulting list will be wrong. (not very likely, but bad practice) – Stephan May 09 '17 at 05:16