PowerShell advanced function (cmdlet) to get Windows Firewall rules according specified protocols and ports


I wrote a very simple helper function (you can use it as cmdlet in own module) to simplify work with Get-NetFirewallRule cmdlet.

Examples

Get FW rules from remote server based on multiple protocols and ports

Get-RvNetFirewallRule -Protocol TCP, UDP -LocalPort @(135..138) -CimSession MyRemoteServer |
    Format-Table

Get disabled rule from the local server based on specified protocol and port

Get-RvNetFirewallRule -Protocol TCP -LocalPort 135 -Enabled:$false |
    Format-Table

Code

Function Get-RvNetFirewallRule
{
    <#
    .SYNOPSIS
        Similar to Get-NetFirewallRule with ability to filter using protocols and ports.

    .DESCRIPTION
        Developer
            Developer: Rudolf Vesely, http://rudolfvesely.com/
            Copyright (c) Rudolf Vesely. All rights reserved
            License: Free for private use only

            "RV" are initials of the developer's name Rudolf Vesely and distingue names of Rudolf Vesely's cmdlets from the other cmdlets.

        Description
            Similar to Get-NetFirewallRule with ability to filter using protocols and ports.

        Requirements
            Developed and tested using PowerShell 4.0.

    .PARAMETER Protocol
        For example: @('TCP', 'UDP')

    .PARAMETER LocalPort
        For example: 80 (HTTP) or @(80, 443, 1433) (HTTP, HTTPS, SQL)

    .EXAMPLE
        'EXAMPLE: Get from remote server'
        Get-RvNetFirewallRule -Protocol TCP, UDP -LocalPort @(135..138) -CimSession MyRemoteServer |
            Format-Table

    .EXAMPLE
        'EXAMPLE: Get from local server'
        Get-RvNetFirewallRule -Protocol TCP -LocalPort 135 -Enabled:$false |
            Format-Table

    .INPUTS

    .OUTPUTS
        Microsoft.Management.Infrastructure.CimInstance#root/standardcimv2/MSFT_NetFirewallRule

    .LINK
        https://techstronghold.com/
    #>

    [CmdletBinding(
        DefaultParametersetName = 'Protocol',
        SupportsShouldProcess = $true,
        PositionalBinding = $false,
        HelpURI = 'https://techstronghold.com/',
        ConfirmImpact = 'Medium'
    )]

    Param
    (
        [Parameter(
            Mandatory = $true,
            Position = 0,
            ParameterSetName = 'Protocol'
        )]
        [string[]]$Protocol,

        [Parameter(
            Mandatory = $true,
            Position = 1
        )]
        [int[]]$LocalPort,

        [Parameter(
            Mandatory = $false
        )]
        [ValidateSet(
            'Inbound',
            'Outbound'
        )]
        [string]$Direction = 'Inbound',

        [Parameter(
            Mandatory = $false
        )]
        [bool]$Enabled = $true,

        [Parameter(
            Mandatory = $false
        )]
        [Microsoft.Management.Infrastructure.CimSession[]]$CimSession
    )

    Begin
    {
        # Configurations
        $ErrorActionPreference = 'Stop'
        if ($PSBoundParameters['Debug']) { $DebugPreference = 'Continue' }
        Set-PSDebug -Strict
        Set-StrictMod

        $parametersAndArgumentsCommon = @{}
        if ($CimSession) { $parametersAndArgumentsCommon.Add('CimSession', $CimSession) }
    }

    Process
    {
        Get-NetFirewallPortFilter -Protocol $Protocol @parametersAndArgumentsCommon |
            ForEach-Object -Process { if ($LocalPort -contains $_.LocalPort) { $_ } } |
            Get-NetFirewallRule @parametersAndArgumentsCommon |
            Where-Object -FilterScript { $_.Enabled.ToString() -eq $Enabled.ToString() -and
                $_.Direction.ToString() -eq $Direction }
    }

    End
    {
    }
}

Leave a Reply

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

Active Directory Advanced function AlwaysOn Availability Groups AlwaysOn Failover Cluster Instances Building Cloud Cloud Cluster Cmdlet Database Deployment Design DFS Domain Controller DSC Fabric Failover Clustering File Server Group Policy Hardware Profile Host Hyper-V Installation Library Library Asset Library Server Network Operations Manager Orchestrator PowerShell PowerShell User Group PowerShell Workflow Security Service Manager SQL Server Storage System Center Template Time Time Synchronization Tips Virtual Machine Virtual Machine Manager VM Network VM Template Windows Server 2012 R2