Contrôle du service de Forefront

Forefront_Service_Control (WriteActionModuleType)

Exécute les fonctions qui nécessitent le contrôle (arrêt et démarrage) de Forefront et des services associés d’Exchange.

Element properties:

TypeWriteActionModuleType
IsolationAny
AccessibilityInternal
RunAsDefault
InputTypeSystem.BaseData
Comment{E85AB2D3-7F7D-4A37-9330-F0803AFB7DF7}

Member Modules:

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

Overrideable Parameters:

IDParameterTypeSelectorDisplay NameDescription
Functionstring$Config/Parameters/Function$FonctionDétermine le mode de contrôle, par exemple démarrage, arrêt, redémarrage et basculer
Modestring$Config/Parameters/Mode$ModeDétermine une action secondaire relative au contrôle « fonction »
TextLogstring$Config/Parameters/TextLog$TextLogDétermine si la journalisation des fichiers texte est activée ou désactivée

Source Code:

<WriteActionModuleType ID="Forefront_Service_Control" Comment="{E85AB2D3-7F7D-4A37-9330-F0803AFB7DF7}" Accessibility="Internal" Batching="false">
<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 Isolation="Any">
<Composite>
<MemberModules>
<WriteAction ID="RunScriptAction" TypeID="MomBackwardCompatibility!System.Mom.BackwardCompatibility.NoHost.ScriptResponse">
<AlertGeneration>$Config/AlertGeneration$</AlertGeneration>
<InvokerType>$Config/InvokerType$</InvokerType>
<Body><Script>
' VBScript File

'*******************************************************************************************
'*
'* 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 =&gt; defines the control function to be performed on the services. Possibles
'* values are start ; stop ; restart ; disable ; enable
'* 2. Mode =&gt; defines the mode of the function possible values 1; 2
'* 3. TextLog =&gt; 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

' 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; 2007 = ????
' Determine Exchange version -&gt; 2003 = 0x00000041 65
' Determine Exchange version -&gt; 2000 = 0x0000003c 60


ForefrontInstallPath = RetrieveRegValue (REG_KEY &amp; "Exchange Server","InstalledPath", 1)
if IsNull(ForefrontInstallPath) then
ForefrontInstallPath = RetrieveRegValue (REG_KEY_64 &amp; "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 = " &amp; 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 " &amp; FirstDot
If FirstDot&gt;0 Then
ForefrontVersion=Left(FSCStartVersion,FirstDot-1)
'Else
' ForefrontVersion=10
End If


WriteLog "Detect FSCStarter version = " &amp; ForefrontVersion


' 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

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 " &amp; FunctionParameter &amp; " Function,Mode " &amp; ModeParameter &amp; " DependancyFlag " &amp; boolDependancyFlag


Dim boolServiceStateFlag
Dim strParam
Dim strCommand

Select Case FunctionParameter
Case "stop"
StopServicesList boolDependancyFlag

Case "start"
StartServicesList boolDependancyFlag

Case "restart"
StopServicesList boolDependancyFlag
StartServicesList boolDependancyFlag

Case "disable"
StopServicesList boolDependancyFlag
FSCUtilityHook ForefrontInstallPath, "disable"
Wait 2
StartServicesList boolDependancyFlag

Case "enable"
StopServicesList boolDependancyFlag
FSCUtilityHook ForefrontInstallPath, "enable"
Wait 2
StartServicesList boolDependancyFlag

End Select

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



'***********************************************************************
'*
'* Function: FSCUtilityMode()
'*
'* Purpose: Sets up call to FSCUtility for switching hooking mode
'*
'***********************************************************************

Function FSCUtilityMode (ForefrontInstallPath, HookMode)

Dim strCommand
Dim strParam

strCommand = """" &amp; ForefrontInstallPath &amp; "\FSCUtility.exe" &amp; """"
strParam = " /mode " &amp; Hookmode

WriteLog "Running FSCUtility to switch Forefront hooking mode to " &amp; 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 = """" &amp; ForefrontInstallPath &amp; "\FSCUtility.exe" &amp; """"
strParam = " /" &amp; Hookmode

WriteLog "Running FSCUtility to " &amp; Hookmode &amp; " 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 &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

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 12 Monitoring
StopService "MSExchangeMonitoring"

' Exchange 12 System Attendant
StopService "MSExchangeSA"

' Exchange 12 Mailbox Assistants
StopService "MSExchangeMailboxAssistants"

' 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 &lt;&gt; "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 Mailbox Assistant
StartService "MSExchangeMailboxAssistants"

' Exchange 12 Mail Submission Service
StartService "MSExchangeMailSubmission"

' Exchange 12 Information Store
StartService "MSExchangeIS"

' Exchange 12 Management
StartService "MSExchangeMGMT"

' Exchange 12 Monitoring
StartService "MSExchangeMonitoring"

' 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: " &amp; 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 &lt;&gt; 0 or IsNull(colListOfServices)) Then
WriteLog "ERROR: Could not connect to Service: " &amp; strService
Else
WriteLog "Success connect to Service: " &amp; strService
For Each refService in colListOfServices
WriteLog "Check Service" &amp; refService.Name &amp; " Status"
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()

Select Case intstop
Case 0
WMIChangeEvent refService.Name, "Stopped"
WriteLog "The service: " &amp; refService.Name &amp; " has Stopped"
WMIChangeEvent refService.Name, "Stopped"
Case 1
WriteLog "The service: " &amp; refService.Name &amp; " Not Supported"
Case 2
WriteLog "The service: " &amp; refService.Name &amp; " Access denied"
Case 3
WriteLog "The service: " &amp; refService.Name &amp; " Dependent services running."
case Else
WriteLog "unable to stop the service: " &amp; 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 &lt;&gt; 0 or IsNull(colListOfServices)) Then

WriteLog "ERROR: Could not connect to Service: " &amp; strService
Else

WriteLog "Success connect to Service: " &amp; strService
For Each refService in colListOfServices

WriteLog "Check Service" &amp; refService.Name &amp; " Status"

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

Next

End If

Set refWMI = Nothing
Set refService = Nothing
set colListOfServices=NoThing

End Function


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

Function CreateLogFolder (ForefrontInstallPath)

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

If objFSO.FolderExists(ForefrontInstallPath &amp; "\MOMLogs") = 0 then
objFSO.CreateFolder(ForefrontInstallPath &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(ForefrontInstallPath &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(ForefrontInstallPath &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

WriteLog "Ready to retrieve file version of " &amp; strPath &amp; strFile

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)

Dim colListOfServices

WriteLog "Try to change WMI Event " &amp; 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 &lt;&gt; 0 or IsNull(colListOfServices)) Then
WriteLog "ERROR: Could not connect to Service: " &amp; ServiceName
Else

WriteLog "Success connect to Service: " &amp; ServiceName
For Each refService in colListOfServices

WriteLog "Check Service" &amp; refService.Name &amp; " Status"
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
Next
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>Forefront 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>[FSMPack2007_FSE,,1.0.0.1]</ManagementPackId>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="RunScriptAction"/>
</Composition>
</Composite>
</ModuleImplementation>
<InputType>SystemLibrary!System.BaseData</InputType>
</WriteActionModuleType>