Posted On December 24, 2019

PowerShell: Setting Windows Firewall Rules

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Setting Windows Firewall Rules
# Set variables for HTTP
$protocolName="HTTP"
$protocol="TCP"
$portNumbers='80','443'
$direction="Inbound"
$scopes='Domain', 'Private'

# Add Firewall Rule
New-NetFirewallRule -DisplayName "$protocolName-$direction" -Profile @($scopes) -Direction $direction -Action Allow -Protocol $protocol -LocalPort @($portNumbers)

# Set variables for VNC
$protocolName="VNC"
$protocol="TCP"
$portNumbers='5900'
$direction="Inbound"
$scopes='Domain', 'Private'

# Add Firewall Rule
New-NetFirewallRule -DisplayName "$protocolName-$direction" -Profile @($scopes) -Direction $direction -Action Allow -Protocol $protocol -LocalPort @($portNumbers)

<# Errors on Windows with PowerShell versions less than 3.0
The term 'New-NetFirewallRule' is not recognized as the name of a cmdlet, function, script file, or operable program. C
heck the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:20
+ New-NetFirewallRule <<<< -DisplayName "$protocolName-$direction" -Profile @($scopes) -Direction $direction -Action A
llow -Protocol $protocol -LocalPort @($portNumbers)
+ CategoryInfo : ObjectNotFound: (New-NetFirewallRule:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
#>

$portNumbers|%{netsh advfirewall firewall add rule name='$protocolName-$direction-$_' dir=in action=allow protocol=$protocol localport=$_;}

Leave a Reply

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

Related Post

How To Format HTML Content For Email

Major email service providers such as Google Gmail and Microsoft Office 365 have been supporting…

Basic CSS: Use RGB to Mix Colors

<style>.red-text {color: #000000;}.orchid-text {color: #000000;}.sienna-text {color: #000000;}.blue-text {color: #000000;}</style><h1 class="red-text">I am red!</h1><h1 class="orchid-text">I am orchid!</h1><h1…

Quick & Useful Snippet to Set SSL TLS Protocol of PowerShell

$requiredTls='Tls12' $availableSslProtocols=[enum]::getnames([net.securityprotocoltype]) if([Net.ServicePointManager]::SecurityProtocol -notin $requiredTls -and $requiredTls -in $availableSslProtocols){ [Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::$requiredTls }