16

This is a Windows 2003 (or later) machine that gets switched on every morning, but no one logs on until some hours later.

I want to use the time in between to run a backup script c:\script\backup.cmd

How can I start this unattended after the machine has come up?

I tried 2 registry keys, but this resulted in the script being run after a user logs on (which is too late):

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunServices

In the end I used Windows TaskScheduler, who has such an option, but I was wondering if there is another possibility?

5 Answers 5

19

Short answer: GPEDIT.MSC (Start, Run, GPEdit.msc)

Windows 2000 and above [1] offer a computer Startup Scripts collection in the policy editor:

  • Computer Settings -> Windows Settings -> Scripts (Startup/Shutdown)

There's an equivalent logon script area (i.e. after computer startup, when a user logs on) in the User configuration bit.

Longer:

GPEDIT.MSC is the Group Policy editing console, and runs against the local computer's Local Group Policy store when it's used directly, so it's useful for setting local-only parameters. When using Active Directory, a similar interface is used to edit AD Forest-hosted group policy objects (GPOs), so the same settings are available across a bunch of machines.

The computer startup scripts run in the computer context, i.e. as LocalSystem, as you noted, so they often can't access network drives which require a certain user or group membership to work. When domain-member computers access network resources, they generally (with exceptions) authenticate using their MACHINENAME$ account - which lets you set Share/NTFS permissions to allow a computer to access those resources.

A startup script is a quick and easy way of getting a process running when the machine boots.

The computer startup process will be affected by the time it takes to run the program, though, so you might want to ensure you call it with the START command from a batch file, or specifying not to wait for the executable to complete in whatever script language you use. (the key point there is: run the script asynchronously unless it's critical, or doesn't need to be run asynchronously cos it will always take no time at all. Long boots = unhappy users).

Using a Win32 Service is an alternative option - you can use the SRVANY utility from the Resource Kit to "service-ify" pretty much any executable. VS.Net 2002 and later also let you build a managed service directly.

And Task Scheduler gets much more capable as of Vista/2008, able to run scripts at startup, on idle, and/or when Event Logs are generated or certain other conditions are met: it's pretty cool! Scheduled Tasks has the possible advantage of being able to specify the user account under which the task runs, if that's important to you.

Caveat Scriptor: http://support.microsoft.com/kb/256320

Run Startup Scripts Asynchronously: http://msdn.microsoft.com/en-us/library/ms811602.aspx

Vista Task Scheduler (what's new): http://technet.microsoft.com/en-us/appcompat/aa906020.aspx

[1] Windows XP, 2003, Vista/2008, Windows 7/2008R2, Windows 8/2012, Windows 8.1/2012R2, Windows 10/Windows Server 2016. Everything. But NT4 didn't!

4
  • 1
    Thank you, it is obvious you answered it from any angle. Commented Mar 6, 2009 at 1:08
  • Computer Settings/Configuration --> Windows Settings --> Scripts This can be found by opening the Local Group Policy Editor, since no one explained how to even get to this window.
    – liltitus27
    Commented Oct 10, 2014 at 17:05
  • @TristanK , I have a Windows 2K , where we clone it (vmware) and isolate it from the network (cutoff the network adapter) , for few tests with our database. But then, the LOCAL GPO startup/shutdown scripts aren't start anymore. The Local GPO startup scripts are dependent of active nertwork ou AD available?
    – ceinmart
    Commented Jun 2, 2017 at 13:26
  • Wait, you have Windows 2000 machines? And they are sometimes network connected? That's a really, really bad combination in 2017. Anyway, I'm pretty sure I used to use this system with workgroup machines in Windows 2000... don't know of any specific reason it would fail when network disconnected, unless there's something fundamental wrong there - eg specific to the script itself, or it's stored in AD/on Sysvol instead of the local hard drive, etc.
    – TristanK
    Commented Jun 5, 2017 at 6:15
8

You have already outlined a good solution:

Setup a scheduled task to run at Start Up and allow the job to run when the user isn't logged on.

1
  • Yes, this works fine. But I would like to disable Task Scheduler so I was looking for a different approach. Commented Mar 5, 2009 at 23:13
1

You can run a script at system startup using group policy gpedit.msc

1
  • This seems fast and easy. As which user would the process run? LocalSystem`? Commented Mar 5, 2009 at 23:12
0

The way you aleady do this seems fine to me; however if you want an alternative approach then services get started when the machine boots so you could write a service that detects if it's a new day (to allow for reboots) and if it is then run your backup.

If I was doing this as a service I'd use TCL because I know it and like it and it has an extension twapi that allows you to run a script as a service. Other scripting languages may well have similar facilities.

1
  • Thank you this looks interesting. When I will have to write a service one day I'll have a look at TCl / twapi Commented Mar 5, 2009 at 23:08
0

There is, if you're using Active Directory. If you can isolate the computer to its own OU or use WMI filtering, you could assign a GPO which has a startup script for the computer. This would ensure that even if someone went in via safe mode and disabled the Task Scheduler, upon startup and connection to the domain, the script would run.

1
  • Thank you, this is a great thing to know, I have some machines in AD and will try this one day. Commented Mar 5, 2009 at 23:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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