Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I would like to schedule a one-time server restart, e.g. to finish installing updates early in the morning. How can I do this from the command line on Windows 2012?

On Windows 2008, I would have used the at command,

at 2am shutdown -r -f -c "restart"

and taken the rest of the afternoon off.

But on Windows 2012, running this command tells me that

The AT command has been deprecated. Please use schtasks.exe instead.

So the equivalent command with schtasks.exe might be

schtasks /create /sc once /tn restart /tr "shutdown - r -f ""restart""" /st 02:00

Apart from being very forgettable, this command has another important downside: it schedules the task for 2am today—not much use unless I'm awake at 1am to run it.

According to the help for schtasks.exe, the /sd switch for setting the start date is not applicable with /sc once. So even if I wanted to type out tomorrow's date in mm/dd/yyyy format—and I don't—I can't do this.

The only possible solution I've found is here, where Kevin Traas suggests creating batch file to create a scheduled task just before midnight that waits for a couple of minutes and then creates another scheduled task to run the command that you actually want to run. Clever, but nowhere near as convenient as at.

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

Despite the documentation, the /SD parameter seems to be compatible with the /SC ONCE. The task is successfully created to run on the date provided, at the time provided. (Tested on W8 and W7)

Additionally, the XP documentation for schtasks.exe goes so far as to say the /SD parameter is required when using /SC ONCE, so I imagine there are a fair number of scripts using the combination.

Example:

C:\Users\Mitch>schtasks /create /sc once /tn restart /tr "shutdown -r -f ""restart""" /st 23:00 /sd 2013-06-13
SUCCESS: The scheduled task "restart" has successfully been created.

C:\Users\Mitch>

If going against the documentation does not sit well with you, consider generating the XML file directly (schema is here), which is definitely supported and definitely supports a task scheduled to run once on a future date. An easy way to get the proper file is to create it in the Task Scheduler mmc snap in and use the export command.

Example:

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2013-06-12T21:20:51.004181</Date>
    <Author>FOOBAR\Mitch</Author>
  </RegistrationInfo>
  <Triggers>
    <TimeTrigger>
      <StartBoundary>2013-06-13T22:20:29</StartBoundary>
      <Enabled>true</Enabled>
    </TimeTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>FOOBAR\Mitch</UserId>
      <LogonType>InteractiveToken</LogonType>
      <RunLevel>LeastPrivilege</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>P3D</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>Your command here</Command>
    </Exec>
  </Actions>
</Task>

Command to import:

schtasks /CREATE /TN "Task Name" /XML filename.xml
share|improve this answer
    
Thanks for the tip about /sd being supported. That's interesting about the XML import too—although personally I don't care about going against the documentation. So at least this is possible—just much, much more difficult than it used to be with at. –  Luke Sampson Jun 13 '13 at 3:25
    
Mildy interesting: /sd doesn't work with /sc once on Windows Server 2008. It results in ERROR: Incorrect Start Date. –  Luke Sampson Jun 13 '13 at 7:06
1  
Hmm... I additionally tested Server 2008 SP2 and Server 2008 R2 without issue. I will note that the date format is based off of your local date/time format, and leading zeroes are required. In the default EN-us install, this would lead to formatting like /SD 06/14/2013. –  Mitch Jun 13 '13 at 20:59
add comment

I ended up creating runat.ps1 to replicate some of the simple features of at, incorporating the wisdom from Mitch's answer.

The syntax is runat (time) (command), e.g.

runat 2am shutdown -r -f -c "restarting the server"
runat 10:15 msg * it`'s 10:15 everyone!

If you, too, are nostalgic for the good old days when you didn't have to type...

schtasks /create /ru system /rl highest /sc once /tn restart /tr shutdown -r -f -c \"restarting the server\" /st 02:00 /sd 06/15/2013

... then you can install it by running this powershell command:

iex (new-object net.webclient).downloadstring('https://gist.github.com/lukesampson/5779205/raw/install.ps1');install runat https://gist.github.com/lukesampson/5778559/raw

Or you can manually download runat.ps1 script here.

share|improve this answer
1  
+1 That is a very helpful script... this is definitely going in my favorites. –  Mitch Jun 14 '13 at 17:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.