Firewall Rules using Powershell
I had to test something out where I needed to make sure the right ports were not being blocked by the windows firewall. In this example, I use port 8080. I used powershell to forcefully open any outbound and inbound ports. Outbound is usually open by default, but I wanted to double ensure it.
New-NetFirewallRule -DisplayName "Allow outbound TCP port 25" -Direction outbound -LocalPort 8080 -Protocol TCP -Action Allow
New-NetFirewallRule -DisplayName "Allow INBOUND TCP port 25" -Direction inbound -LocalPort 8080 -Protocol TCP -Action Allow
New-NetFirewallRule -DisplayName "Block Outbound Port 80" -Direction Outbound -LocalPort 80 -Protocol TCP -Action Block
Test it out – Use tcp.connect to see if you can get to the server on the port
$tcp = New-Object System.Net.Sockets.TcpClient $tcp.Connect($myserver,"8080") $tcp.Connected
You may need this if you are trying to use an SSL/TLS connection to test out your script/program
System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
You can also use Test-NetConnection
Test-NetConnection -ComputerName www.contoso.com –Port 8080
Leave a Reply