Change the start time of tasks by 1 hour using schtasks.exe - windows

Is it possible to move the start or run time of scheduled tasks in Windows server 2003 machine by 1 hour without hardcoding the time using /ST paramter of schtasks command? I also don't want to use Scheduled Tasks GUI because it is a lengthy process using it for many tasks.
For eg. if task A run at 8 AM and task B at 10 AM, I want to move them to 9 AM and 11 AM respectively. If it's not possible, please suggest better ways of doing it. Thanks in advance!

AFAIK schtasks does not provide a way to change the schedule relative to the existing schedule. You could query the current schedule from the task and then modify it accordingly:
setlocal EnableDelayedExpansion
for /f "tokens=5" %%t in (
'schtasks /query /tn foo /fo list ^| find /i "next run time:"'
) do (
if "%%t"="08:00:00" (
schtasks /change /tn foo /st 09:00
else if "%%t"="10:00:00" (
schtasks /change /tn foo /st 11:00
)
)

Can't be done with schtask.exe
You can't use the /st switch with the /change switch. You'd have to delete and create. But you would risk losing advanced options that you can't see with Schtask.exe. Basically you'll need to look at powershell instead to accomplish what you want.

Related

Remotely Starting a Batch File

SCHTASKS /create /S $strComputer /tn "David\Start Gecko" /tr
"'C:\eQube-Tools\Batch and Registry Files\gecko.bat'" /sc once /sd
01/01/2099 /st 01:00 /u Boss /p *password* /RU SYSTEM
pause
SCHTASKS /run /S $strComputer /tn "David\Start Gecko"
So, I have a problem with running the batch file this creates, it says it's running on the task scheduler on the remote pc, but nothing happens. But the odd thing is: if I locally create a "basic task" it runs fine both remotely and running it by hand at that pc.
My question is there any way to have it know you want to create a "basic task" I've looked at the switches but can't find anything for /create, I also tried the /xml but this is something I plan to create, use then destroy so simpler the better.
** the reason I'm using SCHtasks is because I had a lot of issues with redirect errors trying to run it directly using powershell.
Specs
All win 7 WORKGROUP
All have matching Admin Accounts with matching password
Firewall set to allow remote sch tasks
Batch file is on remote pc
the batch file I'm trying to run
#echo off
c:
cd \eQube-Tools\Batch and Registry Files
start virtualhelpscreen.exe
:top
c:
cd \gecko
timeout 12 /nobreak
gecko.exe
cls
goto :top
SCHTASKS /create /S $strComputer /xml "filesavedtomainpc" /tn "David\Start Gecko"
one issue left is it not forcing full screen like it does when you run the batch file but that might be down to the program, for now the question I asked is answered.

Create a task which will expire/disable after sixmonth from its start date

I want to create a windows task which will run a batch file every day at 12.00am for only six months from the day the task is created. I'm using following command to create a task. I'm not sure how to set expiration date so that the task will stop executing after six months.
My command is:
schtasks /create /sc Daily /mo 10 /tn run /tr "C:\Users\Rashmi\test.bat" /st 00.00
Any help is appreciated.
get the help of Powershell to easily calculate the enddate (it's generaly possible with pure batch, but it's a nightmare):
for /f "delims=" %%a in ('powershell "(get-date).AddMonths(+6).ToString('MM/dd/yyyy')"') do set "enddate=%%a"
schtasks /create /sc Daily /mo 10 /tn run /tr "C:\Users\Rashmi\test.bat" /st 00.00 /ED %enddate%
See here for a pure batch solution and a solution using VBS instead of Powershell (but needs a temporary file).
You want a /ed parameter and to establish the value dynamically using the %date% value
See https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/schtasks#BKMK_create

Schtaks - Command Prompt Parameters

I have a batch file I need to run ever x minutes. I know how to set up the scheduled task but I'm not sure if there is a parameter I can use to take advantage of the advanced setting "Stop the existing instance".
Basically, I want to be able to run the batch file every x minutes and when it runs again, if for some odd reason the first instance did not complete, I need it to be killed and the new instance to run.
Right now, I'm using:
schtasks /create /tn [TASKNAME] /tr C:\[DIR]\au.bat /sc MINUTE /mo 15 /ru "System"
which is great, but it lacks this extra part.
I already know the GUI is an option but the reason I am using the command prompt is that this is included in an installer clients will be using to make things easy for them and I don't want them to have to take this last step of going into the GUI to make this one change.
EDIT:
Ended up going with something like this:
REM Checks to see if any instances of 'MYTITLE'
FOR /F "tokens=* USEBACKQ" %%F IN (`tasklist /v /fo csv ^| findstr /i MYTITLE`) DO (
FOR /F "tokens=2 delims=," %%B IN (%%F) DO (
taskkill /pid %%B /f
)
)
title=MYTITLE
[BATCH FILE DOES STUFF HERE]
At launch, it looks to see if there are any instances that match MYTITLE and then kills those tasks. Then names itself and runs the batch. If it stays open, it will be killed next time the task it run.
Actually, the quick and easy answer to my question is to simply set up the task in Task Scheduler in the GUI and when all parameters are set, just export the xml file. Once this is done, you just have to create using the /xml option.
schtasks /create /tn [TASKNAME] /XML C:\[DIR]\schedule.xml
You can store the STATUS (i.e. {START, STOP, RUNNING, ...}) of your application along with its PID in environment variables.
For example (environment variables):
MY_APP_STATUS = 1
MY_APP_PID = 1234
For getting PID, see:
getting process ID of exe running in Bat File,
how to get own process pid from the command prompt in windows
In your batch script, you can kill the running batch if the environment variable is set using the PID.
You can use taskkill command to do this:
taskkill /pid 1234 /f
You need to reset the STATUS flag after killing the existing running batch and update the PID with the new running instance.

I cannot shutdown clients from windows server 2012?

I have few windows 7 clients whom i need to reboot from windows server . i tried to setup a group policy
Action-create ,Name- Auto shut ,Account-NT/System,Action-shutdown.exe Trigger:4.00 AM :daily
This was applied for OU but it fails to deliver Can someone points where did i went wrong here and how to do it properly??
Have you consider using the command shutdown with the /m switch?
You can create a batch script will all machines you want to reboot
#echo off
for %%a in (computer1 computer2 computer3) do (
shutdown /r /f /m \\%%a /t 30
)
and create a task that executes the script.
schtasks /create /SC DAILY /ST "04:00" /TN "task-name" /RL HIGHEST /TR "script-name"/F

How to create task using schtasks.exe with multiple days of month

I try to create this scheduled task using the command line:
I tried:
schtasks.exe /Create /tn test1 /tr notepad.exe /sc montly /m jan,feb /d 12,27
but it seems that /d can get only one parameter, for example: /d 12
How can I create the same task as in the wizard? I want to create a task that runs every 12 and 27 of january and febuary.
Thanks,
Inbal.
The only way I found to do it, is by using xml and pass it as a parameter to the /create command.

Resources