Why "schtasks" does not run my job? - windows-xp

I scheduled a task on Windows-XP using schtasks utility, but the task does not run. Here is what I see in the SchedLgU.Txt log file:
"MySQL Automatic Backup.job" (WampServer) 10/2/2010 6:36:43 PM ** ERROR **
Unable to start task.
The specific error is:
0x800700c1: (Unable to find an error message)
Try using the Task page Browse button to locate the application.
I found out that the reason for this error is spaces in the path to my script. The command that I used to set up the task look like:
schtasks /create /tn "MySQL Automatic Backup"
/tr "d:\path with spaces to my script\my script.bat" /sc daily ...
If I replace the spaces with underscores, for example, the problem disappears.
How could I solve this problem ?
And another question: What does the Start In column means in the output for schtasks /query /v ?
Thanks !

I found the answer:
Spaces in file paths can be used by using two sets of quotes,
one set for CMD.EXE and one for SchTasks.exe.
The outer quotes for CMD need to be double quotes;
the inner quotes can be single quotes or escaped double quotes.
i.e. it should be like this:
schtasks /create /tn "MySQL Automatic Backup"
/tr "\"d:\path with spaces to my script\my script.bat\"" /sc daily ...

Related

Schtasks command line folderpath with spaces

I want to copy and replace a file from one folder to another folder via the command line using copy and schtasks. I'm having trouble with the spaces in the folderpaths. For some reason I get an: ERROR: Invalid argument/option. The spaces are between "test 1" and "test 2"
This is my line:
schtasks /create /tn CopyFile /tr "copy "C:\Users\pc\desktop\test 1\test2022.xlsx" "C:\Users\pc\desktop\test 2\test2022.xlsx"" /sc MINUTE /mo 1
I also tried this with xcopy but got the same result:
"xcopy /y"
What am I doing wrong? Thank you in advance!

generating batch file to back up enabled/disabled state of Scheduled Tasks

From a batch file I am trying to:
Capture the task name and enabled/disabled state of all scheduled tasks in Windows 10 x64
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:
Executes the command SCHTASKS /Query /FO LIST /v
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
Inserts these values to 'schtasks /Change' commands for all tasks and export to the target batch file.
Any help will be much appreciated.
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.
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:
Reference his variables to populate the command 'schtasks /Change /TN "\TASKNAME" /SCHEDULED_TASK_STATE,' and
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?

Obtaining exit code from schtasks

I'm trying to start a scheduled task from a Powershell script, like this:
schtasks /Run /TN "Test" > nul
I'm testing as I go, so I've set up the task to call a batch file that intentionally does not exist.
Once this happens, I see on the next line of code that the error level was 0, despite seeing 0x1 in the Task Scheduler's Last Run Result column.
In the console, I see
SUCCESS: Attempted to run the scheduled task "Test"
This looks to me as though it's considering the launch successful, and not waiting around to capture the error reported to Task Scheduler. (0x1)
How can I detect this error in Powershell?
I seem to remember that schtasks.exe just instructs the the task scheduler to go run a job and immediately exists without waiting around. Hence it always returning 0 unless the task itself can't be found (exit code: 1) or something catastrophic happens (exit code: no idea).
If you want to get the "Last Run Result" you'll need to query the task, for example:
schtasks.exe /v /query /tn "A Real Task" /fo list
This will return the task details including its run status as a list of fields and values which will include the "Last Run Result".
To see more formatting options run:
schtasks.exe /query /?
So you could capture data about a task (in this case CSV formatted) by doing:
$taskinfo = schtasks.exe /query /tn "A Real Task" /FO CSV /V
...then process it in some way. Obviously you'd need to check the task's run status to make sure it's completed before reading the "Last Run Result".

2 url's in powershell with cmd

I have create the following schedule task but I would like to add a second url which will run after the end of the first. Could anyone help με how can I do it??
Thanks in advance.
schtasks /create /tn "My Task Title" /tr "powershell -ExecutionPolicy unrestricted -Command \"(New-Object Net.WebClient).DownloadString(\\\"Url1\\\")\"" /sc DAILY /ru username /rp pass
If you have multiple URLs it would be best to move some of the logic into a PowerShell script file to push some of the logic away from the command line. Not that you could put this all in the command line but for ease and readability using the -File parameter of powershell would be a better way to go. First you would need to create a file called "Get-WebStrings.ps1" with the following contents.
# Check to be sure we have at least one argument.
If($args.Count -gt 0){
# Treat each argument as a URL that we need to download.
ForEach($singleURL in $args){
# Download the string
(New-Object Net.WebClient).DownloadString($singleURL)
# Optional depending on your needs
# (New-Object Net.WebClient).DownloadString($singleURL) | Out-Null
}
}
What this script will do it take the arguments sent to it as treat each one as a url and download the data. By default this would output to console. If merely performing the download is all you wish then you could pipe the output into Out-Null (see commented code above).
Next you would need to create the task in the command line much like you have already done. Note that this file needs to be accessible on the local system where the task is being called! Each URL is placed inside single quotes in the string.
schtasks /create /tn "My Task Title" /tr "powershell -ExecutionPolicy unrestricted -File 'C:\Temp\Get-WebStrings.ps1' 'https://www.google.com' 'http://www.purple.com'" /sc daily /ru username /rp pass
If you were to look at the Action inside Task Scheduler it would look like this after that was run.
-ExecutionPolicy unrestricted -File "C:\Temp\Get-WebStrings.ps1" "https:\\www.google.com" "https:\\employee.firstair.ca"

How to execute a scheduled task with "schtasks" without opening a new command line window?

I have a batch file that creates a scheduled task using schtasks like this:
schtasks /create /tn my_task_name
/tr "...\my_path\my_task.bat"
/sc daily
/st 10:00:00
/s \\my_computer_name
/u my_username
/p my_password
It works OK except the fact that when my_task.bat is executed - a new command line window is opened (and closed after execution).
I would like to avoid opening this new window (i.e. to run the task in quiet mode, in the background).
I thought to use
start /b ...\my_path\my_task.bat
but I don't know how, because since I have to call start from the batch file I need to precede it with cmd /c, which again causes the new window to open.
How could I solve this problem ?
You can do this by specifying /RU option for schtasks. This option
specifies the user account (user
context) under which the task runs.
For the system account, valid values
are "", "NT AUTHORITY\SYSTEM" or
"SYSTEM".
And thus, try this
schtasks /create /tn my_task_name
....
/st 10:00:00
/ru "SYSTEM"
....
You can use the Windows Shell Scripting extensions to execute a batch file in invisible mode.
Create a plain text file and name it <scriptname>.vbs
Paste the following code in the .vbs file
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\Batch Files\syncfiles.bat" & Chr(34), 0
Set WshShell = Nothing
Change the name and path of you batch file according to your need
Save the .vbs file and schedule the same in schtasks