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

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

Related

Scheduled powershell script run by system does not run in foreground

I am trying to schedule a script to run when I'm not present at my computer. The script has to run with administrator rights AND run in the foreground so that I can see any message or window that is created when I get back to the computer.
The problem is that scheduled system scripts run in the background, this resulting in no visible window or console to show me any results.
here is the command i use to create my scheduled task (using powershell):
SchTasks /Create /SC ONIDLE /I 1 /TN MyScheduledTask /TR "Powershell scheduler.ps1" /RU "System"
All I need is some way to make some (or all if that's easier) of the commands in the script to become visible in the foreground to the user.
Thanks for any feedback.
When running scheduled tasks under the system account, they will run in the background.
You are running the task under System, /RU "System" use /RU with your username and /RP for your user password.
Please also take a look at this question: Powershell Task Scheduler Stuck Running

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

Self elevating cmd or powershell script?

I have a script which I have to run frequently, but the exact moment is user-driven so I cannot use a scheduled task etc.
This script has to run with Admin-privileges (my account already has admin rights) so I cannot simply launch the script.
Is there anye way to "self-elevate" this script?
very simple example:
ECHO "I would like to have admin rights to recycle the app-pool"
pause
There is no way of self-elevation. But you are quite close to the solution. Create a task with the scheduler and then trigger it with another bat file like this:
SCHTASKS /U "user name" /P "password" /TN "Yourtaskname"
Or you can give a normal user the rights to run a task and skip /U "user name" /P "password".

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