Powershell

[Powershell] how to authorize a firewall rule for ping

Click to rate this post!
[Total: 0 Average: 0]

Powershell, how to authorize a firewall rule for ping?



Two solutions are available to us. Either use the “netsh” commands or use Powershell’s NetFirewallRule

First of all: « netsh advfirewall »
Some quick research gave me the protocol corresponding to “Ping”. This is icmpv4 code 8 or otherwise called “Echo Request”

Use a cmd console and paste this code :

netsh advfirewall firewall add rule name="ICMP Allow PING echo request" protocol="icmpv4:8,any" dir=in action=allow

Here, we define some simple option
name : rule name
protocol: protocol name
dir : the direction Inbound or outbound
action : (allow ou block)
localport : (Not used here, but could have specified the name of a port, such as 8080)
program : « c:\programfiles\messenger\msmsgs.exe »


Then, a Cmdlet de Powershell for the firewall NetFirewallRule.
Classically, either Get, Add ou Set.
In my example, this is a default rule under windows 2012 ans 2016.

#1, define our rules to activate

$rule_list= “File and Printer Sharing (Echo Request – ICMPv4-In)”,”File and Printer Sharing (Echo Request – ICMPv4-Out)”


#2 : we create a loop for each rule and we test if it ts activated.
 foreach($rule in $rule_list)
 {
 $status= (Get-NetFirewallRule -DisplayName “$rule“).Enabled
 if ($status -eq “False”) {Set-NetFirewallRule -DisplayName “$rule” -Enabled True
   “$rule has been activated”}
   else {“$rule is activated”}
 }





 

Leave a Reply

Your email address will not be published. Required fields are marked *