在Windows启动时运行脚本,而无需用户登录 [英] Run a script on Windows startup without a user logged on

查看:4330
本文介绍了在Windows启动时运行脚本,而无需用户登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个Windows 2003计算机,每天早上开机,但没有人登录,直到几个小时后。



我想使用之间的时间运行备份脚本c:\script\backup.cmd



如何在机器启动后启动这个脚本?



我尝试了两个注册表项,但这导致脚本在用户登录后运行(太晚了):

  HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run 

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunServices

最后我使用的Windows TaskScheduler,谁有这样的选择,但我想知道是否有另一种可能性?

解决方案

简短答案 GPEDIT.MSC GPEdit.msc)



Windows 2000及以上版本[1]在政策编辑器中提供了启动脚本集合:




  • 计算机设置 - > Windows设置 - > 脚本(启动/关闭)



有一个等效的登录脚本区

$ b

GPEDIT.MSC是组策略编辑控制台,在直接使用本地计算机的组策略存储时运行,因此它对设置仅限本地的参数很有用。使用Active Directory时,使用相同的界面来编辑林中托管的组策略对象(GPO),因此在一堆机器上可以使用相同的设置。



如你所说,计算机启动脚本在计算机上下文(即LocalSystem)中运行,因此它们通常不能访问需要某个用户或组成员资格工作的网络驱动器,等等。当计算机访问网络时,他们通常(有例外)使用其MACHINENAME $帐户。



启动脚本是一种快速简单的方法当机器启动时让进程运行。



计算机启动过程将受到运行程序所需的时间的影响,因此您可能需要确保使用批处理中的START命令调用它文件,或指定不等待可执行文件以您使用的任何脚本语言完成。 (关键点是:异步运行脚本 ,除非它是关键的,或者不需要异步运行,否则它将永远占用 =不愉快的用户)。



使用Win32服务是一个替代选项 - 您可以使用资源工具包中的 SRVANY ify几乎任何可执行文件。 VS.Net 2002及更高版本还允许您直接构建托管服务。



并且任务计划程序能够在启动时,空闲时和/或当生成事件日志或满足某些其他条件时运行脚本:它非常酷!计划任务具有可能的优点,即能够指定运行任务的用户帐户,如果这对您很重要。



Caveat脚本:
http://support.microsoft.com/kb/256320



异步运行启动脚本:
http:/ /msdn.microsoft.com/en-us/library/ms811602.aspx



Vista任务计划程序(新增功能):
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一切。但是NT4没有!


This is a Windows 2003 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?

解决方案

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 Group Policy store when it's used directly, so it's useful for setting local-only parameters. When using Active Directory, the same interface is used to edit 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. LocalSystem, as you noted, so they often can't access network drives which require a certain user or group membership to work, and so on. When computers access the network, they generally (with exceptions) use their MACHINENAME$ account.

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!

这篇关于在Windows启动时运行脚本,而无需用户登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆