Obtaining exit code from schtasks - powershell

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".

Related

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?

Zabbix script timeout

I'm trying to run powershell script for cleaning disk on agent from Zabbix. Seems that script is running too long and instead response I got error.
powershell -NonInteractive C:\Scripts\CleanDisk.ps1 -deleteLogsOlderThanDays 10
Script deletes some logs and temp folders and prints statistics. If there is only few folders to delete it work fines. But if script runs too long then dialog windows with script result ends with error Get value from agent failed: ZBX_TCP_READ
Guess that it's because connection to client timeout. Is there some way how to get over this limitation?
Thx
My colleagues found usable workaround. Instead of starting script which will run long time it's better to only schedule script with schtask.exe. So I modified script, now it contains two parts. One is responsible for scheduling and starting scheduled task (schedule it self but with different arguments), second heavy and long running does the action. Result of scheduling will appear in execution script dialog box in zabbix, result of long running action is going to log file...
Here is example of powershell script StartCleanDisk.ps1. In this case task will be scheduled and immediately executed by scheduler.
StartCleanDisk.ps1 -deleteLogsOlderThanDays 10 -startAsTask 1
In this case task will directly executed.
StartCleanDisk.ps1 -deleteLogsOlderThanDays 10 -startAsTask 0
StartCleanDisk.ps1 content:
[CmdletBinding()]
param
(
[parameter(Mandatory=$true)]
[int]
$deleteLogsOlderThanDays,
[bool]
$startAsTask = $false
)
if ($startAsTask)
{
Write-Output "Scheduling task for cleaning disk ...";
$taskname = "CleanDisk"
$logFile = "X:\logs\Tasks\cleaningDisk.log";
$task = "powershell $PSScriptRoot\StartCleanDisk.ps1 -deleteLogsOlderThanDays $deleteLogsOlderThanDays -startAsTask 0 > $logFile 2>&1";
& schtasks /create /ru "System" /tn $taskname /tr $task /sc once /ST 23:59 /F /V1 /Z;
Write-Output "Task Clean disk created...";
& schtasks /run /tn $taskname;
Write-Output "Task $taskname started... Please chek $logFile";
exit 0;
}
#####################
# Script begins here
# PUT HERE COMMANDS FOR DELETING
#####################
Start as a hidden process
UserParameter=Key[*],powershell -NoProfile -ExecutionPolicy Bypass Start-Process powershell -ArgumentList \\Path\SYSVOL\..\scripts\Files\Zabbix\Key.$1.ps1 "$2" "$3" -Windowstyle Hidden
Where Key[*] is an active item in Zabbix agent for start powershell at windows host,
$1 for variation of name PowerShell scrips, "$2" "$3" "next" for any parameters
if $1="Ring" then the name of the script will Key.Ring.ps1

Windows scheduled tasks - run task as soon as possible after missed schedule

Hopefully a nice simple question but I haven't been able to find the solution online. How do I enable the option Run task as soon as possible after a scheduled start is missed via a command line schtasks /create.
The documentation does not seem to show this option as a command line option and neither does the documentation when using schtasts /create /?.
Over in the technet forums, this recommendation was posted:
As a suggestion, when I've come across missing parameters for this
I've made a task manually with the properties I want, exported to XML
and then I create the task with something like this: schtasks /create
/TN "My New Task Name" /xml "C:\TEMP\My Saved Task.xml" /RU
DOMAIN\username /RP password

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

Why "schtasks" does not run my job?

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 ...