'*******************************************************************************************
'*
'* 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 => defines the control function to be performed on the services. Possibles
'* values are start ; stop ; restart ;switch ; disable ; enable
'* 2. Mode => defines the mode of the function possible values 1; 2 ; ese ; vsapi
'* 3. TextLog => defines whether this scripts logs entries to a text file under the local
'* Antigen installation folder. Possible Values are true ; false
'*
'*******************************************************************************************
' Read Software Version and install Path from server
' Determine Exchange version -> 2003 = 0x00000041 65
' Determine Exchange version -> 2000 = 0x0000003c 60
AntigenInstallPath = RetrieveRegValue (REG_KEY & "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
If TextLogParameter = "true" then
CreateLogFolder(AntigenInstallPath)
End If
' Set what exchange services should be handled
' boolDependancyFlag = True -> only dependant exchange services will be handled
' boolDependancyFlag = False -> all Exchange and IIS services will be handled
WriteLog "NULL"
WriteLog "Beginning Execution of AntigenService Script using " & FunctionParameter & " 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
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 = """" & AntigenInstallPath & "\Antutil.exe" & """"
strParam = " /mode " & Hookmode
'***********************************************************************
'*
'* Function: AntutilHook()
'*
'* Purpose: Sets up call to ANTUTIL for switching hooking mode
'*
'***********************************************************************
Function AntutilHook (AntigenInstallPath, HookMode)
Dim strCommand
Dim strParam
strCommand = """" & AntigenInstallPath & "\Antutil.exe" & """"
strParam = " /" & Hookmode
WriteLog "Running ANTUTIL to " & Hookmode & " 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 & 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 <> "Disabled" Then
StopService "POP3Svc"
End If
Set refService = refWMI.Get("Win32_Service.Name='IMAP4Svc'")
If refService.StartMode <> "Disabled" Then
StopService "IMAP4Svc"
End If
WriteLog "Executing a stop of Antigen, Exchange and IIS services"
Set refService = refWMI.Get("Win32_Service.Name='POP3Svc'")
If refService.StartMode <> "Disabled" Then
StopService "POP3Svc"
End If
Set refService = refWMI.Get("Win32_Service.Name='IMAP4Svc'")
If refService.StartMode <> "Disabled" Then
StopService "IMAP4Svc"
End If
Set refService = refWMI.Get("Win32_Service.Name='MSExchangeSRS'")
If refService.StartMode <> "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 <> "Disabled" Then
StopService "NntpSvc"
End If
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 <> "Disabled" Then
StartService "POP3Svc"
End If
Set refService = refWMI.Get("Win32_Service.Name='IMAP4Svc'")
If refService.StartMode <> "Disabled" Then
StartService "IMAP4Svc"
End If
End Select
Else
WriteLog "Executing a start of Antigen, Exchange and IIS services"
If FunctionParameter <> "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 <> "Disabled" Then
StartService "NntpSvc"
End If
StartService "W3SVC"
StartService "SMTPSVC"
Set refService = refWMI.Get("Win32_Service.Name='MSExchangeSRS'")
If refService.StartMode <> "Disabled" Then
StartService "MSExchangeSRS"
End If
Set refService = refWMI.Get ("Win32_Service.Name='IMAP4Svc'")
If refService.StartMode <> "Disabled" Then
StartService "IMAP4Svc"
End If
Set refService = refWMI.Get("Win32_Service.Name='POP3Svc'")
If refService.StartMode <> "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='" & strService & "'")
If Err <> 0 Then
WriteLog "ERROR: Could not connect to Service: " & strService
Else
If refService.State = "Stopped" Then
WriteLog "The service: " & refService.Name & " is already stopped"
Else
WriteLog "Stopping the service: " & refService.Name
intstop = refService.StopService()
WMIChangeEvent refService.Name, "Stopped"
WriteLog "The service: " & refService.Name & " 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='" & strService & "'")
If Err <> 0 Then
WriteLog "ERROR: Could not connect to Service: " & strService
Else
If refService.State = "Running" Then
WriteLog "The service: " & refService.Name & " is already started"
ElseIf refService.StartMode = "Disabled" Then
WriteLog "The service: " & refService.Name & " is unexpectedly disabled. Cannot start"
Else
WriteLog "Starting the service: " & refService.Name
intStart = refService.StartService()
WMIChangeEvent refService.Name, "Running"
WriteLog "The service: " & refService.Name & " is started"
End If
End If
Dim objFSO
set objFSO = CreateObject ("Scripting.FileSystemObject")
If objFSO.FolderExists(AntigenInstallPath & "\MOMLogs") = 0 then
objFSO.CreateFolder(AntigenInstallPath & "\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 & "\MOMLogs\" & "Tasks.log", 8, False)
' If log file doesn't exist - create it
If Err.Number <> 0 Then
Set objf = objfs.CreatetextFile(AntigenInstallPath & "\MOMLogs\" & "Tasks.log", False)
Err.Clear
End If
If strLogText = "NULL" then
objf.WriteLine("")
Else
strTimeStamp = Date & " " & Time & " "
objf.WriteLine(strTimeStamp & 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 -> 1 = String Value
' -> 2 = DWORD Value
const HKEY_LOCAL_MACHINE = &H80000002
Dim strServerName
Dim objReg
Dim strRegValue
On Error Resume Next
Err.Clear
Set objReg=GetObject("winmgmts:\root\default:StdRegProv")
If Err.Number <> 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 <> 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='" & strPath & "\" & strFile & "'")
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='" & ServiceName & "'")
If refService.State <> 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