powershell 如何在分配给右侧按钮的“删除防火墙规则”命令之后分配“操作=允许”命令?

ycl3bljg  于 2023-08-05  发布在  Shell
关注(0)|答案(1)|浏览(85)

有一个Context Menu,我已经分配给鼠标右键为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"

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\\\\\\\"\"'\""


Powershell在我运行代码时打开;但是它在打开之前跳过了Administrator Permission (UAC)提示符。
我不太确定但我认为在代码的某个地方,这些符号中的一个或多个"\'丢失了或多个。
我将感谢在这个问题上的任何帮助或指导。提前感谢您的关注和兴趣。

nuypyhwy

nuypyhwy1#

.reg文件值中的语法注意事项以及对powershell.exe(Windows PowerShell CLI)的调用将在this answer的底部部分针对您的上一个问题进行讨论。
要使此处显示的值适应您的新用例,请执行以下操作:

  • 在问题第一个代码片段中较大的命令行中嵌入的现有功能调用上对额外的netsh调用进行建模。
  • 在隔离中,这为您提供:
netsh advfirewall firewall add rule name=$([System.IO.Path]::GetFileNameWithoutExtension(\\\\\\\"%1\\\\\\\")) dir=out action=allow program=\\\\\\\"%1\\\\\\\"

字符串

  • 现在使用;,PowerShell的语句分隔符,将此调用直接附加到正在运行的调用,这意味着它 * 正好位于关闭的'* 内部(即在'...'字符串的结束'之前,该字符串包含Start-Process调用的位置隐含的-ArgumentList值):
@="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\\\\\\\"'"

相关问题