'*******************************************************************************************
'*
'* File Name:
'* ---------
'* $File$
'* $Revision$
'*
'* Purpose:
'* --------
'* Stop and Start Forefront services. Switch store hooking mode. Enable or Disable Forefront 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 ; disable ; enable
'* 2. Mode => defines the mode of the function possible values 1; 2
'* 3. TextLog => defines whether this scripts logs entries to a text file under the local
'* Forefront 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\Microsoft\Forefront Server Security\"
Const REG_KEY_64 = "SOFTWARE\Wow6432Node\Microsoft\Forefront Server Security\"
' Constants for Forefront version comparision
Const FOREFRONT_10="10"
Public ForefrontInstallPath
Public ExchangeVersion
Public ForefrontVersion
' Read Software Version and install Path from server
' Determine Exchange version -> 2007 = ????
' Determine Exchange version -> 2003 = 0x00000041 65
' Determine Exchange version -> 2000 = 0x0000003c 60
ForefrontInstallPath = RetrieveRegValue (REG_KEY & "Exchange Server","InstalledPath", 1)
if IsNull(ForefrontInstallPath) then
ForefrontInstallPath = RetrieveRegValue (REG_KEY_64 & "Exchange Server","InstalledPath", 1)
end if
InstalledProduct = "Exchange Server"
' If cannot find Forefront for Exchange install path, error out.
If IsNull(ForefrontInstallPath) Then
WriteMOMEvent "TASK ERROR: Unable to retrieve Forefront for Exchange installation path. This script is intended to be run against machines running Forefront for Exchange.", 1
ScriptContext.Quit
End If
If TextLogParameter = "true" then
CreateLogFolder(ForefrontInstallPath)
End If
WriteLog "NULL"
WriteLog "Ready to Check Version"
' we will not check exchange version here
FSCStartVersion=RetrieveFileVersion (ForefrontInstallPath, "FSCStarter.exe")
WriteLog "FSCStarter.exe version = " & FSCStartVersion
If IsNull(FSCStartVersion) then
WriteMOMEvent "TASK ERROR: Unable to retrieve FSCStarter.exe This script is intended to be run with FSCStarter.exe", 1
ScriptContext.Quit
End If
FirstDot=InStr(FSCStartVersion,".")
WriteLog "Find first Dot in FSCStarter version at " & FirstDot
If FirstDot>0 Then
ForefrontVersion=Left(FSCStartVersion,FirstDot-1)
'Else
' ForefrontVersion=10
End If
WriteLog "Detect FSCStarter version = " & ForefrontVersion
' 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
Dim boolDependancyFlag
If FunctionParameter = "enable" or FunctionParameter = "disable" Then
BoolDependancyFlag = False
Else
Select Case ModeParameter
Case "1"
boolDependancyFlag = True
Case "2"
boolDependancyFlag = False
End Select
End If
WriteLog "Beginning Execution of ForefrontService Script using " & FunctionParameter & " Function,Mode " & ModeParameter & " DependancyFlag " & boolDependancyFlag
Dim boolServiceStateFlag
Dim strParam
Dim strCommand
Select Case FunctionParameter
Case "stop"
StopServicesList boolDependancyFlag
Case "start"
StartServicesList boolDependancyFlag
Case "restart"
StopServicesList boolDependancyFlag
StartServicesList boolDependancyFlag
WriteLog "Finished Execution of ForefrontService Script using " & FunctionParameter & " Function"
'***********************************************************************
'*
'* Function: FSCUtilityMode()
'*
'* Purpose: Sets up call to FSCUtility for switching hooking mode
'*
'***********************************************************************
Function FSCUtilityMode (ForefrontInstallPath, HookMode)
Dim strCommand
Dim strParam
strCommand = """" & ForefrontInstallPath & "\FSCUtility.exe" & """"
strParam = " /mode " & Hookmode
WriteLog "Running FSCUtility to switch Forefront hooking mode to " & HookMode
ShellExecute strCommand, strParam
End Function
'***********************************************************************
'*
'* Function: FSCUtilityHook()
'*
'* Purpose: Sets up call to ANTUTIL for switching hooking mode
'*
'***********************************************************************
Function FSCUtilityHook (ForefrontInstallPath, HookMode)
Dim strCommand
Dim strParam
strCommand = """" & ForefrontInstallPath & "\FSCUtility.exe" & """"
strParam = " /" & Hookmode
WriteLog "Running FSCUtility to " & Hookmode & " Forefront 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
WriteLog "Executing a stop of Forefront Security Client Controller"
StopService "MSExchangeIS" ' Depends on FSEStore
StopService "MSExchangeTransport" ' Depends on FSEIMC
'StopService "FSEStore" ' Depends on FSCController
StopService "FSEIMC" ' Depends on FSCController
StopService "FSCController" ' This is what we want to stop
StopService "FSCStatisticsService" 'The Forefront Server Statistics Service
StopService "FSEMailPickup" 'The Forefront Server Statistics Service
StopService "FSCMonitor" 'The Forefront Server Monitor Service
Else
'Exchange Services
'For Exchange 12
WriteLog "Ready to Stop Exchange Services"
' Exchange 12 POP3 Service
StopService "MSExchangePop3"
' Exchange 12 IMAP4 Service
StopService "MSExchangeImap4"
' Exchange 12 Replication Service
StopService "MsExchangeRepl"
' Exchange 12 Information Store
StopService "MSExchangeIS"
' Exchange 12 Mail Submission Service
StopService "MSExchangeMailSubmission"
' Exchange Edge Transport Service
StopService "MSExchangeTransport"
' Exchange Edge Transport Log Search
StopService "MSExchangeTransportLogSearch"
'Forefront Services
WriteLog "Ready to Stop Forefront Services"
StopService "MSExchangeIS" ' Depends on FSEStore
StopService "MSExchangeTransport" ' Depends on FSEIMC
'StopService "FSEStore" ' Depends on FSCController
StopService "FSEIMC" ' Depends on FSCController
StopService "FSCController" ' This is what we want to stop
StopService "FSCStatisticsService" 'The Forefront Server Statistics Service
StopService "FSEMailPickup" 'The Forefront Server Statistics Service
StopService "FSCMonitor" 'The Forefront Server Monitor Service
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
WriteLog "Executing a start of Forefront Security Client Controller"
StartService "FSCMonitor" ' The Forefront Server Monitor Service
StartService "FSEMailPickup" ' The Forefront Mail Pickup Service
startService "FSCStatisticsService" ' The Forefront Server Statistics Service
StartService "FSCController" ' The Forefront Server Controller Service
'StartService "FSEIMC" ' The Forefront Server IMC Service
'StartService "FSEStore" ' Depends on FSCController
StartService "MSExchangeTransport" ' Depends on FSEIMC
StartService "MSExchangeIS" ' Depends on FSEStore
Else
WriteLog "Executing a start of Forefront, Exchange and IIS services"
If FunctionParameter <> "disable" Then
StartService "FSCMonitor" ' The Forefront Server Monitor Service
StartService "FSEMailPickup" ' The Forefront Mail Pickup Service
startService "FSCStatisticsService" ' The Forefront Server Statistics Service
StartService "FSCController" ' The Forefront Server Controller Service
'StartService "FSEIMC" ' The Forefront Server IMC Service
'StartService "FSEStore" ' Depends on FSCController
StartService "MSExchangeTransport" ' Depends on FSEIMC
StartService "MSExchangeIS" ' Depends on FSEStore
Else
WriteLog "Forefront Services have been unhooked from Exchange and will not be started"
End If
'WWW Services
'Exchange 12 Services
' Exchange Edge Transport Service
StartService "MSExchangeTransport"
' Exchange Edge Transport Log Search
StartService "MSExchangeTransportLogSearch"
' Exchange 12 System Attendant
StartService "MSExchangeSA"
' Exchange 12 Replication Service
StartService "MsExchangeRepl"
' Exchange 12 IMAP4 Service
StartService "MSExchangeImap4"
' Exchange 12 POP3 Service
StartService "MSExchangePop3"
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
Dim colListOfServices
On Error Resume Next
WriteLog "Try to stop service: " & strService
Set refWMI = GetObject("winMgmts:")
strQuery="Select * from Win32_Service where Name='"+strService+"'"
Set refService=null
Err.Clear
Set colListOfServices=refWMI.ExecQuery(strQuery)
If (Err <> 0 or IsNull(colListOfServices)) Then
WriteLog "ERROR: Could not connect to Service: " & strService
Else
WriteLog "Success connect to Service: " & strService
For Each refService in colListOfServices
WriteLog "Check Service" & refService.Name & " Status"
If refService.State = "Stopped" Then
WriteLog "The service: " & refService.Name & " is already stopped"
Else
WriteLog "Stopping the service: " & refService.Name
intstop = refService.StopService()
Select Case intstop
Case 0
WMIChangeEvent refService.Name, "Stopped"
WriteLog "The service: " & refService.Name & " has Stopped"
WMIChangeEvent refService.Name, "Stopped"
Case 1
WriteLog "The service: " & refService.Name & " Not Supported"
Case 2
WriteLog "The service: " & refService.Name & " Access denied"
Case 3
WriteLog "The service: " & refService.Name & " Dependent services running."
case Else
WriteLog "unable to stop the service: " & refService.Name
End Select
End If
Next
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
Dim colListOfServices
On Error Resume Next
Set refWMI = GetObject("winMgmts:")
strQuery="Select * from Win32_Service where Name='"+strService+"'"
Set refService=null
Err.Clear
Set colListOfServices=refWMI.ExecQuery(strQuery)
If (Err <> 0 or IsNull(colListOfServices)) Then
WriteLog "ERROR: Could not connect to Service: " & strService
Else
WriteLog "Success connect to Service: " & strService
For Each refService in colListOfServices
WriteLog "Check Service" & refService.Name & " Status"
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
Next
End If
Set refWMI = Nothing
Set refService = Nothing
set colListOfServices=NoThing
Dim objFSO
set objFSO = CreateObject ("Scripting.FileSystemObject")
If objFSO.FolderExists(ForefrontInstallPath & "\MOMLogs") = 0 then
objFSO.CreateFolder(ForefrontInstallPath & "\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(ForefrontInstallPath & "\MOMLogs\" & "Tasks.log", 8, False)
' If log file doesn't exist - create it
If Err.Number <> 0 Then
Set objf = objfs.CreatetextFile(ForefrontInstallPath & "\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
WriteLog "Ready to retrieve file version of " & strPath & strFile
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)
Dim colListOfServices
WriteLog "Try to change WMI Event " & ServiceName
Set refWMI = GetObject("winMgmts:")
strQuery = "SELECT * FROM __InstanceModificationEvent WITHIN 5 WHERE TargetInstance ISA 'Win32_Service'"
Set refEventSource = refWMI.ExecNotificationQuery(strQuery)
boolAgain = True
strQuery="Select * from Win32_Service where Name='"+ServiceName+"'"
Set refService=null
Err.Clear
Set colListOfServices=refWMI.ExecQuery(strQuery)
If (Err <> 0 or IsNull(colListOfServices)) Then
WriteLog "ERROR: Could not connect to Service: " & ServiceName
Else
WriteLog "Success connect to Service: " & ServiceName
For Each refService in colListOfServices
WriteLog "Check Service" & refService.Name & " Status"
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
Next
End If
Set refWMI = Nothing
Set refEventSource = Nothing
Set refService = Nothing