{"id":17861,"date":"2010-07-04T00:01:00","date_gmt":"2010-07-04T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/07\/04\/hey-scripting-guy-weekend-scripter-improving-yesterdays-windows-firewall-script\/"},"modified":"2010-07-04T00:01:00","modified_gmt":"2010-07-04T00:01:00","slug":"hey-scripting-guy-weekend-scripter-improving-yesterdays-windows-firewall-script","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-weekend-scripter-improving-yesterdays-windows-firewall-script\/","title":{"rendered":"Hey, Scripting Guy! Weekend Scripter: Improving Yesterday&#8217;s Windows Firewall Script"},"content":{"rendered":"<p>&nbsp;\nMicrosoft Scripting Guy Ed Wilson here. The Smokey Mountains are about 10 degrees cooler than <a href=\"http:\/\/en.wikipedia.org\/wiki\/Charlotte,_North_Carolina\">Charlotte<\/a>, <a href=\"http:\/\/en.wikipedia.org\/wiki\/North_Carolina\">North Carolina<\/a>, is. The problem is that it was nearly 100 degrees Fahrenheit in Charlotte. Anyway, getting to spend some time with my old high school friend has been fun, even if he does not know Windows PowerShell from a seashell. Oh well, he still has good taste in music.\nOn the drive up into the mountains, the Scripting Wife was commenting on the beautiful trees, creeks, and occasional wild animal; I, on the other hand, was thinking of ways to improve my firewall script.\nWhen we arrived at the cabin John had not yet arrived, so I got out my laptop and got to work on my improved firewall script. The Get-EnabledFireWallRules.ps1 script uses an enumeration to parse the protocol instead of displaying a protocol number like <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/07\/03\/hey-scripting-guy-weekend-scripter-how-to-retrieve-enabled-windows-firewall-rules.aspx\">the script from yesterday<\/a> did. In addition, the <strong>Format-Table<\/strong> cmdlet uses a hash table to perform the lookup for the enumeration value as well as to interpret the direction of the rule. The complete Get-EnabledFireWallRules.ps1 script is shown here.\n<strong>Get-EnabledFireWallRules.ps1<\/strong>\n<span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">New-ProtocolEnum<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #2b91af\">$enum<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8221; <br \/>namespace myspace&nbsp; <br \/>{&nbsp; <br \/>public enum protocol <br \/>{&nbsp; <br \/>HOPOPT = 0, ICMPv4 = 1, IGMP = 2, TCP = 6, UDP = 17, IPv6 = 41, <br \/>IPv6Route = 43, IPv6Frag = 44 ,GRE = 47, ICMPv6 = 58, IPv6NoNxt = 59, <br \/>IPv6Opts = 60, VRRP = 112, PGM = 113, L2TP = 115 <br \/>} <br \/>} <br \/>&#8221; <br \/><\/span><span style=\"color: #000000\">Add-Type<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-TypeDefinition<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$enum<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Language<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">CSharpVersion3<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">New-ProtocolEnum<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Test-LoadedEnum<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Param([string]<\/span><span style=\"color: #2b91af\">$enum<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Try<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">[reflection.assembly]::GetAssembly([<\/span><span style=\"color: #0000ff\">type<\/span><span style=\"color: #000000\">]<\/span><span style=\"color: #2b91af\">$enum<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">out-null<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">New-Object<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">psobject<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Property<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">`<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">@{<\/span><span style=\"color: #808080\"> &#8220;Name&#8221; <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$enum<\/span><span style=\"color: #000000\">.tostring()<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">;<\/span><span style=\"color: #808080\"> &#8220;Loaded&#8221; <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">[bool]<\/span><span style=\"color: #2b91af\">$true<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Catch<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">[system.exception]<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">New-Object<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">psobject<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Property<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">`<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">@{<\/span><span style=\"color: #808080\"> &#8220;Name&#8221; <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$enum<\/span><span style=\"color: #000000\">.tostring()<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">;<\/span><span style=\"color: #808080\"> &#8220;Loaded&#8221; <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">[bool]<\/span><span style=\"color: #2b91af\">$false<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Test-LoadedEnum<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">import-Enum<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Param(<\/span><span style=\"color: #2b91af\">$rtn<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">If<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #2b91af\">$rtn<\/span><span style=\"color: #000000\">.Loaded)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> &#8220;$($rtn.name) is loaded&#8221; <\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">else<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp; <br \/>&#8220;$($rtn.name) NOT loaded. Loading &#8230;&#8221;&nbsp; <br \/><\/span><span style=\"color: #000000\">New-ProtocolEnum<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">import-enum<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-FireWallRules<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #2b91af\">$fw<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">New-Object<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-ComObject<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">hnetcfg.fwpolicy2<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #2b91af\">$currentProfile<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$fw<\/span><span style=\"color: #000000\">.CurrentProfileTypes<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #2b91af\">$fw<\/span><span style=\"color: #000000\">.Rules<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">Where-Object<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #2b91af\">$_<\/span><span style=\"color: #000000\">.enabled<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">-AND<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$_<\/span><span style=\"color: #000000\">.Profiles<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$currentProfile<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">Sort-Object<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Property<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">direction<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">Format-Table<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Property<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">localports,<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">@{LABEL<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&#8220;Protocol&#8221;<\/span><span style=\"color: #0000ff\">;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">EXPRESSION<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">{[enum]::parse([<\/span><span style=\"color: #0000ff\">type<\/span><span style=\"color: #000000\">]<\/span><span style=\"color: #808080\">&#8220;myspace.protocol&#8221;<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #2b91af\">$_<\/span><span style=\"color: #000000\">.protocol)}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">},<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">@{LABEL<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&#8220;Direction&#8221;<\/span><span style=\"color: #0000ff\">;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">EXPRESSION<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">SWITCH(<\/span><span style=\"color: #2b91af\">$_<\/span><span style=\"color: #000000\">.direction){<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&#8220;in&#8221;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&#8220;out&#8221;<\/span><span style=\"color: #000000\">}}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">},<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-autosize<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-FirewallRules<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">***<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">ENTRY<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">POINT<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">TO<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">SCRIPT<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">***<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">import-enum<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-rtn<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">(test-LoadedEnum<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-enum<\/span><span style=\"color: #808080\"> &#8220;myspace.protocol&#8221;<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Get-FireWallRules<\/span>\nThe <strong>New-ProtocolEnum<\/strong> function is used to create the <strong>myspace.protocol<\/strong> enumeration. The creation of new enums was discussed in a <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/06\/06\/hey-scripting-guy-weekend-scripter-the-fruity-bouquet-of-windows-powershell-enumerations.aspx\">Weekend Scripter article<\/a> a couple of weeks ago. The mapping of the protocol numbers to names is discussed in <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/cc770685(WS.10).aspx\">a TechNet article in the Library.<\/a> The <strong>New-ProtocolEnum<\/strong> function is shown here:\n&nbsp;<\/p>\n<div class=\"code\"><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">New-ProtocolEnum<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #2b91af\">$enum<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8221; <br \/>namespace myspace&nbsp; <br \/>{&nbsp; <br \/>public enum protocol <br \/>{&nbsp; <br \/>HOPOPT = 0, ICMPv4 = 1, IGMP = 2, TCP = 6, UDP = 17, IPv6 = 41, <br \/>IPv6Route = 43, IPv6Frag = 44 ,GRE = 47, ICMPv6 = 58, IPv6NoNxt = 59, <br \/>IPv6Opts = 60, VRRP = 112, PGM = 113, L2TP = 115 <br \/>} <br \/>} <br \/>&#8221; <br \/><\/span>  <\/p>\n<div class=\"code\"><span style=\"color: #000000\">Add-Type<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-TypeDefinition<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$enum<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Language<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">CSharpVersion3<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">New-ProtocolEnum<\/span><span style=\"color: #808080\">&nbsp;<\/span> <\/div>\n<\/p><\/div>\n<p>The <strong>Test-LoadedEnum<\/strong> function is lifted from a recent <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/06\/13\/hey-scripting-guy-weekend-scripter-testing-for-a-loaded-enumeration.aspx\">Weekend Scripter article<\/a> that I wrote while in Hilton Head, <a href=\"http:\/\/en.wikipedia.org\/wiki\/South_carolina\">South Carolina<\/a>. See that article for a discussion of how it works. The complete <strong>Test-LoadedEnum<\/strong> function is shown here:\n&nbsp;<\/p>\n<div class=\"code\"><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Test-LoadedEnum<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Param([string]<\/span><span style=\"color: #2b91af\">$enum<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Try<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">[reflection.assembly]::GetAssembly([<\/span><span style=\"color: #0000ff\">type<\/span><span style=\"color: #000000\">]<\/span><span style=\"color: #2b91af\">$enum<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">out-null<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">New-Object<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">psobject<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Property<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">`<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">@{<\/span><span style=\"color: #808080\"> &#8220;Name&#8221; <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$enum<\/span><span style=\"color: #000000\">.tostring()<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">;<\/span><span style=\"color: #808080\"> &#8220;Loaded&#8221; <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">[bool]<\/span><span style=\"color: #2b91af\">$true<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Catch<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">[system.exception]<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">New-Object<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">psobject<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Property<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">`<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">@{<\/span><span style=\"color: #808080\"> &#8220;Name&#8221; <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$enum<\/span><span style=\"color: #000000\">.tostring()<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">;<\/span><span style=\"color: #808080\"> &#8220;Loaded&#8221; <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">[bool]<\/span><span style=\"color: #2b91af\">$false<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Test-LoadedEnum<\/span> <\/div>\n<p>I decided to write a function to determine if I need to load the enumeration or not. This removed some of the logic from the beginning of the script. In addition, it provides a completely re-usable function. The <strong>Import-Enum<\/strong> function is shown here:\n&nbsp;<\/p>\n<div class=\"code\"><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">import-Enum<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Param(<\/span><span style=\"color: #2b91af\">$rtn<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">If<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #2b91af\">$rtn<\/span><span style=\"color: #000000\">.Loaded)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> &#8220;$($rtn.name) is loaded&#8221; <\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">else<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp; <br \/>&#8220;$($rtn.name) NOT loaded. Loading &#8230;&#8221;&nbsp; <br \/><\/span><span style=\"color: #000000\">New-ProtocolEnum<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">import-enum<\/span> <\/div>\n<p>The <strong>Get-FireWallRules<\/strong> function is much like the script from yesterday&rsquo;s post. The difference is that it tests for the current profile and uses two hash tables to create customized table entries. The one that does the protocol lookup from the <strong>myspace.protocol<\/strong> enum is shown here:\n<span style=\"color: #000000\">@{LABEL<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&#8220;Protocol&#8221;<\/span><span style=\"color: #0000ff\">;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">EXPRESSION<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">{[enum]::parse([<\/span><span style=\"color: #0000ff\">type<\/span><span style=\"color: #000000\">]<\/span><span style=\"color: #808080\">&#8220;myspace.protocol&#8221;<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #2b91af\">$_<\/span><span style=\"color: #000000\">.protocol)}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span>\nThe key was using the <strong>system.enum<\/strong> static parse method, the type constraint for the <strong>myspace.protocol<\/strong> enum, and passing the protocol number. This technique was discussed in the working with <a href=\"http:\/\/blogs.technet.comhttps:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-weekend-scripter-enumerations-and-values\/\">enumerations and values Weekend Scripter article<\/a>. The complete <strong>Get-FirewallRules<\/strong> function is shown here:\n&nbsp;<\/p>\n<div class=\"code\"><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-FireWallRules<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #2b91af\">$fw<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">New-Object<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-ComObject<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">hnetcfg.fwpolicy2<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #2b91af\">$currentProfile<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$fw<\/span><span style=\"color: #000000\">.CurrentProfileTypes<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #2b91af\">$fw<\/span><span style=\"color: #000000\">.Rules<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">Where-Object<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #2b91af\">$_<\/span><span style=\"color: #000000\">.enabled<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">-AND<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$_<\/span><span style=\"color: #000000\">.Profiles<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$currentProfile<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">Sort-Object<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Property<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">direction<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">Format-Table<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Property<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">localports,<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">@{LABEL<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&#8220;Protocol&#8221;<\/span><span style=\"color: #0000ff\">;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">EXPRESSION<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">{[enum]::parse([<\/span><span style=\"color: #0000ff\">type<\/span><span style=\"color: #000000\">]<\/span><span style=\"color: #808080\">&#8220;myspace.protocol&#8221;<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #2b91af\">$_<\/span><span style=\"color: #000000\">.protocol)}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">},<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">@{LABEL<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&#8220;Direction&#8221;<\/span><span style=\"color: #0000ff\">;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">EXPRESSION<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">SWITCH(<\/span><span style=\"color: #2b91af\">$_<\/span><span style=\"color: #000000\">.direction){<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&#8220;in&#8221;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&#8220;out&#8221;<\/span><span style=\"color: #000000\">}}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">},<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-autosize<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-FirewallRules<\/span> <\/div>\n<p>The entry point to the script calls the <strong>Import-Enum<\/strong> function while passing the result from the <strong>Test-LoadedEnum<\/strong> function. Next, it calls the <strong>Get-FirewallRules<\/strong> function. This is shown here:\n&nbsp;<\/p>\n<div class=\"code\"><span style=\"color: #000000\">import-enum<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-rtn<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">(test-LoadedEnum<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-enum<\/span><span style=\"color: #808080\"> &#8220;myspace.protocol&#8221;<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Get-FireWallRules<\/span> <\/div>\n<p>When the Get-EnabledFireWallRules.ps1 script runs, the output is displayed that is shown in the following image.\n<a href=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/july\/hey0704\/wes-07-04-10-01.jpg\"><img decoding=\"async\" alt=\"Image of output displayed when script is run\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/july\/hey0704\/wes-07-04-10-01.jpg\"><\/a>\nThe cool thing about all this is that it provides an excellent example for using our recent discussion of <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/enum\/\">working with enums<\/a>. Well, I think I hear the &ldquo;old folks&rdquo; stirring inside the cabin. I believe I overheard Becky and the Scripting Wife talking about making <a href=\"http:\/\/en.wikipedia.org\/wiki\/Pancake\">pancakes<\/a>, so perhaps I will go inside and see if they need a taste tester. I hope you have a great weekend.\nIf you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n&nbsp;<\/p>\n<p><strong>Ed Wilson and Craig Liebendorfer, Script<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Microsoft Scripting Guy Ed Wilson here. The Smokey Mountains are about 10 degrees cooler than Charlotte, North Carolina, is. The problem is that it was nearly 100 degrees Fahrenheit in Charlotte. Anyway, getting to spend some time with my old high school friend has been fun, even if he does not know Windows PowerShell [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[148,161,3,4,63,61,45],"class_list":["post-17861","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-enum","tag-firewall","tag-scripting-guy","tag-scripting-techniques","tag-security","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; Microsoft Scripting Guy Ed Wilson here. The Smokey Mountains are about 10 degrees cooler than Charlotte, North Carolina, is. The problem is that it was nearly 100 degrees Fahrenheit in Charlotte. Anyway, getting to spend some time with my old high school friend has been fun, even if he does not know Windows PowerShell [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17861","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=17861"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17861\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=17861"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=17861"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=17861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}