Antigen Service Control

Antigen_Service_Control.NoHost (WriteActionModuleType)

Performs functions requiring the control (stop and start) of Antigen and Exchange related services

Element properties:

TypeWriteActionModuleType
IsolationAny
AccessibilityInternal
RunAsDefault
InputTypeSystem.BaseData
Comment{25385F79-E395-46B5-84C9-68949E7936AD}

Member Modules:

ID Module Type TypeId RunAs 
RunScriptAction WriteAction System.Mom.BackwardCompatibility.NoHost.ScriptResponse Default

Overrideable Parameters:

IDParameterTypeSelectorDisplay NameDescription
Functionstring$Config/Parameters/Function$FunctionDetermines the mode of control eg. stop; start; restart; switch....
Modestring$Config/Parameters/Mode$ModeDetermines a secondary action related to the "function" control
TextLogstring$Config/Parameters/TextLog$TextLogDetermines if text file logging is enabled or disabled

Source Code:

<WriteActionModuleType ID="Antigen_Service_Control.NoHost" Accessibility="Internal" Comment="{25385F79-E395-46B5-84C9-68949E7936AD}">
<Configuration>
<IncludeSchemaTypes>
<SchemaType>MomBackwardCompatibility!System.Mom.BackwardCompatibility.AlertGenerationSchema</SchemaType>
</IncludeSchemaTypes>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="AlertGeneration" type="AlertGenerationType"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="InvokerType" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Parameters" minOccurs="0">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Function" type="xsd:string" minOccurs="0"/>
<xsd:element name="Mode" type="xsd:string" minOccurs="0"/>
<xsd:element name="TextLog" type="xsd:string" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="Function" Selector="$Config/Parameters/Function$" ParameterType="string"/>
<OverrideableParameter ID="Mode" Selector="$Config/Parameters/Mode$" ParameterType="string"/>
<OverrideableParameter ID="TextLog" Selector="$Config/Parameters/TextLog$" ParameterType="string"/>
</OverrideableParameters>
<ModuleImplementation>
<Composite>
<MemberModules>
<WriteAction ID="RunScriptAction" TypeID="MomBackwardCompatibility!System.Mom.BackwardCompatibility.NoHost.ScriptResponse">
<AlertGeneration>$Config/AlertGeneration$</AlertGeneration>
<InvokerType>$Config/InvokerType$</InvokerType>
<Body><Script>

'*******************************************************************************************
'*
'* File Name:
'* ---------
'* AntigenServiceControl.vbs
'* $Revision: 8 $
'*
'* Purpose:
'* --------
'* Stop and Start Antigen services. Switch store hooking mode. Enable or Disable Antigen hooks
'*
'* Parameters:
'* -----------
'* Function
'* Mode
'* TextLog
'*
'*
'*
'* Parameter definitions:
'* ----------------------
'* 1. Function =&gt; defines the control function to be performed on the services. Possibles
'* values are start ; stop ; restart ;switch ; disable ; enable
'* 2. Mode =&gt; defines the mode of the function possible values 1; 2 ; ese ; vsapi
'* 3. TextLog =&gt; defines whether this scripts logs entries to a text file under the local
'* Antigen installation folder. Possible Values are true ; false
'*
'*******************************************************************************************

' Constant for controlling text file operations

Const FOR_READING = 1

'Registry Constants

Const HIVE = "HKEY_LOCAL_MACHINE\"
Const REG_KEY = "SOFTWARE\Sybari Software\"

' Constants for Antigen version comparision

Const ANTIGEN_9 = "9"
Const ANTIGEN_8 = "8"
Const ANTIGEN_7 = "7"

Public AntigenInstallPath
Public ExchangeVersion
Public AntigenVersion

' Retrieve script parameters

FunctionParameter = ScriptContext.Parameters.Get("Function")
ModeParameter = ScriptContext.Parameters.Get("Mode")
TextLogParameter = ScriptContext.Parameters.Get("TextLog")


' Read Software Version and install Path from server
' Determine Exchange version -&gt; 2003 = 0x00000041 65
' Determine Exchange version -&gt; 2000 = 0x0000003c 60


AntigenInstallPath = RetrieveRegValue (REG_KEY &amp; "Antigen For Exchange","InstalledPath", 1)
InstalledProduct = "Antigen For Exchange"

' If cannot find Antigen for Exchange install path, error out.
' This script is not intended for use with Antigen for SMTP
If IsNull(AntigenInstallPath) Then
WriteMOMEvent "TASK ERROR: Unable to retrieve Antigen for Exchange installation path. This script is intended to be run against machines running Antigen for Exchange.", 1
ScriptContext.Quit
End If

ExchangeVersion = RetrieveRegValue ("SOFTWARE\Microsoft\Exchange\Setup","Services Version", 2)

If IsNull(ExchangeVersion) Then
WriteMOMEvent "TASK ERROR: Unable To Retrieve Exchange Installation Path", 1
ScriptContext.Quit
End If

AntigenVersion = Left(RetrieveFileVersion (AntigenInstallPath, "AntigenStarter.exe"), 1)

If TextLogParameter = "true" then
CreateLogFolder(AntigenInstallPath)
End If


' Set what exchange services should be handled
' boolDependancyFlag = True -&gt; only dependant exchange services will be handled
' boolDependancyFlag = False -&gt; all Exchange and IIS services will be handled


WriteLog "NULL"
WriteLog "Beginning Execution of AntigenService Script using " &amp; FunctionParameter &amp; " Function"

Dim boolDependancyFlag


If FunctionParameter = "enable" or FunctionParameter = "disable" Then
BoolDependancyFlag = False
Else
Select Case ModeParameter
Case "1"
boolDependancyFlag = True
Case "2"
boolDependancyFlag = False
Case "ese"
boolDependancyFlag = False
Case "vsapi"
boolDependancyFlag = False
End Select
End If


Dim boolServiceStateFlag
Dim strParam
Dim strCommand

Select Case FunctionParameter
Case "stop"
StopServicesList boolDependancyFlag

Case "start"
StartServicesList boolDependancyFlag

Case "restart"
StopServicesList boolDependancyFlag
StartServicesList boolDependancyFlag

Case "switch"
StopServicesList boolDependancyFlag
Wait 2
'ScriptContext.Sleep (2000)
AntutilMode AntigenInstallPath, ModeParameter
Wait 2
StartServicesList boolDependancyFlag
CheckHookMode

Case "disable"
StopServicesList boolDependancyFlag
AntutilHook AntigenInstallPath, "disable"
Wait 2
StartServicesList boolDependancyFlag

Case "enable"
StopServicesList boolDependancyFlag
AntutilHook "enable"
Wait 2
StartServicesList boolDependancyFlag

End Select

WriteLog "Finished Execution of AntigenService Script using " &amp; FunctionParameter &amp; " Function"


'***********************************************************************
'*
'* Function: CheckHookMode()
'*
'* Purpose: Checks Hooking Mode from registry
'*
'***********************************************************************

Function CheckHookMode

Dim intRegHook


intRegHook = RetrieveRegValue ("SOFTWARE\Sybari Software\Antigen for Exchange","AntigenHooking", 2)
Select Case intRegHook
Case 1
WriteLog "Antigen is now set to ESE shim mode."
Case 2
WriteLog "Antigen is now set to VSAPI mode."

End Select


End Function


'***********************************************************************
'*
'* Function: AntutilMode()
'*
'* Purpose: Sets up call to ANTUTIL for switching hooking mode
'*
'***********************************************************************

Function AntutilMode (AntigenInstallPath, HookMode)

Dim strCommand
Dim strParam

strCommand = """" &amp; AntigenInstallPath &amp; "\Antutil.exe" &amp; """"
strParam = " /mode " &amp; Hookmode

WriteLog "Running ANTUTIL to switch Antigen hooking mode"
ShellExecute strCommand, strParam

End Function


'***********************************************************************
'*
'* Function: AntutilHook()
'*
'* Purpose: Sets up call to ANTUTIL for switching hooking mode
'*
'***********************************************************************

Function AntutilHook (AntigenInstallPath, HookMode)

Dim strCommand
Dim strParam

strCommand = """" &amp; AntigenInstallPath &amp; "\Antutil.exe" &amp; """"
strParam = " /" &amp; Hookmode

WriteLog "Running ANTUTIL to " &amp; Hookmode &amp; " Antigen hooking"
ShellExecute strCommand, strParam

End Function


'***********************************************************************
'*
'* Function: ShellExecute()
'*
'* Purpose: Sets up call to ANTUTIL for switching hooking mode
'*
'***********************************************************************

Function ShellExecute (Command, Parameter)

set wshShell = CreateObject ("wscript.shell")

wshShell.run Command &amp; Parameter,,false

Set wshShell = Nothing

End Function


'***********************************************************************
'*
'* Function: StopServiceSList()
'*
'* Purpose: Generates Calls to stop all required Services
'*
'***********************************************************************

Function StopServicesList (boolDependancyFlag)
Dim refWMI
Dim refService

Set refWMI = GetObject("winMgmts:")

if boolDependancyFlag then
Select Case AntigenVersion
Case ANTIGEN_9
WriteLog "Executing a stop of Antigen services"
StopService "AntigenService"
Case ANTIGEN_8
WriteLog "Executing a stop of Antigen services"
StopService "AntigenService"
Case ANTIGEN_7
WriteLog "Executing a stop of Antigen and dependancy related Exchange services"
Set refService = refWMI.Get("Win32_Service.Name='POP3Svc'")
If refService.StartMode &lt;&gt; "Disabled" Then
StopService "POP3Svc"
End If

Set refService = refWMI.Get("Win32_Service.Name='IMAP4Svc'")
If refService.StartMode &lt;&gt; "Disabled" Then
StopService "IMAP4Svc"
End If

StopService "SMTPSVC"
StopService "MSExchangeIS"
StopService "AntigenIMC"
StopService "AntigenStore"
StopService "AntigenService"
StopService "AntigenMonitor"
End Select
Else
'Exchange Services

WriteLog "Executing a stop of Antigen, Exchange and IIS services"
Set refService = refWMI.Get("Win32_Service.Name='POP3Svc'")
If refService.StartMode &lt;&gt; "Disabled" Then
StopService "POP3Svc"
End If
Set refService = refWMI.Get("Win32_Service.Name='IMAP4Svc'")
If refService.StartMode &lt;&gt; "Disabled" Then
StopService "IMAP4Svc"
End If
Set refService = refWMI.Get("Win32_Service.Name='MSExchangeSRS'")
If refService.StartMode &lt;&gt; "Disabled" Then
StopService "MSExchangeSRS"
End If
StopService "MSExchangeMGMT"
StopService "RESvc"
StopService "MSExchangeMTA"
StopService "MSExchangeIS"
StopService "MSExchangeSA"

'IIS Services

Set refService = refWMI.Get("Win32_Service.Name='NntpSvc'")
If refService.StartMode &lt;&gt; "Disabled" Then
StopService "NntpSvc"
End If

StopService "SMTPSVC"
StopService "W3SVC"
StopService "HTTPFilter"
StopService "NNTPSVC"
StopService "MSFTPSVC"
StopService "IISADMIN"

'Antigen Services

StopService "AntigenIMC"
StopService "AntigenStore"
StopService "AntigenService"
StopService "AntigenMonitor"
End If

Set refWMI = Nothing
Set refService = Nothing

End Function

'***********************************************************************
'*
'* Function: StartServiceSList()
'*
'* Purpose: Generates Calls to stop all required Services
'*
'***********************************************************************

Function StartServicesList (boolDependancyFlag)

Dim refWMI
Dim refService

Set refWMI = GetObject("winMgmts:")

if boolDependancyFlag then
Select Case AntigenVersion
Case ANTIGEN_9
WriteLog "Executing a start of Antigen services"
StartService "AntigenService"
Case ANTIGEN_8
WriteLog "Executing a start of Antigen services"
StartService "AntigenService"
Case ANTIGEN_7
WriteLog "Executing a start of Antigen and dependancy related Exchange services"
StartService "AntigenMonitor"
StartService "AntigenService"
StartService "AntigenStore"
StartService "AntigenIMC"
StartService "MSExchangeIS"
StartService "SMTPSVC"

Set refService = refWMI.Get("Win32_Service.Name='POP3Svc'")
If refService.StartMode &lt;&gt; "Disabled" Then
StartService "POP3Svc"
End If

Set refService = refWMI.Get("Win32_Service.Name='IMAP4Svc'")
If refService.StartMode &lt;&gt; "Disabled" Then
StartService "IMAP4Svc"
End If
End Select
Else
WriteLog "Executing a start of Antigen, Exchange and IIS services"

'IIS Service

StartService "IISADMIN"
StartService "MSFTPSVC"
StartService "NNTPSVC"

If FunctionParameter &lt;&gt; "disable" Then
'Antigen Services
StartService "AntigenMonitor"
StartService "AntigenIMC"
StartService "AntigenStore"
StartService "AntigenService"
Else
WriteLog "Antigen Services have been unhooked from Exchange and will not be started"
End If

'WWW Services

Set refService = refWMI.Get("Win32_Service.Name='NntpSvc'")
If refService.StartMode &lt;&gt; "Disabled" Then
StartService "NntpSvc"
End If
StartService "W3SVC"
StartService "SMTPSVC"

'Exchange Services

StartService "MSExchangeSA"
StartService "MSExchangeIS"
StartService "MSExchangeMTA"
StartService "RESvc"
StartService "MSExchangeMGMT"

Set refService = refWMI.Get("Win32_Service.Name='MSExchangeSRS'")
If refService.StartMode &lt;&gt; "Disabled" Then
StartService "MSExchangeSRS"
End If
Set refService = refWMI.Get ("Win32_Service.Name='IMAP4Svc'")
If refService.StartMode &lt;&gt; "Disabled" Then
StartService "IMAP4Svc"
End If

Set refService = refWMI.Get("Win32_Service.Name='POP3Svc'")
If refService.StartMode &lt;&gt; "Disabled" Then
StartService "POP3Svc"
End If

End If

Set refWMI = Nothing
Set refService = Nothing

End Function

'***********************************************************************
'*
'* Function: StopService()
'*
'* Purpose: Generates Calls to stop all Services
'*
'***********************************************************************


Function StopService (strService)

Dim refWMI
Dim refService

On Error Resume Next
Err.Clear

Set refWMI = GetObject("winMgmts:")


Set refService = refWMI.Get("Win32_Service.Name='" &amp; strService &amp; "'")
If Err &lt;&gt; 0 Then
WriteLog "ERROR: Could not connect to Service: " &amp; strService
Else
If refService.State = "Stopped" Then
WriteLog "The service: " &amp; refService.Name &amp; " is already stopped"
Else
WriteLog "Stopping the service: " &amp; refService.Name
intstop = refService.StopService()

WMIChangeEvent refService.Name, "Stopped"

WriteLog "The service: " &amp; refService.Name &amp; " has Stopped"
End If
End If

Set refWMI = Nothing
Set refService = Nothing

End Function


'***********************************************************************
'*
'* Function: StartService()
'*
'* Purpose: Generates Calls to start all Services
'*
'***********************************************************************


Function StartService (strService)

Dim refWMI
Dim refService

On Error Resume Next
Err.Clear

Set refWMI = GetObject("winMgmts:")


Set refService = refWMI.Get("Win32_Service.Name='" &amp; strService &amp; "'")
If Err &lt;&gt; 0 Then
WriteLog "ERROR: Could not connect to Service: " &amp; strService
Else
If refService.State = "Running" Then
WriteLog "The service: " &amp; refService.Name &amp; " is already started"
ElseIf refService.StartMode = "Disabled" Then
WriteLog "The service: " &amp; refService.Name &amp; " is unexpectedly disabled. Cannot start"
Else
WriteLog "Starting the service: " &amp; refService.Name
intStart = refService.StartService()

WMIChangeEvent refService.Name, "Running"

WriteLog "The service: " &amp; refService.Name &amp; " is started"
End If
End If

Set refWMI = Nothing
Set refService = Nothing

End Function


'***********************************************************************
'*
'* Function: CreateLogFolder()
'*
'* Purpose: Creates Log subfolder under Antigen
'*
'***********************************************************************

Function CreateLogFolder (AntigenInstallPath)

Dim objFSO
set objFSO = CreateObject ("Scripting.FileSystemObject")

If objFSO.FolderExists(AntigenInstallPath &amp; "\MOMLogs") = 0 then
objFSO.CreateFolder(AntigenInstallPath &amp; "\MOMLogs")
End If

Set objFSO = Nothing

End Function


'***********************************************************************
'*
'* Function: WriteLog()
'*
'* Purpose: Writes script activity and errors to a log file
'*
'***********************************************************************


Function WriteLog (strLogText)

Dim objfs
Dim objf
Dim strTimeStamp

If TextLogParameter = "true" Then

On Error Resume Next
Err.Clear

Set objfs = CreateObject("Scripting.FileSystemObject")
Set objf = objfs.OpentextFile(AntigenInstallPath &amp; "\MOMLogs\" &amp; "Tasks.log", 8, False)


' If log file doesn't exist - create it
If Err.Number &lt;&gt; 0 Then
Set objf = objfs.CreatetextFile(AntigenInstallPath &amp; "\MOMLogs\" &amp; "Tasks.log", False)
Err.Clear
End If

If strLogText = "NULL" then
objf.WriteLine("")
Else
strTimeStamp = Date &amp; " " &amp; Time &amp; " "
objf.WriteLine(strTimeStamp &amp; strLogText)
End If
End If

Set objfs = Nothing
Set objf = Nothing

End Function



'********************************************************************
'*
'* Function: RetrieveRegValue
'*
'* Purpose: Retrieves String Value from Registry
'*
'********************************************************************

Function RetrieveRegValue (Key, strValueName, intValueType)

' intValueType -&gt; 1 = String Value
' -&gt; 2 = DWORD Value

const HKEY_LOCAL_MACHINE = &amp;H80000002
Dim strServerName
Dim objReg
Dim strRegValue


On Error Resume Next
Err.Clear
Set objReg=GetObject("winmgmts:\root\default:StdRegProv")
If Err.Number &lt;&gt; 0 Then
Err.Clear
strRegValue = NULL
Else

Select Case intValueType
Case 1
strErr = objReg.GetStringValue (HKEY_LOCAL_MACHINE, Key, strValueName, strRegValue)

Case 2
strErr = objReg.GetDWORDValue (HKEY_LOCAL_MACHINE, Key, strValueName, strRegValue)

End Select

' if reading the registry fails via wmi return error

If strErr &lt;&gt; 0 then
strRegValue = NULL
End If
End If

Set objReg = Nothing

RetrieveRegValue = strRegValue

End Function


'********************************************************************
'*
'* Function: RetrieveFileVersion
'*
'* Purpose: Retrieves File Version number
'*
'********************************************************************

Function RetrieveFileVersion (strPath, strFile)

Dim refFile
Dim ver

Set refFile = GetObject("winMgmts:CIM_DataFile.Name='" &amp; strPath &amp; "\" &amp; strFile &amp; "'")

ver = refFile.version

Set refFile = Nothing

RetrieveFileVersion = ver

End Function


'********************************************************************
'*
'* Function: Wait
'*
'* Purpose: Creates timed pause in the execution
'*
'*
'********************************************************************

Function Wait (intNumSeconds)

ScriptContext.Sleep intNumSeconds

End Function

'********************************************************************
'*
'* Function: WMIChangeEvent()
'*
'* Purpose: Waits for appropriate WMI event to signal servive state change
'*
'*
'********************************************************************

Function WMIChangeEvent (ServiceName, State)


Set refWMI = GetObject("winMgmts:")

strQuery = "SELECT * FROM __InstanceModificationEvent WITHIN 5 WHERE TargetInstance ISA 'Win32_Service'"

Set refEventSource = refWMI.ExecNotificationQuery(strQuery)

boolAgain = True

Set refService = refWMI.Get("Win32_Service.Name='" &amp; ServiceName &amp; "'")

If refService.State &lt;&gt; State Then

Do
Set refEvent = refEventSource.NextEvent()
If (strComp(refEvent.TargetInstance.Name, ServiceName, 1) = 0) And refEvent.TargetInstance.State = State Then
boolAgain = False
End If
Loop While boolAgain

End If

Set refWMI = Nothing
Set refEventSource = Nothing
Set refService = Nothing

End Function

'***********************************************************************
'*
'* Function: WriteMOMEvent()
'*
'* Purpose: Writes events to MOM EventLog
'*
'***********************************************************************

Function WriteMOMEvent(EventMessage, EventType)

Dim objEvt
Set objEvt = ScriptContext.CreateEvent

objEvt.Message = EventMessage
objEvt.EventType = EventType
ScriptContext.Submit(objEvt)

set objEvt = Nothing

End Function
</Script></Body>
<Language>VBScript</Language>
<Name>Antigen Service Control</Name>
<Parameters>
<Parameter>
<Name>Function</Name>
<Value>$Config/Parameters/Function$</Value>
</Parameter>
<Parameter>
<Name>Mode</Name>
<Value>$Config/Parameters/Mode$</Value>
</Parameter>
<Parameter>
<Name>TextLog</Name>
<Value>$Config/Parameters/TextLog$</Value>
</Parameter>
</Parameters>
<ManagementPackId>[Microsoft.Antigen.v9,,1.0.0.1]</ManagementPackId>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="RunScriptAction"/>
</Composition>
</Composite>
</ModuleImplementation>
<InputType>SystemLibrary!System.BaseData</InputType>
</WriteActionModuleType>