Scheduled task paused for a given period
Solvedbarnabe0057 Posted messages 14431 Registration date Status Contributeur Last intervention -
Hello,
I have created scheduled tasks to turn on computers at the beginning of the day and to turn them off at the end of the day. I would like to ensure that during holidays the PCs do not turn on in the morning, so to pause or temporarily disable the computer restarts from such date to such date. Then to reactivate this task for another period, and so on.
Is this possible?
Thank you
14 réponses
Hello,
We are not sure if the PCs are physically turned off and if the two scheduled tasks are launched from a central computer for the entire network or on each PC.
In the first case, even if they are turned off, we can use the Wake on Lan function if the computers are equipped with it, but in any case, it is enough to disable the network-deployed tasks from the central computer.
If these tasks are now launched on each PC, there is no way to do it if they are turned off, so I assume they are in hibernation.
The task manager only allows delaying a task for a random duration and does not support holiday dates or others that it does not know, so I can’t even set the start and end dates manually by choosing those dates.
We get a bit more "finesse" by setting the task not graphically but via SCHTASKS in the command line, but the options are only month, week, start date/end date, enable/disable, which means I will have to modify these two tasks based on one of the last two criteria and then copy them to each PC.
Unless there is a considerable number of PCs across multiple sites, wouldn’t it be quicker to just press the power button on each PC or unplug them?
Thank you for your response
Actually, I use the wake on lan application from Aquila, and we can schedule the start and stop of machines throughout the network at specific times
The software uses the scheduled tasks feature of Windows, so once set up, the actions can be found in the task scheduler.
I am therefore obliged before every vacation to disable the startup tasks and then reactivate them at the end of the vacation
The idea would be to automate this by specifying on which dates to disable and then reactivate the startup task
For example with a formula like this
If date = ddmmyyyy > stop task named "start pcs"
AND this for 15 days before reactivating it
Something like that, I haven't tested it?
Start by testing the functionality, if it works, it will be time to refine it by entering the dates interactively and/or by entering just the start date and then calculating the end date based on a number of days; if End and Run are not enough, you will have to modify the dates via SCHTASKS, which will generate a slightly more complicated script.
Note that the date must be in the format dd/mm/yyyy.
@echo off SET START=31/10/2022 SET END=06/11/2022 IF %DATE% LSS %START% GOTO EOF IF %DATE% GTR %END% GOTO END SCHTASKS /End /TN task-name GOTO EOF :END SCHTASKS /Run /TN task-name :EOF
Okay, thanks, I'll explore that.
I assume that it's better to indicate the location of the task rather than just the name. How can I find the place where the scheduled task is stored?
For now, when I run the script, it says "the specified file cannot be found."
The default location of tasks is (they are stored in the registry), but I believe I forgot in the syntax that for the /Create option the name can be raw as task-name while for the other switches it must be specified in quotes "task-name".
This is of course a task launched on the local PC (the name is not called on a network PC) otherwise one must specify the /S switch:
SCHTASKS /End /S system /TN "task-name"
It is necessary to specify if applicable the user owner of this task:
SCHTASKS /End /U username /P password /TN "task-name"
If the /Run and /End syntaxes are to be blamed (I haven't tested), to test the syntax do a dummy creation of a task that we will delete afterwards, I won't tire myself, I just copy a repetitive example:
SCHTASKS /create /tn "On demand demo" /tr "C:\Windows\notepad.exe" /sc ONCE /sd 01/01/1910 /st 00:00
I'm a bit lost and not sure I understood. I created a batch file with the proposed commands, but it's indicating a file not found. I conclude that it cannot identify where the task is located. Does the script have a specific location? Should I create the script directly in the task scheduler or should I create a classic task with the batch file as the program to run every day?
Here’s what I’ve done so far. With the WakeOnLan software, I created locally on the server, 2 scheduled tasks; starting PCs in the morning and shutting down PCs in the evening. They are located in the task scheduler library > Aquila Technology > WakeOnLan
Then I created a .bat file with the proposed command that stops the start task if that date is reached. In the task scheduler, I created a new task with properties set to start every day, program to execute = the batch file
Regarding the syntax, I am not familiar with your Wake on Lan utility and I don't know to what extent the SCHTASKS command will find the task if it is not at the root of the task scheduler library.
We will start by checking the syntax, you never know, start by creating a new task at the root of the scheduler that I will name test; I will create a file message.cmd wherever I want on my disk, then I specify in Actions under Program: cmd and under arguments: /C "G:\!Menus\OutilsSys\taches\message.cmd" where what precedes message is the path to message.cmd on the disk.
echo "toto"
pause
that you will start by executing manually the first time to enter the appropriate user rights, then schedule to run for example every day at a near future time and repeat every 15 minutes.
The SCHTASKS batch file is only meant, under the circumstances you mentioned, to modify the execution conditions of an already existing task; it is written in a batch for convenience, but could perfectly well be written from the command line, in any case it should not be written in a Russian doll mode in a second task since it will only be executed on demand, modifying once and for all (at least until instructed otherwise through the same means) the execution days.
Once my test task is set up, I will create a second batch that I will call stop.cmd and I will also place it wherever I like:
rem @echo off (to test, I want to see what happens)
SCHTASKS /End /TN test
rem I also put a pause to test what happens
pause
I execute as administrator to check if the display of toto disappears (and reappears if I replace /End with /Run), I add /U and /P switches if I get thrown out for insufficient rights.
If it works, then the syntax is not the issue; we can then consider that your problem does not work because your task is not at the root of the scheduler, and that it must specify the path via /TN "FOLDERPATH\TASKNAME":
Assuming I want to tackle, for example, the task that checks if Firefox is the default browser and which is not at the root of the scheduler but in the Mozilla folder, I write (note, it seems that I am only allowed 255 characters)
SCHTASKS /End /TN "Mozilla\Firefox Default Browser Agent 308046B0AF4A39CB"
The computer from which the SCHTASKS command is launched must not be in sleep mode itself, as the command is not able to "wake it up".
Sorry, I'm having trouble getting started this morning.
What I wrote concerns the manual launch or stop.
If I want to test a start and end date every day, I will have to either schedule my SCHTASKS batch based on what is <3> by specifying the destination task path if needed so that it runs every day at the time I've chosen, or if the machine hosting it is turned off at night, I should put it in the start menu.
Okay, thanks, I'm starting to make progress. In fact, since the task has already been created by Aquila Software, I need to specify the path as it doesn't get added to the root of the task library, namely:
SCHTASKS /End /TN "Aquila Technology\Wake On LAN\Demarrer_pc_matin"
This works, but the message indicates "the task has been completed", whereas what I want is not to stop the ongoing task but to pause it, to disable it. When you right-click on a task, this option is indeed available. What is the batch command to disable a task?
Regarding the commands for integrating the holiday periods, I'm not sure if I understood correctly. Because here too, it's not about "running" the task for periods outside of holidays but just activating it, since it will be the software that launches the task to start the PCs. Is there an activation command for the task?
Do the commands indicate this correctly:
if %date% less than %debut% or greater than %fin% (i.e., outside the holiday period) then do nothing
else (so between the start and end of the holiday period): disable the PC startup task
Oops, I should have looked a bit! I just found the command I had tried using /disable instead of /end but I need to add /change
So, my disable command is as follows
SCHTASKS /change /disable /TN "Aquila Technology\Wake On LAN\Demarrer_pc_matin"
For now, the batch is not working; there must be a glitch during the vacation period
Basically, if it's doable, here’s how I envision the batch so that it can work throughout the year with the input of each of the 5 vacation periods:
set toussaint1=30/10/2022
set toussaint2=6/11/2022
set noel1=
set noel2=
set hivers1=
set hivers2=
set paques1=
set paques2=
set ete1=
set ete2=
If %date% is between toussaint1 and toussaint2 or noel1 and noel2 or .....
then launch the command SCHTASKS /change /disable /TN "Aquila Technology\Wake On LAN\Demarrer_pc_matin"
Otherwise, launch the command SCHTASKS /change /enable /TN "Aquila Technology\Wake On LAN\Demarrer_pc_matin"
Yes, why not.
Disable is generally after the task, I don’t know if that matters:
SCHTASKS /change /TN "Aquila Technology\Wake On LAN\Start_pc_morning" /disable (enable to restore)
/End is designed to terminate the task, I suppose the result is the same (Run to restore).
We have no other levers to act in Batch: SLEEP does not ensure a sufficient delay, TASKKILL actually kills not the task but the associated process only when it has started.
Rather than making a conditional and while we’re at it using /change, we could specify that in the example above, the task should no longer be executed from the start date of the vacation using the /ED switch, but it should be from the end date of the vacation /SD, but we might have it behind our back because the start date is greater than the end date, and in any case, it’s probably heavier to put the appropriate dates in 2 to 4 SCHTASKS sentences.
Alternatively, we can use the date condition that I illustrated in <3>, with /End or /Disable.
If the current date %DATE% is strictly less than LSS the start date of the vacation, we do nothing: EOF.
Otherwise, we must also check that the end date is not exceeded GTR: if it is, we go to FIN which launches the second SCHTASKS sentence that wakes up in the morning.
Between the two, we disable the wake-up task and exit.
I have not taken the second task (shutdown in the evening) into account because if by definition the computers are not turned on during the vacation, they won't be shut down.
However, the corresponding task will continue to run and produce an error message; if we're purists, we can avoid that by duplicating the first SCHTASKS sentence with exactly the same one that this time will disable the evening task.
@echo off SET DEBUT=31/10/2022 SET FIN=06/11/2022 IF %DATE% LSS %DEBUT% GOTO EOF IF %DATE% GTR %FIN% GOTO FIN SCHTASKS /change /TN "Aquila Technology\Wake On LAN\Start_pc_morning" /disable SCHTASKS /change /TN "Aquila Technology\Wake On LAN\Shutdown_pc_evening" /disable GOTO EOF :FIN SCHTASKS /change /TN "Aquila Technology\Wake On LAN\Start_pc_morning" /enable SCHTASKS /change /TN "Aquila Technology\Wake On LAN\Shutdown_pc_evening" /enable :EOF
The /disable or /enable seems to be able to be placed before or after
For the shutdown task, I have no interest in disabling it as long as it remains active every day of the year, so that in case of a one-time startup during the holidays, the PCs will still shut down in the evening
In fact, for it to achieve what I want, namely to disable the startup during the period (between the start and end of the holidays), I need to reverse the enable and disable
This gives
@echo off SET DEBUT=31/10/2022 SET FIN=06/11/2022 IF %DATE% LSS %DEBUT% GOTO EOF IF %DATE% GTR %FIN% GOTO FIN SCHTASKS /change /TN "Aquila Technology\Wake On LAN\Demarrer_pc_matin" /enable GOTO EOF :FIN SCHTASKS /change /TN "Aquila Technology\Wake On LAN\Demarrer_pc_matin" /disable :EOF Is there a way to integrate all the holidays into a single file? A bit like the idea in post <10>
I would need to create as many "set" commands as there are holidays, but then the LSS and GTR might contradict each other
No "between" command exists in batch?
I told my life story while you were modifying your message yourself, I didn't see that you wanted to do it with a shotgun for the whole year, although fundamentally it doesn't change much (we'll modify the script once a year instead of once a quarter), but Barnabe offered you a solution in that regard.
I don't understand your enable/disable inversion: if we are during school holidays, shouldn't we indeed prevent PCs from starting up in the morning?
As for the rest, I've used a utility for a long time that worked wonders for date handling, where DOS isn't very good (and in which you could also define a separate file for holidays), but it's pure and hard 16-bit DOS and therefore will only work in emulation on a 32-bit system; on a 64-bit system, it's not even worth trying.
For all intents and purposes (there may be other emulated DOS utilities managing the date on a 64-bit system since, I haven't looked into it since):
https://archive.org/details/freesoftwarefordos_batch_fdate2
Hello everyone,
I think this could help you:
@echo off chcp 65001 >nul setlocal enabledelayedexpansion set "allsaints=31/10/2022-06/11/2022" set "christmas=17/12/2022-03/01/2023" set "winter=11/02/2023-27/02/2023" for %%A in (%allsaints% %christmas% %winter%) do call :checkDate "%%~A" echo. & pause exit :checkDate set /a holidays=0 set "today=!DATE:~6,4!!DATE:~3,2!!DATE:~0,2!" rem set "today=20221031" echo. & echo ### %today% ### %~1 for /f "tokens=1-6 delims=/-" %%A in ("%~1") do ( set "startHolidays=%%C%%B%%A" set "endHolidays=%%F%%E%%D" ) if %today% GEQ !startHolidays! (if %today% LEQ !endHolidays! (set /a holidays=1)) if !holidays! EQU 1 (set action=disable) else (set action=enable) echo SCHTASKS /change /TN "Aquila Technology\Wake On LAN\Start_pc_morning" /!action! echo SCHTASKS /change /TN "Aquila Technology\Wake On LAN\Shut_down_pc_evening" /!action! goto :eof Don't forget to remove the ECHO in front of SCHTASKS once you're sure everything works as you want.
wow thanks, I see roughly what the script does, it is quite complete
however, it seems to do things in reverse and activates during vacation while the commands seem to request a deactivation. Is that normal? I modified it by reversing action=enable (for 1) and action=disable (for 0, that is, else). Strange but it seems to work.
the script detects vacation=0 even though I indicated I am right in the middle of the All Saints' Day holiday by modifying the start and end dates.
We're agreed that I can run this script every morning? It should thus redefine the deactivation or activation of the task according to the current date and the indicated holiday period
Yes, you can run it every morning.
I don't think he does things the wrong way around; when it's vacation (vacation=1), he disables the 2 scheduled tasks. As you mentioned earlier, it would be better not to disable the task that turns off the PCs in the evening.
By default, vacation=0, so if there is ever a problem in the script (incorrectly formatted date, etc ...), the tasks remain activated.
To test with the dates, you can modify today's date by adjusting this line:
rem set "today=20221031"
Do not touch the variables debutVacances and finVacances.
There's something wrong. I run my tests by changing the date in the PC settings. When the script is executed, it says "the scheduled task has already been activated," whereas normally it should disable it since I'm on school vacation.
The line with REM is not supposed to influence the script, right? Since it's just a remark.
In any case, when I change the date on the PC, it detects the correct date, but the calculation doesn't seem to work properly, and it can't determine if I'm on vacation or not.
Yes, the current date is indeed displayed in the MS-DOS window. When I type echo %date%, it shows the date as December 30, for example. However, it lists multiple lines for each holiday. I'm not sure the script correctly identifies which holiday period I'm in.
Here is a video showing that the task remains active even during the holiday period.
https://drive.google.com/file/d/12C4E1XcZFrtNIL_qYQHEzLioZarpRLtZ/view?usp=sharing
I have doubts about some commands. What does this one do precisely?
for /f "tokens=1-6 delims=/-" %%A in ("%~1") do ( set "debutVacances=%%C%%B%%A" set "finVacances=%%F%%E%%D" )
This command formats the date, changing from
31/10/2022-06/11/2022 to 20221031 and 20221106 The loop splits the string %~1 (which is, for example, 31/10/2022-06/11/2022) using the characters - and / resulting in 6 pieces of strings %%A %%B %%C %%D %%E %%F
-
I'm not sure the script correctly identifies which holiday period I'm in
==>> you've got me thinking, I believe I know where the problem comes from: the script does not remember whether today's date corresponds to a specific holiday period.
-
Consequently, if today's date corresponds to the All Saints' or Christmas holidays, the script will then go after the winter holidays and undo whatever it might have done just before.
-
My apologies for this oversight, here is the correction:
@echo off chcp 65001 >nul setlocal enabledelayedexpansion set "toussaint=31/10/2022-06/11/2022" set "noel=17/12/2022-03/01/2023" set "hiver=11/02/2023-27/02/2023" set /a vacances=0 for %%A in (%toussaint% %noel% %hiver%) do call :controleDate "%%~A" SCHTASKS /change /TN "Aquila Technology\Wake On LAN\Demarrer_pc_matin" /!action! SCHTASKS /change /TN "Aquila Technology\Wake On LAN\Eteindre_pc_soir" /!action! echo. & pause exit :controleDate if !vacances! EQU 1 (goto :eof) set "today=!DATE:~6,4!!DATE:~3,2!!DATE:~0,2!" rem set "today=20221031" echo. & echo ### Today's date: !DATE! ### Holidays: %~1 for /f "tokens=1-6 delims=/-" %%A in ("%~1") do ( set "debutVacances=%%C%%B%%A" set "finVacances=%%F%%E%%D" ) if %today% GEQ !debutVacances! (if %today% LEQ !finVacances! (set /a vacances=1)) if !vacances! EQU 1 (set action=disable) else (set action=enable) goto :eof
That's great!! It works perfectly. I'm even going to add all the holidays at the beginning of the script for a few years so I don't have to modify the holiday periods each year.
Thanks again to both of you for your always valuable help.
Hello
I have a small issue with a new task I created called Demarrer_pc_soir. When I add it to my software Aquila and include it in the script to activate or not based on the vacation period, I get this message:
Error, the specified task name "(...)\Demarrer_pc_soir" does not exist in the system
Any ideas? There doesn't seem to be any error with the case, and the task is indeed present in the task manager
