' FILE: InfFirewallSetup.vbs
' Version 2.0
Option Explicit 

' Define Constants from the SDK and their associated string name Profile Type
Const NET_FW_PROFILE_DOMAIN = 0
Const NET_FW_PROFILE_DOMAIN_NAME = "Domain"
Const NET_FW_PROFILE_STANDARD = 1
Const NET_FW_PROFILE_STANDARD_NAME = "Standard"

'*****************************************************************************
' Function to find an application exception
Function FindApplicationException(fwProfile, applicationPath)
   Dim application

   Set FindApplicationException = Nothing

   For Each application In fwProfile.AuthorizedApplications
      If application.ProcessImageFileName = applicationPath Then
         Set FindApplicationException = application
      End If
   Next
End Function

'*****************************************************************************
' Helper routine for ConfigureApplication
Sub AddApplication(fwProfile, applicationName, applicationPath)
   Dim application
   Set application = CreateObject("HNetCfg.FwAuthorizedApplication")
   application.Name = applicationName
   application.ProcessImageFileName = applicationPath
   call fwProfile.AuthorizedApplications.Add(application)
End Sub 

'*****************************************************************************
' Helper routine to add an open port
Sub AddPort(fwProfile, applicationName, portNumer)
   Dim port
   Set port = CreateObject("HNetCfg.FwOpenPort")
   port.Name = applicationName
   port.Port = portNumer
   port.Protocol = 6
   port.RemoteAddresses = "*"
   port.Enabled = True
   fwProfile.GloballyOpenPorts.Add port
End Sub 

'*****************************************************************************
' Subroutine to set the application to a profile if it does not exist 
Sub ConfigureApplication(fwProfile, applicationName, applicationPath)
   Dim application

   Set application = FindApplicationException(fwProfile, applicationPath)

   If application Is Nothing Then
      Call AddApplication(fwProfile, applicationName, applicationPath)
      Set application = FindApplicationException(fwProfile, applicationPath)
      If Not application Is Nothing Then
         application.Enabled = False
      End If
   End If
End Sub

'*****************************************************************************
' Subroutine to set the application to a profile if it does not exist 
Sub ConfigureApplicationB(fwProfile, applicationName, applicationPath)
   Dim application

   Set application = FindApplicationException(fwProfile, applicationPath)

   If application Is Nothing Then
      Call AddApplication(fwProfile, applicationName, applicationPath)
      Set application = FindApplicationException(fwProfile, applicationPath)
      If Not application Is Nothing Then
         application.Enabled = True
      End If
   End If
End Sub

'*****************************************************************************
' MAIN - InfFirewallSetup.vbs
'*****************************************************************************

Dim ApplicationPathAr(15)
Dim ApplicationNameAr(15)
Dim AppCnt

' Assign path and name for each scope app that must be exceptioned
' in the Windows Firewall
ApplicationPathAr(0) = "C:\scope\util\ExecuteGpibScript.exe"
ApplicationNameAr(0) = "Infiniium ExecuteGpibScript Utility"
ApplicationPathAr(1) = "C:\scope\util\SlewRate.exe"
ApplicationNameAr(1) = "Infiniium SlewRate Utility"
ApplicationPathAr(2) = "C:\scope\util\Snapshot.exe"
ApplicationNameAr(2) = "Infiniium Snapshot Utility"
ApplicationPathAr(3) = "C:\scope\util\StepAveraging.exe"
ApplicationNameAr(3) = "Infiniium StepAveraging Utility"
ApplicationPathAr(4) = "C:\scope\util\ToggleAveraging.exe"
ApplicationNameAr(4) = "Infiniium ToggleAveraging Utility"
ApplicationPathAr(5) = "C:\scope\util\ClearMessageLine.exe"
ApplicationNameAr(5) = "Infiniium ClearMessageLine Utility"
ApplicationPathAr(6) = "C:\scope\util\CrestFactor.exe"
ApplicationNameAr(6) = "Infiniium CrestFactor Utility"
ApplicationPathAr(7) = "C:\scope\util\ZeroCrossings.exe"
ApplicationNameAr(7) = "Infiniium ZeroCrossings Utility"
ApplicationPathAr(8) = "C:\Program Files\Agilent\Agilent M1 v4\M1_32.exe"
ApplicationNameAr(8) = "ASA Oscilloscope Tools"

' Set AppCnt to the total number of apps
AppCnt = 9

' Disable error handling for CreateObject
On Error Resume Next 

' Create the firewall manager object
Dim fwMgr
Set fwMgr = CreateObject("HNetCfg.FwMgr")

Dim domainProfile
' Get the Domain Profile for Local Policy and Add Application Exception 
Set domainProfile = fwMgr.LocalPolicy.GetProfileByType(NET_FW_PROFILE_DOMAIN)

' Get the Domain Profile for Local Policy and Add Application Exception 
Dim standardProfile
Set standardProfile = fwMgr.LocalPolicy.GetProfileByType(NET_FW_PROFILE_STANDARD)

If IsObject(fwMgr) Then
   ' Re-enable default handling for everything else
   On Error GoTo 0

   Dim i
   For i = 0 To AppCnt - 1
      Call ConfigureApplication(domainProfile, ApplicationNameAr(i), ApplicationPathAr(i)) 
      Call ConfigureApplication(standardProfile, ApplicationNameAr(i), ApplicationPathAr(i)) 
   Next

   Call ConfigureApplicationB( domainProfile, "Infiniium Oscilloscope Application", "C:\scope\bin\agScope.exe" )
   Call ConfigureApplicationB( standardProfile, "Infiniium Oscilloscope Application", "C:\scope\bin\agScope.exe" )

   Call AddPort(domainProfile, "WinVNC", 5900)
   Call AddPort(standardProfile, "WinVNC", 5900)
   Call AddPort(domainProfile, "WinVNCWeb", 5800)
   Call AddPort(standardProfile, "WinVNCWeb", 5800)
End If
