VS Add inbound firewall rule programatically

Claire DOVE 1 Reputation point
2022-08-12T13:31:06.023+00:00

I copied the code provided for the question 'Add firewall rule programatically' here: add-firewall-rule-programaticallyand have got it to function without errors. Unfortunately, being rather dense, I don't understand what it does. Where is the executable that needs to be authorized referenced (if it is referenced)?

What I want to do is create a new rule to allow my program to write and read files on another computer on a LAN. At the moment the user has to go all through the complicated process of creating a new rule via Windows Defender.

Writing the rule programatically would be part of the existing installation program which already requires administrator rights. Is this possible?

Can anyone help please?

Windows 10 Security
Windows 10 Security
Windows 10: A Microsoft operating system that runs on personal computers and tablets.Security: The precautions taken to guard against crime, attack, sabotage, espionage, or another threat.
2,840 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Claire DOVE 1 Reputation point
    2022-08-15T13:31:16.82+00:00

    I have found another way to do this. I create a bat file that uses netsh:

    netsh advfirewall firewall add rule name="My New Rule" dir=in action=allow program="C:\Program Files (x86)\My Program\My Program.exe" profile=private enable=yes  
    

    and I run it from my VS app which requires administrator rights.

    0 comments No comments

  2. Limitless Technology 39,511 Reputation points
    2022-08-18T07:36:20.72+00:00

    Hello,

    Initially, those scripts where no app exe is specified corresponds to rules that affect only the port. For example, do not allow communication on port, or allow communication.

    For more complex rules, such as specific Exe files you can use the New-NetFirewallRule cmdlet, like explained in the next article, in specific the Example 7, to associate rule with specific app.

    https://learn.microsoft.com/en-us/powershell/module/netsecurity/new-netfirewallrule?view=windowsserver2022-ps

    ------------------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments