EDC.Base.SetDefaultCategories.Script.WriteAction (WriteActionModuleType)

Element properties:

TypeWriteActionModuleType
IsolationAny
AccessibilityInternal
RunAsDefault
InputTypeSystem.BaseData

Member Modules:

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

Overrideable Parameters:

IDParameterTypeSelector
Debugbool$Config/Debug$
DebugFileNamestring$Config/DebugFileName$
LogSuccessEventbool$Config/LogSuccessEvent$
ScriptGroupIdstring$Config/ScriptGroupId$
TimeoutSecondsint$Config/TimeoutSeconds$

Source Code:

<WriteActionModuleType ID="EDC.Base.SetDefaultCategories.Script.WriteAction" Accessibility="Internal" Batching="false">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Debug" type="xsd:boolean"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="DebugFileName" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="LogSuccessEvent" type="xsd:boolean"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="ScriptGroupId" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="TimeoutSeconds" type="xsd:int"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="Debug" Selector="$Config/Debug$" ParameterType="bool"/>
<OverrideableParameter ID="DebugFileName" Selector="$Config/DebugFileName$" ParameterType="string"/>
<OverrideableParameter ID="LogSuccessEvent" Selector="$Config/LogSuccessEvent$" ParameterType="bool"/>
<OverrideableParameter ID="ScriptGroupId" Selector="$Config/ScriptGroupId$" ParameterType="string"/>
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" ParameterType="int"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<WriteAction ID="Script" TypeID="Windows!Microsoft.Windows.ScriptWriteAction">
<ScriptName>SetDefaultCategories.vbs</ScriptName>
<Arguments>$Config/Debug$ "$Config/DebugFileName$" $Config/LogSuccessEvent$ "$Config/ScriptGroupId$"</Arguments>
<ScriptBody><Script>
Option Explicit
'***********************************************************************************************************
' Disclaimer
'
' This sample script is not supported under any Microsoft standard support program or service. This sample
' script is provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties
' including, without limitation, any implied warranties of merchantability or of fitness for a particular
' purpose. The entire risk arising out of the use or performance of this sample script and documentation
' remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation,
' production, or delivery of this script be liable for any damages whatsoever (including, without limitation,
' damages for loss of business profits, business interruption, loss of business information, or other
' pecuniary loss) arising out of the use of or inability to use this sample script or documentation, even
' if Microsoft has been advised of the possibility of such damages.
'
'***********************************************************************************************************
' SetDefaultCategories.vbs 1.0 v20100526
'
' If the registry entries do not exist, create and write the default values to
' HKLM\SOFTWARE\EDC\OpsMgr07\EDCServerCategory
' HKLM\SOFTWARE\EDC\OpsMgr07\PrimaryCategory
'
' Version for Operations Manager 2007
'
' JM
'***********************************************************************************************************
'Script specific constants
'Const SCRIPT_NAME = ""
Const SCRIPT_VERSION = "1.0"
'Const DEBUG_FILE_NAME = "%WinDir%\Debug\SetDefaultCategories.log"

Const EDC_SERVER_CATEGORY_REGISTRY_PATH = "HKLM\SOFTWARE\EDC\OpsMgr07\EDCServerCategory"
Const DEFAULT_SERVER_CATEGORY = "default"
Const PRIMARY_CATEGORY_REGISTRY_PATH = "HKLM\SOFTWARE\EDC\OpsMgr07\PrimaryCategory"
Const DEFAULT_PRIMARY_CATEGORY = "default"

Const REG_SZ_TYPE = "REG_SZ"

'Event constants
Const EVENT_TYPE_SUCCESS = 0
Const EVENT_TYPE_ERROR = 1
Const EVENT_TYPE_WARNING = 2
Const EVENT_TYPE_INFORMATION = 4

Const EVENT_ID_SUCCESS = 1000 'Use IDs in the range 1 - 1000
Const EVENT_ID_SCRIPTERROR = 999 'Then you can use eventcreate.exe to test the MP
'Const EVENT_ID_PROCESSERROR = 998

'Other constants
Const FOR_READING = 1 'Open a file for reading only. You can't write to this file.
Const FOR_WRITING = 2 'Open a file for writing.
Const FOR_APPENDING = 8 'Open a file and write to the end of the file.

'Global variables
Dim mblnDebug 'As Boolean
Dim mstrDebugFileName 'As String
Dim mstrIdentifier 'As String
Dim mobjDebugLog 'As TextStream
Dim mblnLogSuccessEvent 'As Boolean

Call Main

'********************************************************************************
' PROCEDURE: Main
' DESCRIPTION: Read the arguments and check whether services are running.
' PARAMETERS: void
'******************************************************************************
Private Sub Main()

Dim objWSHShell 'As WScript.Shell
Dim strEDCSrvCategory 'As String
Dim strPrimaryCategory 'As String
Dim objFSO 'As Scripting.FileSystemObject
Dim blnScriptSuccess 'As Boolean
Dim objMomScriptAPI 'As MOM.ScriptAPI

Call SetLocale("en-us")

'On Error Resume Next

Set objMomScriptAPI = CreateObject("MOM.ScriptAPI")
Set objWSHShell = CreateObject("WScript.Shell")


'Read script parameters and set arrIncludeProcessesNames, strExcludeProcesses.
'Call GetScriptParameters
If Not GetScriptParameters() Then
' If the script is called without the required arguments,
' create an information event and then quit.
Call WScript.Echo("Error! The script was called with fewer than 4 arguments or the arguments could not be parsed.")
Call objMomScriptAPI.LogScriptEvent(mstrIdentifier &amp; " -- " &amp; WScript.ScriptName &amp; " " &amp; SCRIPT_VERSION, EVENT_ID_SCRIPTERROR, EVENT_TYPE_WARNING, _
"The script was called with fewer than 4 arguments or the arguments could not be parsed.")
WScript.Quit -1
End If

'Open the debug log file if required
If mblnDebug Then
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FolderExists(Left(mstrDebugFileName, InStrRev(mstrDebugFileName, "\") - 1)) Then Call MakeFolder(Left(mstrDebugFileName, InStrRev(mstrDebugFileName, "\") - 1), objFSO)
Set mobjDebugLog = objFSO.CreateTextFile(mstrDebugFileName, True)
Call mobjDebugLog.WriteLine("Script " &amp; WScript.ScriptName &amp; " " &amp; SCRIPT_VERSION &amp; " started -- " &amp; Now &amp; vbCrLf)
End If

On Error Resume Next
If mblnDebug Then Call mobjDebugLog.WriteLine("Read the registry value '" &amp; EDC_SERVER_CATEGORY_REGISTRY_PATH &amp; "'...")
strEDCSrvCategory = objWSHShell.RegRead(EDC_SERVER_CATEGORY_REGISTRY_PATH)
If mblnDebug Then Call mobjDebugLog.WriteLine("The read operation returned '" &amp; strEDCSrvCategory &amp; "'...")
If Len(strEDCSrvCategory) = 0 Then
'EDCServerCategory not already set. Continue.
If mblnDebug Then Call mobjDebugLog.WriteLine("The EDC Server Category is not set yet. Set the EDC Server Category to the default value '" &amp; DEFAULT_SERVER_CATEGORY &amp; "'...")
Call objWSHShell.RegWrite(EDC_SERVER_CATEGORY_REGISTRY_PATH, DEFAULT_SERVER_CATEGORY, REG_SZ_TYPE)
blnScriptSuccess = True
If mblnDebug Then Call mobjDebugLog.WriteLine("The EDC Server Category is now set to " &amp; objWSHShell.RegRead(EDC_SERVER_CATEGORY_REGISTRY_PATH) &amp; ". Continue...")
Else
'EDCServerCategory already set. Do nothing.
If mblnDebug Then Call mobjDebugLog.WriteLine("The EDC Server Category is already set. Continue...")
blnScriptSuccess = True
End If

If mblnDebug Then Call mobjDebugLog.WriteLine("Read the registry value '" &amp; PRIMARY_CATEGORY_REGISTRY_PATH &amp; "'...")
strPrimaryCategory = objWSHShell.RegRead(PRIMARY_CATEGORY_REGISTRY_PATH)
If mblnDebug Then Call mobjDebugLog.WriteLine("The read operation returned '" &amp; strPrimaryCategory &amp; "'...")
If Len(strPrimaryCategory) = 0 Then
'PrimaryCategory not already set. Continue.
If mblnDebug Then Call mobjDebugLog.WriteLine("The Primary Category is not set yet. Set the Primary Category to the default value '" &amp; DEFAULT_PRIMARY_CATEGORY &amp; "'...")
Call objWSHShell.RegWrite(PRIMARY_CATEGORY_REGISTRY_PATH, DEFAULT_PRIMARY_CATEGORY, REG_SZ_TYPE)
If mblnDebug Then Call mobjDebugLog.WriteLine("The Primary Category is now set to " &amp; objWSHShell.RegRead(PRIMARY_CATEGORY_REGISTRY_PATH) &amp; ".")
Else
'PrimaryCategory already set. Do nothing.
If mblnDebug Then Call mobjDebugLog.WriteLine("The Primary Category is already set. Return True.")
End If

If mblnDebug Then
Call mobjDebugLog.WriteLine("Script finished -- " &amp; Now)
Call mobjDebugLog.Close
End If

'Create an error or, if enabled, a success event if the script ran through as expected.
If mblnLogSuccessEvent And blnScriptSuccess Then
Call objMomScriptAPI.LogScriptEvent(mstrIdentifier &amp; " -- " &amp; WScript.ScriptName &amp; " " &amp; SCRIPT_VERSION, EVENT_ID_SUCCESS, EVENT_TYPE_INFORMATION, _
"The script '" &amp; WScript.ScriptName &amp; " " &amp; SCRIPT_VERSION &amp; "' completed successfully")
ElseIf Not blnScriptSuccess Then
Call objMomScriptAPI.LogScriptEvent(mstrIdentifier &amp; " -- " &amp; WScript.ScriptName &amp; " " &amp; SCRIPT_VERSION, EVENT_ID_SCRIPTERROR, EVENT_TYPE_WARNING, _
"The script '" &amp; WScript.ScriptName &amp; " " &amp; SCRIPT_VERSION &amp; "' failed.")
Exit Sub
End If

End Sub

'**************************************************************
' FUNCTION: MakeFolder
' DESCRIPTION: Creates a folder and all necessary parent
' folders.
' PARAMETERS: IN String strFolder: full path of the folder
' IN Object objFSO: Scripting.FileSystemObject object
' RETURNS: Boolean
'**************************************************************
Private Function MakeFolder(ByRef strFolder, ByRef objFSO) 'As Boolean

Dim strParentFolder 'As String

On Error Resume Next

'Get parent folder of given folder
strParentFolder = Left(strFolder, InStrRev(strFolder, "\") - 1)
'If the parent folder does not exist create it. (recursive call)
If Not objFSO.FolderExists(strParentFolder) Then Call MakeFolder(strParentFolder, objFSO)
'Create folder
Call objFSO.CreateFolder(strFolder)
'If error no equals zero function is successful
If Err.Number = 0 Then MakeFolder = True
Call Err.Clear

End Function

'******************************************************************************
' FUNCTION: GetScriptParameters
' DESCRIPTION: Reads the script's parameters
' and sets the global variables.
' PARAMETERS: void
' RETURNS: Boolean: True if successful
'******************************************************************************
Private Function GetScriptParameters() 'As Boolean

Dim objArguments 'As WScript.Arguments
Dim blnReplaceDblBackslashes 'As Boolean

On Error Resume Next

Set objArguments = WScript.Arguments
If objArguments.Count &lt; 4 Then Exit Function

'Get parameters and set global variables
mblnDebug = CBool(objArguments(0))
mstrDebugFileName = Replace(objArguments(1), Chr(34), "")
mblnLogSuccessEvent = CBool(objArguments(2))
mstrIdentifier = Replace(objArguments(3), Chr(34), "")

GetScriptParameters = True

End Function
</Script></ScriptBody>
<TimeoutSeconds>300</TimeoutSeconds>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="Script"/>
</Composition>
</Composite>
</ModuleImplementation>
<InputType>System!System.BaseData</InputType>
</WriteActionModuleType>