سؤال

I am working on Windows server 2008 r2; I scheduled a task that starts each day at 10:00 and runs for 8 hours. If the server is restarted AND time is between 10-18 THEN the task should start as soon as possible, otherwise it should not run.

If I set a trigger at startup it starts at any time.

If I set the option "run as soon as possible" and the server was not working at start time then the task is started when the server is up, if instead the task was already lauched "today" it is not restarted automatically.

So I ask your advice: is it possible to force the start of the task in the correct timeframe using just task scheduler? I'd rather avoid an approach based on "run anyway but TSKILL if time is not correct".

هل كانت مفيدة؟

المحلول

I don't think this ALL can be accomplished with just a scheduled task. You would need to create a scrip file and then schedule that. I added Day of week also because I think you might be looking for M-F (it can be removed if not needed)

REM skip if not Mon-Fri
for /f %%a in ('wmic path win32_localtime get dayofweek /format:list ^| findstr "="') do (set %%a)
IF %dayofweek% LSS  1 goto skip
IF %dayofweek% GTR  5 goto skip

REM skip if no 10am-6pm
IF %time:~0,2% LSS  10 goto skip
IF %time:~0,2% GTR  18 goto skip
echo pass

REM skip if already run today  source:https://groups.google.com/forum/#!topic/alt.msdos.batch.nt/uIzJ-rmx3PA
set Semaphore=%temp%\Semaphore.bat
if not exist "%Semaphore%" goto notRunToday
call "%Semaphore%"
if "%lastrun%" equ "%date%" (
  echo %~nx0 already ran once today.
  goto skip)
:notRunToday
echo set lastrun=%date%>"%Semaphore%"

REM run the command
MyProgram.exe

:skip
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top