在 Windows 关机时执行批处理脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12434863/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:52:43  来源:igfitidea点击:

Executing a batch script on Windows shutdown

windowsbatch-filewindows-7shutdowngroup-policy

提问by PNS

Is there any way, in Windows 7 Professional, to run a batch script (e.g., a .BAT file) when the user clicks on "shutdown" (not a batch file scheduled to shut down the machine, just one that runs only if the user clicks on "shutdown")?

有没有什么办法可以在Windows 7 Professional用户单击“关闭”时运行批处理脚本(例如 .BAT 文件)(不是计划关闭机器的批处理文件,只有当用户单击“关掉”)?

Ideally, such a script would show the command prompt window and offer an option to cancel the shutdown procedure.

理想情况下,这样的脚本将显示命令提示符窗口并提供取消关闭过程的选项。

Solutions that do not require a third-party utility would be preferable.

不需要第三方实用程序的解决方案会更可取。

回答by bahrep

You can create a local computer policy on Windows. See the TechNet at http://technet.microsoft.com/en-us/magazine/dd630947

您可以在 Windows 上创建本地计算机策略。请参阅http://technet.microsoft.com/en-us/magazine/dd630947 上的 TechNet

  1. Run gpedit.mscto open the Group Policy Editor,
  2. Navigate to Computer Configuration | Windows Settings | Scripts (Startup/Shutdown).
  1. 运行gpedit.msc打开组策略编辑器,
  2. 导航到计算机配置 | Windows 设置 | 脚本(启动/关闭)

enter image description here

在此处输入图片说明

回答by asif

Well, its an easy way of doing some registry changes: I tried this on 2008 r2and 2016servers.

嗯,这是进行一些注册表更改的简单方法:我在2008 r22016服务器上尝试过这个。

Things need to be done:

需要做的事情:

  1. Create a text file "regedit.txt"
  2. Paste the following code in it:
  1. 创建一个文本文件“ regedit.txt
  2. 在其中粘贴以下代码:
Windows Registry Editor Version 5.00 

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts]

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\Shutdown] 

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Shutdown]     

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\Shutdown
regedit.exe /s regedit.reg
] "GPO-ID"="LocalGPO" "SOM-ID"="Local" "FileSysPath"="C:\Windows\System32\GroupPolicy\Machine" "DisplayName"="Local Group Policy" "GPOName"="Local Group Policy" [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\Shutdown
regedit.exe /s regedit.reg
mkdir C:\Windows\System32\GroupPolicy\Machine\Scripts\Startup
mkdir C:\Windows\System32\GroupPolicy\Machine\Scripts\Shutdown
mkdir C:\Windows\System32\GroupPolicy\User\Scripts\Startup
mkdir C:\Windows\System32\GroupPolicy\User\Scripts\Shutdown
] "Script"="terminate_script.bat" "Parameters"="" "ExecTime"=hex(b):00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Shutdown
Group Policy\Local Computer Policy\Windows Settings\Scripts (Startup/Shutdown)
] "GPO-ID"="LocalGPO" "SOM-ID"="Local" "FileSysPath"="C:\Windows\System32\GroupPolicy\Machine" "DisplayName"="Local Group Policy" "GPOName"="Local Group Policy" [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Shutdown##代码####代码##] "Script"="terminate_script.bat" "Parameters"="" "IsPowershell"=dword:00000000 "ExecTime"=hex(b):00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00
  1. Save this file as regedit.regextension

  2. Run it on any command line using below command:

    ##代码##
  1. 将此文件保存为regedit.reg扩展名

  2. 使用以下命令在任何命令行上运行它:

    ##代码##

回答by paka

I found this topic while searching for run script for startup and shutdown Windows 10. Those answers above didn't working. For me on windows 10 worked when I put scripts to task scheduler. How to do this: press window key and write Task scheduler, open it, then on the right is Add task... button. Here you can add scripts. PS: I found action for startup and logout user, there is not for shutdown.

我在搜索启动和关闭 Windows 10 的运行脚本时发现了这个主题。上面的那些答案不起作用。对我来说,当我将脚本放入任务计划程序时,我在 Windows 10 上工作。如何做到这一点:按窗口键并编写任务计划程序,打开它,然后右侧是添加任务...按钮。您可以在此处添加脚本。PS:我找到了启动和注销用户的操作,没有关闭。

回答by rss2363

Create your own shutdown script - called Myshutdown.bat - and do whatever you were going to do in your script and then at the end of it call shutdown /a. Then execute your bat file instead of the normal shutdown.

创建您自己的关机脚本 - 称为 Myshutdown.bat - 并执行您将在脚本中执行的任何操作,然后在脚本结束时调用 shutdown /a。然后执行您的 bat 文件而不是正常关机。

(See http://www.w7forums.com/threads/run-batch-file-on-shutdown.11860/for more info.)

(有关更多信息,请参见http://www.w7forums.com/threads/run-batch-file-on-shutdown.11860/。)

回答by Dugb

For the above code to function; you need to make sure the following directories exist (mine didn't). Just add the following to a bat and run it:

使上述代码发挥作用;您需要确保以下目录存在(我的没有)。只需将以下内容添加到 bat 并运行它:

##代码##

It's just that GP needs those directories to exist for:

只是 GP 需要这些目录存在:

##代码##

to function properly.

正常运行。