Created
December 22, 2016 19:12
-
-
Save Stephanevg/a951872bd13d91c0eefad7ad52994f47 to your computer and use it in GitHub Desktop.
Get the current firewall rules. This function is a wrapper around NETSH. It has been written to address the lack of cmdlet on Windows 2008R2 and previous versions. It should be used ONLY for systems prior to 2012.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Get-NetshFireWallrule { | |
Param( | |
[String]$RuleName | |
) | |
if ($RuleName){ | |
$Rules = netsh advfirewall firewall show rule name="$ruleName" | |
}else{ | |
$Rules = netsh advfirewall firewall show rule name="all" | |
} | |
$return = @() | |
$HAsh = [Ordered]@{} | |
foreach ($Rule in $Rules){ | |
if ($Rule -match '^Rule Name:\s+(?<RuleName>.+$)'){ | |
$Hash.RuleName = $Matches.RuleName | |
}Else{ | |
if ($Rule -notmatch "----------------------------------------------------------------------"){ | |
switch -Regex ($Rule){ | |
'^Rule Name:\s+(?<RuleName>.*$)'{$Hash.RuleName = $MAtches.RuleName;Break} | |
'^Enabled:\s+(?<Enabled>.*$)'{$Hash.Enabled = $MAtches.Enabled;Break} | |
'^Direction:\s+(?<Direction>.*$)'{$Hash.Direction = $MAtches.Direction;Break} | |
'^Profiles:\s+(?<Profiles>.*$)'{$Hash.Profiles = $MAtches.Profiles;Break} | |
'^Grouping:\s+(?<Grouping>.*$)'{$Hash.Grouping = $MAtches.Grouping;Break} | |
'^LocalIP:\s+(?<LocalIP>.*$)'{$Hash.LocalIP = $MAtches.LocalIP;Break} | |
'^RemoteIP:\s+(?<RemoteIP>.*$)'{$Hash.RemoteIP = $MAtches.RemoteIP;Break} | |
'^Protocol:\s+(?<Protocol>.*$)'{$Hash.Protocol = $MAtches.Protocol;Break} | |
'^LocalPort:\s+(?<LocalPort>.*$)'{$Hash.LocalPort = $MAtches.LocalPort;Break} | |
'^RemotePort:\s+(?<RemotePort>.*$)'{$Hash.RemotePort = $MAtches.RemotePort;Break} | |
'^Edge traversal:\s+(?<Edge_traversal>.*$)'{$Hash.Edge_traversal = $MAtches.Edge_traversal;$obj = New-Object psobject -Property $Hash;$return += $obj;Break} | |
default {Break} | |
} | |
} | |
} | |
} | |
return $return | |
} | |
$AllRules = Get-NetshFireWallrule | |
$AllRules |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment