Iterating a Collection
The following code example iterates through a collection of AuthorizedApplications displaying their properties.
Option Explicit On Error Resume Next ' IP Version Constants Const NET_FW_IP_VERSION_V4 = 0 Const NET_FW_IP_VERSION_V4_NAME = "IPv4" Const NET_FW_IP_VERSION_V6 = 1 Const NET_FW_IP_VERSION_V6_NAME = "IPv6" Const NET_FW_IP_VERSION_ANY = 2 Const NET_FW_IP_VERSION_ANY_NAME = "Any" ' Scope Constants Const NET_FW_SCOPE_ALL = 0 Const NET_FW_SCOPE_ALL_NAME = "All" Const NET_FW_SCOPE_LOCAL_SUBNET = 1 Const NET_FW_SCOPE_LOCAL_SUBNET_NAME = "Local Subnet" Const NET_FW_SCOPE_CUSTOM = 2 Const NET_FW_SCOPE_CUSTOM_NAME = "Custom" WScript.Echo("Create the FwPolicy object.") Dim fwMgr Set fwMgr = CreateObject("HNetCfg.FwMgr") WScript.Echo("Get the Policy object.") Dim fwPolicy Set fwPolicy = fwMgr.LocalPolicy WScript.Echo("Get the Profile Object.") Dim CurrentProfile Set CurrentProfile = fwPolicy.CurrentProfile WScript.Echo("Get Authorized Applications Object.") Dim fwAuthorizedApplications Set fwAuthorizedApplications = CurrentProfile.AuthorizedApplications if fwAuthorizedApplications.Count > 0 then WScript.Echo("Enumerating " & fwAuthorizedApplications.Count & " Authorized Application(s):") Dim app For Each app In CurrentProfile.AuthorizedApplications WScript.Echo(" Name: " & app.Name) WScript.Echo(" Image Filename " & app.ProcessImageFileName) Select Case app.IpVersion Case NET_FW_IP_VERSION_V4 WScript.Echo(" IP Version: " & NET_FW_IP_VERSION_V4_NAME) Case NET_FW_IP_VERSION_V6 WScript.Echo(" IP Version: " & NET_FW_IP_VERSION_V6_NAME) Case NET_FW_IP_VERSION_ANY WScript.Echo(" IP Version: " & NET_FW_IP_VERSION_ANY_NAME) End Select Select Case app.Scope Case NET_FW_SCOPE_ALL WScript.Echo(" Scope: " & NET_FW_SCOPE_ALL_NAME) Case NET_FW_SCOPE_LOCAL_SUBNET WScript.Echo(" Scope: " & NET_FW_SCOPE_LOCAL_SUBNET_NAME) Case NET_FW_SCOPE_CUSTOM WScript.Echo(" Scope: " & NET_FW_SCOPE_CUSTOM_NAME) End Select WScript.Echo(" RemoteAddresses: " & app.RemoteAddresses) WScript.Echo(" Enabled: " & app.Enabled) WScript.Echo("") Next else WScript.Echo("No Authorized Applications were found for Current Profile.") end if
Send comments about this topic to Microsoft
Build date: 2/14/2012
Sample re-coded into PowerShell
<#
.SYNOPSIS
This script displays each Firewall Authorised Application
.DESCRIPTION
This script gets the list of authorised applications, then
displays them. This is a re-write of a MSDN Script written in
VBScript.
.NOTES
File Name : Get-FWAuthorisedApplications.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell Version 2.0
.LINK
This script posted to:
http://www.pshscripts.blogspot.com
MSDN Sample posted at:
http://msdn.microsoft.com/en-us/library/aa366181%28VS.85%29.aspx
.EXAMPLE
PSH [C:\foo]: .\Get-FWAuthorisedApplications.ps1
2 Authorised Applications:
Name: : Delivery Manager Service
Image Filename : C:\Program Files (x86)\Kontiki\KService.exe
IP Version : ANY
Scope : All subnets
RemoteAddresses: *
Enabled : True
Name: : BitTorrent
Image Filename : C:\Program Files (x86)\BitTorrent\bittorrent.ex
IP Version : ANY
Scope : All subnets
RemoteAddresses: *
Enabled : True
#>
##
# Start of script
##
# IP Version Constants
$NET_FW_IP_VERSION_V4 = 0
$NET_FW_IP_VERSION_V4_NAME = "IPv4"
$NET_FW_IP_VERSION_V6 = 1
$NET_FW_IP_VERSION_V6_NAME = "IPv6"
$NET_FW_IP_VERSION_ANY = 2
$NET_FW_IP_VERSION_ANY_NAME = "ANY"
# Scope constants
$NET_FW_SCOPE_ALL = 0
$NET_FW_SCOPE_ALL_NAME = "All subnets"
$NET_FW_SCOPE_LOCAL_SUBNET = 1
$NET_FW_SCOPE_LOCAL_SUBNET_NAME = "Local subnet only"
$NET_FW_SCOPE_CUSTOM = 2
$NET_FW_SCOPE_CUSTOM_NAME = "Custom Scope (see RemoteAddresses)"
# Create the firewall manager object
$fwMgr = new-object -com HNetCfg.FwMgr
# Get the current profile for the local firewall policy
$profile = $fwMgr.LocalPolicy.CurrentProfile
#Display authorised applications
"{0} Authorised Applications:" -f $profile.AuthorizedApplications.Count
foreach ($app in $profile.AuthorizedApplications) {
" Name: : {0}" -f $app.Name
" Image Filename : {0}" -f $app.ProcessImageFileName
switch ($app.IpVersion) {
$NET_FW_IP_VERSION_V4 {" IP Version : {0}" -f $NET_FW_IP_VERSION_V4_NAME}
$NET_FW_IP_VERSION_V6 {" IP Version : {0}" -f $NET_FW_IP_VERSION_V6_NAME}
$NET_FW_IP_VERSION_ANY {" IP Version : {0}" -f $NET_FW_IP_VERSION_ANY_NAME}
}
switch ($app.Scope) {
$NET_FW_SCOPE_ALL {" Scope : {0}" -f $NET_FW_SCOPE_ALL_NAME}
$NET_FW_SCOPE_LOCAL_SUBNET {" Scope : {0}" -f $NET_FW_SCOPE_LOCAL_SUBNET_NAME}
$NET_FW_SCOPE_CUSTOM {" Scope : {0}" -f $NET_FW_SCOPE_CUSTOM_NAME}
}
" RemoteAddresses: {0}" -f $app.RemoteAddresses
" Enabled : {0}" -f $app.Enabled
""
}
- 3/22/2010
- Thomas Lee
- 3/22/2010
- Thomas Lee
Issues in the VBScript Script
The above script has a couple of issues:
- The various constants are not defined
- The firewall objects are not created
- 3/21/2010
- Thomas Lee