' Sample Code is provided for the purpose of illustration only and is not intended to be
' used in a production environment. THIS SAMPLE CODE AND ANY RELATED INFORMATION ARE PROVIDED
' "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. We
' grant You a nonexclusive, royalty-free right to use and modify the Sample Code and to
' reproduce and distribute the object code form of the Sample Code, provided that.
' You agree:
' (i) to not use Our name, logo, or trademarks to market Your software product in
' which the Sample Code is embedded;
' (ii) to include a valid copyright notice on Your software product in which the Sample Code
' is embedded; and
' (iii) to indemnify, hold harmless, and defend Us and Our suppliers from and against
' any claims or lawsuits, including attorneys’ fees, that arise or result from the
' use or distribution of the Sample Code
Option Explicit
Dim rule
Dim success
success = FALSE
' Add your application path and name below,
' NOTE: Case Sensitive
Const AppPath = "C:\temp\myapp.exe"
' Create the FwPolicy2 object.
Dim fwPolicy2
Set fwPolicy2 = CreateObject("HNetCfg.FwPolicy2")
' Get the Rules object
Dim RulesObject
Set RulesObject = fwPolicy2.Rules
For Each rule In Rulesobject
if (rule.ApplicationName = AppPath) then
Dim newApplication
Set newApplication = CreateObject("HNetCfg.FWRule")
' Copy the Firewall Rule
newApplication.Action = rule.Action
newApplication.ApplicationName = rule.ApplicationName
newApplication.Description = rule.Description
newApplication.Direction = rule.Direction
newApplication.EdgeTraversal = rule.EdgeTraversal
newApplication.Enabled = rule.Enabled
newApplication.Grouping = rule.Grouping
newApplication.Interfaces = rule.Interfaces
newApplication.LocalAddresses = rule.LocalAddresses
newApplication.Name = rule.Name
newApplication.Profiles = rule.Profiles
newApplication.RemoteAddresses = rule.RemoteAddresses
newApplication.ServiceName = rule.ServiceName
'Remove the Firewall Rule
RulesObject.Remove(rule.Name)
WScript.Echo "Removed application """ & newApplication.Name & """"
'Add back the Firewall Rule
RulesObject.Add(newApplication)
WScript.Echo "Added application """ & newApplication.Name & """"
success = TRUE
end if
Next
If success = FALSE Then
WScript.Echo "FAIL: Did not perform the remove/add operation to the application. Perhaps the AppPath does not exist"
End If
'---References
' [1] http://msdn.microsoft.com/en-us/library/aa365344(v=VS.85).aspx