如何通过右键单击上下文菜单执行 Windows 注册表中的两个命令

如何通过右键单击上下文菜单执行 Windows 注册表中的两个命令

我正在尝试通过右键单击上下文菜单来控制 Windows 更新。我有注册表项,可以添加和/或删除注册表中的值来实现此目的。我可以成功执行单个命令。

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell\Windows Update]
"SubCommands"="Pause updates;Resume updates;Show or hide updates;Open Windows update"
"icon"="wuapi.dll,-0"
"Position"="Bottom"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Pause updates]

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Pause updates\command]
@="reg import pausewu.reg" 

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Resume updates]

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Resume updates\command]
@="reg import resumewu.reg" 

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Show or hide updates]

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Show or hide updates\command]
@="cmd /c wushowhide.diagcab"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Open Windows update]

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Open Windows update\command]
@="cmd /c start ms-settings:windowsupdate"

我需要同时执行两个命令,但是当我添加第二个命令时,两个命令都不会运行。

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell\Windows Update]
"SubCommands"="Pause updates;Resume updates;Show or hide updates;Open Windows update"
"icon"="wuapi.dll,-0"
"Position"="Bottom"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Pause updates]

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Pause updates\command]
@="reg import pausewu.reg" && echo "cmd /c start ms-settings:windowsupdate"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Resume updates]

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Resume updates\command]
@="reg import resumewu.reg" && echo "cmd /c start ms-settings:windowsupdate"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Show or hide updates]

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Show or hide updates\command]
@="reg import resumewu.reg" && echo "cmd /c wushowhide.diagcab"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Open Windows update]

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Open Windows update\command]
@="cmd /c start ms-settings:windowsupdate"

答案1

从技术上来说这是可行的,但可能不是最好的方法。

如何

为了运行多个命令,它们必须是“单行”命令。命令序列必须能够在命令提示符中在一行中运行。这意味着它们需要包含在"后的引号 ( )中@=

测试这一点的一个好方法是复制所有内容并将"其粘贴到运行对话框中。

这将起作用:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Pause updates\command]
@="cmd /c reg import pausewu.reg && start ms-settings:windowsupdate"

笔记:命令中使用的任何引号或反斜杠都需要转义(\", \\)。

更好的方法

更好的选择是创建批处理文件并运行它们。创建一个pause.bat包含命令的文件(例如:):

@ECHO off
reg import pausewu.reg
start ms-settings:windowsupdate

.reg然后在您的文件中使用以下内容:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Pause updates\command]
@="cmd /c \"C:\\your\\path\\pause.bat\""

笔记:再次强调,命令中使用的任何引号或反斜杠都需要转义(\", \\)。

为什么更好

这种方法更好,原因如下:

  1. 您只需运行.reg一次文件,即可根据需要修改批处理文件。手动编辑注册表的次数越少越好。
  2. 如果您想添加更多命令或需要任何奇特功能的命令,这是唯一的方法。
  3. 批处理文件中的错误不会破坏注册表。注册表中的错误可能会带来巨大的麻烦。
  4. 如果您正在测试某些东西,您可以轻松备份批处理文件。

答案2

我打算从注册表执行 2 条命令。这要简单得多,而且应该足够安全,不会破坏注册表。我还添加了执行程序进入公式以隐藏命令窗口。

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell\Windows Update]
"SubCommands"="Pause updates;Resume updates;Show or hide updates;Launch Windows Update"
"icon"="shell32.dll,-47"
"Position"="Top"
"SeparatorBefore"=""
"SeparatorAfter"=""

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Pause updates]
"Icon"="%windir%\\System32\\psr.exe,2"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Pause updates\command]
@="NSudo.exe -U:T -ShowWindowMode:Hide cmd /c reg import PauseUpdates.reg && start ms-settings:windowsupdate"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Resume updates]
"Icon"="%windir%\\System32\\shell32.dll,238"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Resume updates\command]
@="NSudo.exe -U:T -ShowWindowMode:Hide cmd /c reg import ResumeUpdates.reg && start ms-settings:windowsupdate"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Show or hide updates]
"Icon"="%windir%\\System32\\shell32.dll,239"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Show or hide updates\command]
@="NSudo.exe -U:T -ShowWindowMode:Hide cmd /c reg import ResumeUpdates.reg && wushowhide.diagcab"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Launch Windows Update]
"Icon"="%windir%\\System32\\setupcln.dll,0"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Launch Windows Update\command]
@="NSudo.exe -U:T -ShowWindowMode:Hide cmd /c start ms-settings:windowsupdate"

相关内容