I plan to include this command in a batch file and, because of that, I can't hardcode the start time.
The schtasks command can run a defined task immediately.
Starts a scheduled task immediately. The run operation ignores the schedule, but uses the program file location, user account, and password that are saved in the task to run the task immediately.
schtasks /run /tn TaskName [/s computer [/u [domain\]user /p password]] /?
So just define the task, then run it immediately at any time you need to.
Working on top of Keith's answer to suit your needs:
timeout /t X ; psexec -i -s Y
timeoutreplaces sleep in Windows 7.
Xis the seconds to wait defined by
/nobreakif you want to disallow the user to continue.
psexecfrom Microsoft SysInternals allows you to run interactive (
-i) as the local system (
-s), please note that you have to start psexec once to accept the first time EULA, or add
Yis your command.
runas doesn't allow you to start as local system, that's why to use PsTools for process-related tasks.
Starting a Scheduled Task or Service seems like too much of a workaround for me...
For example, Patrick's answer can simply be rewritten as:
timeout /t 120 ; psexec -i -s calc.exe
This starts calc.exe in 2 minutes. Change the
set /A START_MIN=%%j + 2
line to change the time interval.
for /f "delims=:. tokens=1,2" %%i in ("%time%") do ( set /A START_HOUR=%%i set /A START_MIN=%%j + 2 ) IF %START_MIN% GEQ 60 ( call :AdjustStartTime ) IF %START_MIN% LSS 10 SET START_MIN=0%START_MIN% SCHTASKS /Create /SC ONCE /TN "My Scheduled Task Name" /RU "" /TR "C:\Windows\System32\Calc.exe" /ST %START_HOUR%:%START_MIN% :AdjustStartTime SET /A START_HOUR=%START_HOUR% + 1 SET /A START_MIN=%START_MIN% - 60 GOTO:EOF
Patrick S.'s answer met my needs.
I would like to mention however that the quotation marks do not need to be there following the
/TR and for whatever reason, seemed to break my script.
Upon removal, his script worked great!
My usage was in an Outlook rule. A script is called and an email is written to the drive with a plugin (messagesave). This is to post to a website from my office PC for a website and my original script that calls the batch needs to terminate prior to a file being written. I had a tough time figuring out how to appropriately delay the scheduled task, even after hitting the 'schtasks' TechNet page.