Create a task in Windows XP/7 Task Scheduler using batch file

In my current job we needed to automatize some processes by using Windows Task Scheduler; I wrote a batch file, which you may find useful,
especially when you don't have direct/remote access to user's desktop.

The main problem of implementation this task was Windows Vista/7 with its UAC, I had to use powershell to get Admin privileges by popping a window prompt to user directly to accept rather than have the user type the password manually upon the request.



::*******************************************::
:: schedule.bat                              ::
:: Set variables TaskName, Task and go ahed  ::
:: http://myunster.com                       ::
::*******************************************::
@ECHO off
ECHO "Proceeding..."

REM Delete variables, may be cached
SET "TaskName="
SET "Task="

REM Set variables
SET TaskName=DummyTaskName
REM Following task will be executed every hour
SET Task=C:\www.domain.dev\usr\local\php5\php.exe C:\www.domain.dev\cron.php

REM Determine if windows xp
VER | find "XP" > NUL
IF %ERRORLEVEL% == 0 GOTO ver_xp

REM Determine if windows Vista/Win7
systeminfo | find "OS Name" > %TEMP%\osname.txt
FOR /F "usebackq delims=: tokens=2" %%i IN (%TEMP%\osname.txt) DO SET Version=%%i
DEL /F %TEMP%\osname.txt

ECHO %Version% | find "Windows 7" > NUL
IF %ERRORLEVEL% == 0 GOTO ver_7
ECHO %Version% | find "Windows Vista" > NUL
IF %ERRORLEVEL% == 0 GOTO ver_vista

:ver_xp
:Run Windows XP specific commands here.
REM Delete variable, may be cached
SET "Result="
REM WinXP doesn't support TN for schtasks /query
FOR /F "delims=, tokens=2" %%R IN ('schtasks /query /fo csv /v ^| findstr /L /C:"%TaskName%"') DO SET Result=%%R
IF (%Result%)==() SET Result="-1"
IF "%TaskName%" == %Result% (
REM Delete Task if it exists
	SCHTASKS /Delete /TN "%TaskName%" /F
)
REM Then Create hourly running one
SCHTASKS /Create /TN "%TaskName%" /TR "%Task%" /SC HOURLY /RU SYSTEM
GOTO exit

:ver_vista
:Run Windows Vista specific commands here.
GOTO Elevation

:ver_7
:Run Windows 7 specific commands here.
GOTO Elevation

:Elevation
REM Don't forget escape double quotes for CMD argument that you will pass to powershell
PushD "%~dp0"
IF EXIST "%~0.ELEVATED" DEL /F "%~0.ELEVATED"
SET Argument=SCHTASKS /Create /F /TN \"%TaskName%\" /TR \"%Task%\" /SC HOURLY /RU SYSTEM
SET ELEVATED_CMD=PowerShell -Command (New-Object -com 'Shell.Application').ShellExecute('Cmd.exe', '/C %Argument%', '', 'runas')
ECHO %ELEVATED_CMD% >> "%~0.ELEVATED"
CALL %ELEVATED_CMD%
DEL /F "%~0.ELEVATED"
PopD
GOTO exit

:exit
ECHO "Done!"
EXIT




You might need powershell for Windows Vista, I don't know if it comes by default with Vista

See also: Backup your MySQL databases installed on windows with a batch file and task scheduler
  • +1
  • June 11, 2010, 10:31pm
  • EvgenyM

Comments (1)

RSS Collapse / Expand
+
0
mmm
avatar

turalo

  • June 14, 2010, 1:48am

Only registered and authorized users can leave comments. Login or Register