Windows Server – Delayed execution startup script

There are times when it’s useful to have a script that runs on every server boot, but with a delay.  For example, let’s say that you want a script to recycle IIS five minutes after starting the server due to some slow starting dependencies.

One of the issues with doing this is that if you add a delay to a startup script, it will delay completion of the Windows startup process.  This isn’t a problem if you’re talking 30 seconds, but when you start talking in minutes, then it becomes a problem.

Or, this is useful not only for a delay, but also to allow the windows startup to complete while a long running script finishes in the background.

I’ve put together a process that defers the execution of the script to Scheduled Tasks, which will then run at the specified delay.

The flow looks like this:

There are 3 files required to perform this.  They are:

  1. DelayStart.vbs –> Schedules the delayed execution using Scheduled Tasks;
  2. StartupScript.bat –> (or whatever you want to call it).  This is your script that you want to run;
  3. CleanupScheduledTask.bat –> This deletes the temporary scheduled task when it’s done.  You will need to call this from StartupScript.bat;

Note that schtasks.exe has a /Z parameter to auto delete itself when finished but it doesn’t appear to work for me.  If it works for you, you can drop the Cleanup script from the workflow.

CleanupScheduledTask.bat needs to be called from StartupScript.bat.  Alternately you can create 2 tasks.  One runs your script and the other runs a few minutes later to delete both scheduled tasks.

To set this up, perform the following steps:

  1. Create a folder called C:/admin/autorun
  2. Create the three scripts as shown below
  3. Edit StartupScript.bat to your liking
  4. You can edit the first few lines of the scripts to use different paths or delays. My example is set for 5 minutes delay.
  5. Schedule DelayStart.vbs to run on startup:
  1. Start –> Run –> “gpedit.msc
  2. Expand Computer Configuration/Windows Settings/Scripts
  3. Double Click Startup
  4. Click Add…
  5. Enter C:\admin\autorun\DelayStart.vbs for the Script Name
  6. Leave Script Parameters blank
  7. Click the OK button
All that is left to do is test and confirm that everything works as expected.

[tab:DelayStart.vbs]

Option Explicit

dim computer, delayMinutes, startupScript

computer = "."
delayMinutes = 5
startupScript = "C:/admin/autorun/StartupScript.bat"

Dim newTime, timeString
newTime = DateAdd("n", delayMinutes, Now())
timeString = Right("00" & DatePart("h", newTime), 2) & ":" & Right("00" & DatePart("n", newTime), 2) & ":00"

Dim WshShell, cmd
Set WshShell = WScript.CreateObject("WScript.Shell")

'delete in case previous is still around
cmd = "schtasks /Delete /TN ""DelayedStartupScript"" /F"
WshShell.Run cmd, 1, True

cmd = "C:/windows/system32/schtasks.exe /create /tn DelayedStartupScript /tr """ & startupScript & """ /sc ONCE /st " & timeString & " /ru SYSTEM "
WshShell.Run cmd, 1, True

[tab:StartupScript.bat]

:: Put whatever you want here.  Just leave the bottom line.
:: This example recycles the app pool called DefaultAppPool

C:/Windows/System32/inetsrv/appcmd.exe recycle apppool "DefaultAppPool"
C:/admin/autorun/cleanupScheduledTask.bat

[tab:CleanupScheduledTask.bat]

C:/windows/system32/schtasks /Delete /TN "DelayedStartupScript" /F

[tab:END]

SOURCE

LINK

LANGUAGE
ENGLISH