There is a Context Menu that I have assigned to the right mouse button as Windows Firewall. And I am using the following code to delete a security rule.

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

And likewise I want it to re-add this code as "action=allow" with a single script after deleting.

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

Other commands I tried

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 opens when I run the code; but it skips the Administrator Permission (UAC) prompt before it opens.

I'm not sure exactly; but I think somewhere in the code one or more of these signs "\' are missing or more.

I would appreciate any help or guidance on this issue. Thank you for your attention and interest in advance.

1

There are 1 answers

0
mklement0 On BEST ANSWER

The syntax considerations at play in your .reg file value in combination with a call to powershell.exe, the Windows PowerShell CLI, are discussed in the bottom section of this answer to your previous question.

To adapt the value shown there to your new use case:

  • Model your additional netsh call on the existing, functioning one that is embedded in the larger command line in the first snippet in your question.

  • In isolation, this gives you:

netsh advfirewall firewall add rule name=$([System.IO.Path]::GetFileNameWithoutExtension(\\\\\\\"%1\\\\\\\")) dir=out action=allow program=\\\\\\\"%1\\\\\\\"
  • Now use ;, PowerShell's statement separator, to append this call directly to the functioning one, which means that it goes just inside the closing ' (i.e. before the closing ' of the '...' string that encloses the positionally implied -ArgumentList value of the Start-Process call):
@="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\\\\\\\"'"