右键菜单Windows防火墙脚本:删除后添加允许规则的UAC权限问题求助
右键菜单实现删除并重新添加防火墙出站规则的问题
已为鼠标右键菜单添加名为Windows Firewall的上下文菜单,当前可用的注册表脚本可实现删除出站防火墙规则:
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\*\shell\Windows Firewall] @="" "MUIVerb"="Windows Firewall" "icon"="%SystemRoot%\\system32\\FirewallControlPanel.dll,0" "subcommands"="" [HKEY_CLASSES_ROOT\*\shell\Windows Firewall\shell\Delete Rule (Outgoing)] @="" "MUIVerb"="Delete Rule (Outgoing)" "Icon"="%SystemRoot%\\system32\\FirewallControlPanel.dll,2" [HKEY_CLASSES_ROOT\*\shell\Windows Firewall\shell\Delete Rule (Outgoing)\command] @="powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass -NoProfile -Command Start-Process -Verb RunAs powershell.exe '-NoExit -ExecutionPolicy Bypass -NoProfile -Command netsh advfirewall firewall delete rule name=$([IO.Path]::GetFileNameWithoutExtension(\\\\\\\"%1\\\\\\\")) program=\\\\\\\"%1\\\\\\\" dir=out'"
需要编写单个脚本,在删除规则后重新添加action=allow的出站规则,尝试了以下几个注册表脚本,但运行时PowerShell会打开却跳过UAC权限提示,推测是引号转义错误导致:
尝试的合并规则脚本(预期版本)
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\*\shell\Windows Firewall] @="" "MUIVerb"="Windows Firewall" "icon"="%SystemRoot%\\system32\\FirewallControlPanel.dll,0" "subcommands"="" [HKEY_CLASSES_ROOT\*\shell\Windows Firewall\shell\Allow (Out)] @="" "MUIVerb"="Allow (Out)" "Icon"="%SystemRoot%\\system32\\FirewallControlPanel.dll,2" [HKEY_CLASSES_ROOT\*\shell\Windows Firewall\shell\Allow (Out)\command] @="powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass -NoProfile -Command Start-Process -Verb RunAs powershell.exe '-NoExit -ExecutionPolicy Bypass -NoProfile -Command netsh advfirewall firewall delete rule name=$([IO.Path]::GetFileNameWithoutExtension(\\\\\\\"%1\\\\\\\")) program=\\\\\\\"%1\\\\\\\" dir=out; netsh advfirewall firewall add rule name='\\"'([System.IO.Path]::GetFileNameWithoutExtension('%1'))'\\"' dir=out action=allow program='\\"'%1'\\"'}'%1'`\\\"\\\"\""
另外两个尝试的命令
@="powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass -NoProfile -Command Start-Process -Verb RunAs powershell.exe '-NoExit -ExecutionPolicy Bypass -NoProfile -Command netsh advfirewall firewall delete rule name=$([IO.Path]::GetFileNameWithoutExtension(\\"%1\\")) program=\\"%1\\" dir=out; netsh advfirewall firewall add rule name='\\"'([System.IO.Path]::GetFileNameWithoutExtension('%1'))'\\"' dir=out action=allow program='\\"'%1'\\"'}' \\"%1\\""
@="powershell.exe -NoProfile -ExecutionPolicy Bypass -Command \\"Start-Process -Verb RunAs powershell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -NoExit -Command \\"netsh advfirewall firewall delete rule name=$([IO.Path]::GetFileNameWithoutExtension(\\\\\\\"%1\\\\\\\")) program=\\\\\\\"%1\\\\\\\" dir=out; netsh advfirewall firewall add rule name=\\\\\\\"$([System.IO.Path]::GetFileNameWithoutExtension('%1'))\\\\\\\" dir=out action=allow program=\\\\\\\"%1\\\\\\\"\\"'\\""
解决方案:修正引号转义的注册表脚本
问题根源在于多层嵌套命令的引号转义混乱,导致Start-Process -Verb RunAs无法正确触发UAC提示。以下是修正后的完整注册表脚本:
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\*\shell\Windows Firewall] @="" "MUIVerb"="Windows Firewall" "icon"="%SystemRoot%\\system32\\FirewallControlPanel.dll,0" "subcommands"="" [HKEY_CLASSES_ROOT\*\shell\Windows Firewall\shell\Allow (Out)] @="" "MUIVerb"="Allow (Out)" "Icon"="%SystemRoot%\\system32\\FirewallControlPanel.dll,2" [HKEY_CLASSES_ROOT\*\shell\Windows Firewall\shell\Allow (Out)\command] @="powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass -NoProfile -Command Start-Process -Verb RunAs powershell.exe '-NoExit -ExecutionPolicy Bypass -NoProfile -Command \"`$filePath = [IO.Path]::GetFullPath('%1'); `$ruleName = [IO.Path]::GetFileNameWithoutExtension(`$filePath); netsh advfirewall firewall delete rule name=`"`$ruleName`" program=`"`$filePath`" dir=out; netsh advfirewall firewall add rule name=`"`$ruleName`" dir=out action=allow program=`"`$filePath`"\"'"
关键修正点:
- 简化参数传递:先将
%1解析为完整文件路径和规则名变量,避免多次解析带来的转义错误 - 正确的引号转义:
- 注册表字符串中的双引号用
\"转义 - PowerShell内部的双引号用
"`"转义(在单引号包裹的命令块中,反引号是PowerShell的转义符)
- 注册表字符串中的双引号用
- 确保UAC触发:
Start-Process -Verb RunAs的参数格式正确,保证系统弹出管理员权限提示 - 路径兼容性:使用
[IO.Path]::GetFullPath处理%1,避免路径含空格或特殊字符时出现解析错误
内容的提问来源于stack exchange,提问作者Albie
相关产品推荐
相关产品推荐

