Goal
I need to add
Block Firewall Context Menu
For.exe
Files.
Sources
Block .EXE in Windows Firewall with context menu (Modified for my use)
Windows 10 | regedit | exefile shell command (firewall add rule) - not running (No answers there)
Code
Reg Add "HKEY_CLASSES_ROOT\exefile\Shell\Firewall\Command" /T Reg_SZ /D "Elevate.exe CMD.exe /Q /C For %%%%A In (\"%%1\") Do Netsh Advfirewall Firewall Add Rule Name=\"%%~nA\" Dir=Out Action=Block Program=\"%%1\""
This code works perfectly fine when using it with filepath-that-doesnt-contain-space
If filepath containing spaces
it will simply throw out this error. To debug, I added & pause
to the end of the previous code
Output
A specified value is not valid.
Usage: add rule name=<string>
dir=in|out
..
Press any key to continue. . .
Twice
A specified value is not valid.
Usage: add rule name=<string>
dir=in|out
..
Press any key to continue. . .
I am not sure why it ran the code twice. One theory I could come up with is that maybe it is trying add the folder with spaces in path as filename
and also the filename
to netsh
?
While debugging using Echo
I found out that it doesn't have ""
inside the Netsh Rule Name=""
and Program=""
which i think it requires if the path or filename has spaces
in it. I believe the "Qoutes"
that are missing are causing this issue probably.
I have also tried escaping and adding double quotes. Still doesn't work.
Reg Add "HKEY_CLASSES_ROOT\exefile\Shell\Firewall\Command" /T Reg_SZ /D "Elevate.exe CMD.exe /Q /C For %%%%A In (\"\"%%1\"\") Do Netsh Advfirewall Firewall Add Rule Name=\"\"%%~nA\"\" Dir=Out Action=Block Program=\"\"%%1\"\""
I hope we can figure this out together as a team. I will edit and update the OP for more clarification or updates, if needed. Thank you to @micheal_heath for answering.