Firewall Tasks Write Action

Microsoft.Forefront.TMG.FirewallService.StartStop.WA (WriteActionModuleType)

This WriteAction module is used for tasks that are targeted at Microsoft.Forefront.TMG.Server

Element properties:

TypeWriteActionModuleType
IsolationAny
AccessibilityInternal
RunAsDefault
InputTypeSystem.BaseData

Member Modules:

ID Module Type TypeId RunAs 
WA WriteAction Microsoft.Windows.ScriptWriteAction Default

Overrideable Parameters:

IDParameterTypeSelector
ScriptNamestring$Config/ScriptName$
TaskToPerformint$Config/TaskToPerform$
Argumentsstring$Config/Arguments$
TimeoutSecondsint$Config/TimeoutSeconds$

Source Code:

<WriteActionModuleType ID="Microsoft.Forefront.TMG.FirewallService.StartStop.WA" Accessibility="Internal" Batching="false">
<Configuration>
<xsd:element name="ScriptName" type="xsd:string"/>
<xsd:element name="TaskToPerform" type="xsd:int"/>
<xsd:element name="Arguments" type="xsd:string"/>
<xsd:element name="TimeoutSeconds" type="xsd:int"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="ScriptName" Selector="$Config/ScriptName$" ParameterType="string"/>
<OverrideableParameter ID="TaskToPerform" Selector="$Config/TaskToPerform$" ParameterType="int"/>
<OverrideableParameter ID="Arguments" Selector="$Config/Arguments$" ParameterType="string"/>
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" ParameterType="int"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<WriteAction ID="WA" TypeID="Windows!Microsoft.Windows.ScriptWriteAction">
<ScriptName>$Config/ScriptName$</ScriptName>
<Arguments>$Config/TaskToPerform$ $Config/Arguments$</Arguments>
<ScriptBody><Script>

'Copyright (c) Microsoft Corporation. All rights reserved.
'*************************************************************************
' $ScriptName: "StartStopISAFirewallService" $
'
' Purpose: Used to start or stop TMG Firewall Service
'
' $File: StartStopISAFirewallService.vbs $
'
' $Arguments: 1: Start ; 2: Stop
'*************************************************************************

SetLocale("en-us")

'===============
' Global variables
'===============
Dim sUtilObj
Dim sErrObj
Dim sBlnTraceRefreshed

Const conForWriting = 2
Const conForAppending = 8
Const conTraceOn = "TRACEON"

sBlnTraceRefreshed = False


'##########################################################################
' Class: Util
' Description: Contains methods for tracing, generating events,
' creating objects
'##########################################################################
Class Util

' Used to say to LogMessage when/how to print the message.
Public DBG_NONE
Public DBG_ERROR
Public DBG_WARNING
Public DBG_TRACE
Public HKEY_LOCAL_MACHINE
Public MOMApiObject
'Internal Debug Level
Private m_nDebugLevel
'Name of the logfile for tracing
Private m_logFileName

'---------------
' Properties
'---------------
Public Property Get LogFileName
LogFileName = m_logFileName
End Property

Public Property Let LogFileName(ByVal fileName)
If Not (IsEmpty(filename) OR IsNull(fileName)) Then
m_logFileName = fileName
Else
sErrObj.GenerateMOMErrorEvent("Log file name cannot be empty or null")
End If
End Property

'---------------
' Methods
'---------------
'=============
' Method: Class_Initialize
' Description: This is the constructor
'=============
Private Sub Class_Initialize()
' Initialize Debug level constants
DBG_TRACE = 1
DBG_WARNING = 2
DBG_ERROR = 3
DBG_NONE = 4

'by default only errors are logged
m_nDebugLevel = DBG_ERROR
'Create a MOM Script API object
Set MOMApiObject = MomCreateObject("MOM.ScriptAPI")
End Sub

'=============
' Method: Class_Terminate
' Description: This is the destructor
'=============
Private Sub Class_Terminate()
Set MOMApiObject = Nothing
End Sub

'=============
' Method: SetDebugLevel
' Description: To change the debugging output level of information
' generated by this utility.
' Parameters: nLevel - Level, either DBG_NONE, DBG_TRACE,
' DBG_WARNING or DBG_ERROR
'=============
Public Sub SetDebugLevel(ByVal nLevel)
m_nDebugLevel = nLevel
End Sub

'=============
' Method: WriteToTrace
' Description: Writes text to a trace file. For every run of the script,
' the trace file is recreated.
' Parameters: msg - Any text that needs to be written to the trace file.
'=============
Public Sub WriteToTrace (ByVal msg)
Dim fso, traceFileObj, objErr

Set objErr = new Error

If Not m_nDebugLevel = DBG_TRACE Then
Exit Sub
End If

On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
If Err.number &lt;&gt; 0 Then objErr.PrintError
On Error Goto 0

'Checking whether the trace file is recreated in this run
If Not sBlnTraceRefreshed Then
On Error Resume Next
Set traceFileObj = fso.OpenTextFile(sUtilObj.LogFileName, conForWriting, True)
If Err.number &lt;&gt; 0 Then objErr.PrintError
On Error Goto 0
sBlnTraceRefreshed = True
Else
On Error Resume Next
Set traceFileObj = fso.OpenTextFile(sUtilObj.LogFileName, conForAppending, True)
If Err.number &lt;&gt; 0 Then objErr.PrintError
On Error Goto 0
End If

msg = FormatDateTime(Date(), vbShortDate) &amp; " " &amp; _
FormatDateTime(Time(), vbLongTime) &amp; "[" &amp; _
WScript.ScriptName &amp; "]--&gt; " &amp; msg
On Error Resume Next
traceFileObj.WriteLine(msg)
If Err.number &lt;&gt; 0 Then objErr.PrintError
On Error Goto 0

traceFileObj.Close
Set traceFileObj = Nothing
Set fso = Nothing

End Sub

'=============
' Method: MomCreateObject
' Description: Creates an Object and includes neccessary error handling
' Parameters: sProgramId - The Program ID of the object type to be created.
' Returns: Returns the object that is created.
'=============
Public Function MomCreateObject(ByVal sProgramId)
Dim errMsg
On Error Resume Next
Set MomCreateObject = CreateObject(sProgramId)

If Err.number &lt;&gt; 0 Then
errMsg = "Unable to create automation object '" &amp; sProgramID &amp; "'" &amp; "--" &amp; _
"Error- Number: " &amp; Err.number &amp; " Description:" &amp; Err.Description &amp; _
" Source:" &amp; Err.Source
If lcase(sProgramId) = lcase("MOM.ScriptAPI") Then
CreateOpsMgrEvent 312, WScript.ScriptName, "ERROR", errMsg
ThrowEmptyDiscoveryData()
Quit()
Else
sErrObj.GenerateMOMErrorEvent(errMsg)
End If
End If
End Function

'=============
' Method: CreateOpsMgrEvent
' Description: Create an event in the Operations Manager log
' Parameters: eventId - ID of the event to be created
' source - Source of this event
' eventType - String specifying the type of event (ERROR, INFORMATION, SUCCESS, WARNING)
' desc - Description of event.
' Returns: Returns the object that is created.
'=============
Public Sub CreateOpsMgrEvent(ByVal eventId, ByVal source, ByVal eventType, ByVal desc)
Dim strCommand, WshShell

On Error Resume Next
Set WshShell = WScript.CreateObject("WScript.Shell")
strCommand = "cmd /c eventcreate /ID " &amp; eventID &amp; _
" /SO " &amp; """" &amp; source &amp; """" &amp; _
" /T " &amp; """" &amp; eventType &amp; """" &amp; _
" /D " &amp; """" &amp; desc &amp; """" &amp; _
" /L " &amp; """Operations Manager"""
WshShell.Run strCommand

If Err.number &lt;&gt; 0 Then WScript.Echo "Error in 'Util.CreateOpsMgrEvent()'"
End Sub

End Class

'##########################################################################
' Class: Error
' Description: Contains methods to Save Error details, Raise Error,
' Clear Error
' Assumptions: Util Object is created and available to use. It should be named
' as "sUtilObj"
'##########################################################################
Class Error
Private m_lNumber
Private m_sSource
Private m_sDescription
Private m_sHelpContext
Private m_sHelpFile
Private ERROR_FILE_NOT_FOUND

Public Sub Class_Initiaze()
ERROR_FILE_NOT_FOUND = 2
End Sub

Public Sub Save()
m_lNumber = Err.number
m_sSource = Err.Source
m_sDescription = Err.Description
m_sHelpContext = Err.HelpContext
m_sHelpFile = Err.helpfile
End Sub
Public Sub Raise()
Err.Raise m_lNumber, m_sSource, m_sDescription, m_sHelpFile, m_sHelpContext
End Sub
Public Sub Clear()
m_lNumber = 0
m_sSource = ""
m_sDescription = ""
m_sHelpContext = ""
m_sHelpFile = ""
End Sub
Public Default Property Get Number()
Number = m_lNumber
End Property
Public Property Get Source()
Source = m_sSource
End Property
Public Property Get Description()
Description = m_sDescription
End Property
Public Property Get HelpContext()
HelpContext = m_sHelpContext
End Property
Public Property Get HelpFile()
HelpFile = m_sHelpFile
End Property

Public Sub PrintError
WScript.Echo "Error writing to trace." &amp; _
"Error: Number-" &amp; Err.number &amp; _
"; Description-" &amp; Err.Description &amp; _
"; Source-" &amp; Err.Source
End Sub
'=============
' Method: GenerateMOMErrorEvent
' Description: Uses the "MOM Script API" object to log a script event. Appends the Error
' details to the message sent as parameter
' Parameters: strMessage - contains the custom text to write to the event
'=============
Public Function GenerateMOMErrorEvent(ByVal strMessage)
strMessage = strMessage &amp; " Error - Number:" &amp; m_lNumber &amp; _
" Source:" &amp; m_sSource &amp; _
" Description:" &amp; m_sDescription &amp; _
" HelpContext:" &amp; m_sHelpContext &amp; _
" HelpFile:" &amp; m_sHelpFile
On Error Resume Next
CreateScriptErrorEvent(strMessage)
Quit()
End Function

'=============
' Method: CreateScriptErrorEvent
' Description: Generate a MOM event with script error message in the
' Operations Manager Log.
' Parameters: strMessage - Message to write to the event
'=============
Public Function CreateScriptErrorEvent(ByVal strMessage)
On Error Resume Next
sUtilObj.MOMApiObject.LogScriptEvent WScript.ScriptName, 4001, 1, strMessage
End Function

'=============
' Method: ErrorCheck
' Description: Checks if an error occurred and writes the error to Operations Manager
' event log.
'=============
Public Sub ErrorCheck()
If Err.number &lt;&gt; 0 Then
Save
GenerateMOMErrorEvent("")
End If
End Sub

Public Sub ErrorCheckMsg(ByVal msg)
If Err.number &lt;&gt; 0 Then
Save
GenerateMOMErrorEvent(msg)
End If
End Sub

End Class

Const conStartISAFirewallService = 1
Const conStopISAFirewallSerive = 2

Sub Main()

Dim objFPCRoot, objFPCServer, arrArgs, startOrStop
Set sUtilObj = new Util
Set sErrObj = new Error

Set arrArgs = Wscript.Arguments
If arrArgs.Count &gt;= 1 AND arrArgs.Count &lt;= 2 Then
If arrArgs.Count = 2 Then
'Check if Trace is ON
If (lcase(trim(arrArgs(1))) = lcase(conTraceOn)) Then
'Set the debug level to Trace
sUtilObj.SetDebugLevel(sUtilObj.DBG_TRACE)
'Set the trace log file name
sUtilObj.LogFileName = WScript.ScriptName &amp; ".trace"
End If
End If
startOrStop = CInt(arrArgs(0))
Else
sErrObj.GenerateMOMErrorEvent("Invalid number of arguments passed to script.")
End if

sUtilObj.WriteToTrace "Start"

Set objFPCRoot = sUtilObj.MomCreateObject("FPC.Root")

On Error Resume Next
Set objFPCServer = objFPCRoot.GetContainingServer()
sErrObj.ErrorCheckMsg "Error retrieving FPCServer"
On Error Goto 0

Select Case startOrStop
Case conStartISAFirewallService
objFPCServer.StartFirewallService()
sUtilObj.WriteToTrace "Start TMG Firewall Service"
Case conStopISAFirewallSerive
objFPCServer.StopFirewallService()
sUtilObj.WriteToTrace "Stop TMG Firewall Service"
Case Else
sErrObj.GenerateMOMErrorEvent "Invalid value of argument 'TaskToPerform':" &amp; startOrStop &amp; " The value should be 1(Start) or 2(Stop)."
End Select

sUtilObj.WriteToTrace "End"

Set objFPCServer = Nothing
Set objFPCRoot = Nothing
Set sUtilObj = Nothing
Set sErrObj = Nothing
End Sub

Main()
</Script></ScriptBody>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="WA"/>
</Composition>
</Composite>
</ModuleImplementation>
<InputType>System!System.BaseData</InputType>
</WriteActionModuleType>