Jump to content

Adding Firewall rule


Recommended Posts

I am trying to add a firewall rule but I think I got the syntax wrong. 

Run(@ComSpec & "/k " & 'netsh advfirewall firewall add rule name=' & '"New"' & 'dir=in action=allow program=' & NoSlash(@ScriptDir) & '"\MyApp.exe"' & 'enable=yes')

Could you please let me know what I missed.

Edited by skon2464
Link to comment
Share on other sites

The syntax issue is likely to do with your lack of spaces between parameters.  Also, you messed up on quoting the app name, not including the path.

-Added space before "dir="

-Added space before "enable="

-Moved double quote from before "MyApp.exe" to right after "program="

Run(@ComSpec & "/k " & 'netsh advfirewall firewall add rule name=' & '"New"' & ' dir=in action=allow program="' & NoSlash(@ScriptDir) & '\MyApp.exe"' & ' enable=yes')
Link to comment
Share on other sites

  • 4 years later...
On 7/22/2013 at 10:59 PM, DW1 said:

The syntax issue is likely to do with your lack of spaces between parameters.  Also, you messed up on quoting the app name, not including the path.

-Added space before "dir="

-Added space before "enable="

-Moved double quote from before "MyApp.exe" to right after "program="

 

Run(@ComSpec & "/k " & 'netsh advfirewall firewall add rule name=' & '"New"' & ' dir=in action=allow program="' & NoSlash(@ScriptDir) & '\MyApp.exe"' & ' enable=yes')

Not working

Link to comment
Share on other sites

  • 6 years later...
On 8/25/2017 at 4:36 PM, naru said:

Not working

Probably because it needs admin privileges.
I therefore run it not with Run but with ShellExecute with runas verb (although I prefer ShellExecuteWait as shown below to know if the rule worked or not).
BTW, you can alternatively use direct firewall API but it means having to run the entire script as admin which is a security risk.

$failed=False
If ShellExecuteWait('netsh', 'advfirewall firewall add rule name="' & $programName & '"' & ' dir=in action=block program="' & $programPath & $programFile & '"', default, "runas") > 0
    $failed=True
ElseIf ShellExecuteWait('netsh', 'advfirewall firewall add rule name="' & $programName & '"' & ' dir=out action=block program="' & $programPath & $programFile & '"', default, "runas") > 0
            $failed=True
EndIf

And to delete a rule:

$failed=False
If ShellExecuteWait('netsh', 'advfirewall firewall delete rule name="' & $programName & '"' & ' program="' & $programPath & $programFile & '"', default, "runas") > 0 then
    $failed = True
EndIf

 

Edited by LWC
Rephrased
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...