在没有用户登录的情况下在 Windows 启动时运行脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/614766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run a script on Windows startup without a user logged on
提问by Karl Thorwald
This is a Windows 2003 machine that gets switched on every morning, but no one logs on until some hours later.
这是一台 Windows 2003 机器,每天早上开机,但直到几个小时后才有人登录。
I want to use the time in between to run a backup script c:\script\backup.cmd
我想利用中间的时间运行备份脚本 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):
我尝试了 2 个注册表项,但这导致脚本在用户登录后运行(为时已晚):
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?
最后我用的是Windows TaskScheduler,谁有这样的选择,但是我想知道还有没有其他的可能?
回答by TristanK
Short answer:GPEDIT.MSC(Start, Run, GPEdit.msc)
简答:GPEDIT.MSC(开始、运行、GPEdit.msc)
Windows 2000 and above [1] offer a computer Startup Scriptscollection in the policy editor:
Windows 2000 及更高版本 [1]在策略编辑器中提供了一个计算机启动脚本集合:
- Computer Settings-> Windows Settings-> Scripts (Startup/Shutdown)
- 计算机设置-> Windows 设置->脚本(启动/关闭)
There's an equivalent logonscript 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.
GPEDIT.MSC 是组策略编辑控制台,直接使用时针对本地计算机的组策略存储运行,因此它对于设置仅限本地的参数很有用。使用 Active Directory 时,使用相同的界面来编辑林托管的组策略对象(GPO),因此相同的设置可在一组计算机上使用。
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.
正如您所指出的,计算机启动脚本在计算机上下文中运行,即本地系统,因此它们通常无法访问需要特定用户或组成员身份才能工作的网络驱动器,等等。当计算机访问网络时,它们通常(有例外)使用它们的 MACHINENAME$ 帐户。
A startup scriptis 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 asynchronouslyunless it's critical, or doesn't need to be run asynchronously cos it will always take no timeat all. Long boots = unhappy users).
但是,计算机启动过程将受到运行程序所需时间的影响,因此您可能希望确保使用批处理文件中的 START 命令调用它,或者指定不等待可执行文件在任何脚本中完成你使用的语言。(关键点是:异步运行脚本,除非它很关键,或者不需要异步运行,因为它永远不会花时间。长靴 = 不满意的用户)。
Using a Win32 Service is an alternative option - you can use the SRVANYutility from the Resource Kit to "service-ify" pretty much any executable. VS.Net 2002 and later also let you build a managed service directly.
使用 Win32 服务是另一种选择 - 您可以使用Resource Kit 中的SRVANY实用程序来“服务化”几乎任何可执行文件。VS.Net 2002 及更高版本还允许您直接构建托管服务。
And Task Schedulergets muchmore 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.
和任务计划得到多少更能够作为对Vista / 2008,能够在启动时在空闲到运行脚本,和/或在事件日志中生成或某些其他条件都满足:它很酷!如果这对您很重要,计划任务具有能够指定运行任务的用户帐户的可能优势。
Caveat Scriptor: http://support.microsoft.com/kb/256320
警告脚本:http: //support.microsoft.com/kb/256320
Run Startup Scripts Asynchronously: http://msdn.microsoft.com/en-us/library/ms811602.aspx
异步运行启动脚本:http: //msdn.microsoft.com/en-us/library/ms811602.aspx
Vista Task Scheduler (what's new): http://technet.microsoft.com/en-us/appcompat/aa906020.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. Everything. But NT4 didn't!
[1] Windows XP、2003、Vista/2008、Windows 7/2008R2、Windows 8/2012、Windows 8.1/2012R2、Windows 10/Windows Server 2016。应有尽有。但是NT4没有!
回答by GateKiller
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.
设置计划任务以在启动时运行并允许作业在用户未登录时运行。
回答by GateKiller
You can run a script at system startup using group policy gpedit.msc
您可以使用组策略 gpedit.msc 在系统启动时运行脚本
回答by Hymanson
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 Iwas doing this as a service I'd use TCL because I know it and like it and it has an extension twapithat allows you to run a script as a service. Other scripting languages may well have similar facilities.
如果我将它作为服务来做,我会使用 TCL,因为我知道并喜欢它,并且它有一个扩展twapi,允许您将脚本作为服务运行。其他脚本语言可能也有类似的功能。
回答by K. Brian Kelley
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.
有,如果您使用的是 Active Directory。如果您可以将计算机与其自己的 OU 隔离或使用 WMI 筛选,则可以分配一个具有计算机启动脚本的 GPO。这将确保即使有人通过安全模式进入并禁用了任务计划程序,在启动和连接到域时,脚本也会运行。