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

PowerShell: Execute as System Elevated Session with a Domain Administrator account

Part 1: Relaunch script in the context of Elevated System privileges: ############# Ensure that Program…

PowerShell: List All Hyper-V Snapshots of All VMs in All Clusters in Domain

# listHyperVSnapshots.ps1 $clusterName='*' function listAllHyperVSnapshots([string[]]$clusterName){ function includeRSAT{ $ErrorActionPreference='stop' [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 #$rsatWindows7x32='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x86-RefreshPkg.msu' $rsatWindows7x64='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x64-RefreshPkg.msu' $rsatWindows81='https://download.microsoft.com/download/1/8/E/18EA4843-C596-4542-9236-DE46F780806E/Windows8.1-KB2693643-x64.msu' $rsat1709…

How To Use Command Line to Configure iDrac Settings

Step 1: Install RacADM $computerlist=@' SERVER1 SERVER2 '@ $computernames=@($computerlist -split "`n")|%{$_.Trim()} $fileURL="https://dl.dell.com/FOLDER08543783M/1/DellEMC-iDRACTools-Web-WINX64-10.3.0.0-4945.exe" $expectedExecutable='racadm.exe' $expectedInstallPath='C:\Program Files\Dell\SysMgt\iDRACTools\racadm'…