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

Element properties:

TypeWriteActionModuleType
IsolationAny
AccessibilityPublic
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$
MPNetBIOSNamestring$Config/MPNetBIOSName$
SMSSiteCodestring$Config/SMSSiteCode$
TimeoutSecondsint$Config/TimeoutSeconds$

Source Code:

<WriteActionModuleType ID="EDC.Base.SMSUpdateLocalMP.Script.WriteAction" Accessibility="Public" 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="MPNetBIOSName" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="SMSSiteCode" 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="MPNetBIOSName" Selector="$Config/MPNetBIOSName$" ParameterType="string"/>
<OverrideableParameter ID="SMSSiteCode" Selector="$Config/SMSSiteCode$" 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>SMSUpdateLocalMP.vbs</ScriptName>
<Arguments>$Config/Debug$ "$Config/DebugFileName$" $Config/LogSuccessEvent$ "$Config/ScriptGroupId$" $Config/MPNetBIOSName$ $Config/SMSSiteCode$</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.
'
'***********************************************************************************************************
'* SMSUpdateLocalMP.vbs 1.2
'*
'* This sample shows how to update the CurrentManagementPoint property of the CCM's SMS_Authority if necessary
'*
'* Version for Operations Manager 2007
'*
'* JM
'***********************************************************************************************************
'Const SCRIPT_NAME = "SMSUpdateLocalMP.vbs"
Const SCRIPT_VERSION = "1.2"

Const CCM_WMI_NAME_SPACE = "root\ccm"
Const SMS_AUTHORITY_WMI_PATH = "SMS_Authority"
Const SMS_AUTHORITY_NAME_PREFIX = "SMS:"
Const SMS_LOCAL_MP_WMI_PATH = "SMS_LocalMP=@"
Const MASTER_SITE_CODE_PROPERTY_NAME = "MasterSiteCode"
Const NAME_PROPERTY_NAME = "Name"
Const SITE_CODE_PROPERTY_NAME = "SiteCode"

Const SITE_CODE_PARAMETER_NAME = "SMSSiteCode"
Const MP_NETBIOS_NAME_PARAMETER_NAME = "MPNetBIOSName"
Const LOG_SUCCESS_EVENT_PARAMETER_NAME = "LogSuccessEvent"
Const DEBUG_PARAMETER_NAME = "Debug"
Const DEBUG_FILE_NAME_PARAMETER_NAME = "DebugFileName"

Const HKEY_CLASSES_ROOT = &amp;H80000000
Const HKEY_CURRENT_USER = &amp;H80000001
Const HKEY_LOCAL_MACHINE = &amp;H80000002
Const HKEY_USERS = &amp;H80000003
Const HKEY_CURRENT_CONFIG = &amp;H80000005

'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 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.

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

Call Main

'*********************************************************************************************
' PROCEDURE: Main
' DESCRIPTION: Reads the script parameters and updates the SMS_LocalMP default instance
' within the WMI namespace root\ccm.
' Depending on the outcome success or error events are created,
' respectively.
' PARAMETERS: void
'*********************************************************************************************
Private Sub Main()

Dim objFSO 'As Scripting.FileSystemObject
Dim strMPNetBIOSName 'As String
Dim strSMSSiteCode 'As String
Dim objMomScriptAPI

Call SetLocale("en-us")

Set objMomScriptAPI = CreateObject("MOM.ScriptAPI")


If Not GetScriptParameters(strMPNetBIOSName, strSMSSiteCode) Then
' If the script is called without the required arguments,
' create an information event and then quit.
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 five arguments or the arguments could not be parsed.")
WScript.Quit -1
End If

Set objFSO = CreateObject("Scripting.FileSystemObject")

'Create debug log and write some informations
If mblnDebug Then
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)
Call mobjDebugLog.WriteLine("Script parameters:" &amp; vbCrLf &amp; vbTab &amp; DEBUG_PARAMETER_NAME &amp; ": " &amp; CStr(mblnDebug) &amp; vbCrLf &amp; _
vbTab &amp; DEBUG_FILE_NAME_PARAMETER_NAME &amp; ": " &amp; mstrDebugFileName &amp; vbCrLf &amp; vbTab &amp; _
LOG_SUCCESS_EVENT_PARAMETER_NAME &amp; ": " &amp; CStr(mblnLogSuccessEvent) &amp; vbCrLf &amp; vbTab &amp; _
SITE_CODE_PARAMETER_NAME &amp; ": " &amp; strSMSSiteCode &amp; vbCrLf &amp; vbTab &amp; _
MP_NETBIOS_NAME_PARAMETER_NAME &amp; ": " &amp; strMPNetBIOSName &amp; vbCrLf)
End If
'Call main function
If UpdateCurrentMP(strMPNetBIOSName, strSMSSiteCode) Then
If mblnLogSuccessEvent Then Call objMomScriptAPI.LogScriptEvent(mstrIdentifier &amp; " -- " &amp; WScript.ScriptName &amp; " " &amp; SCRIPT_VERSION, EVENT_ID_SUCCESS, EVENT_TYPE_INFORMATION, _
"Script " &amp; WScript.ScriptName &amp; " " &amp; SCRIPT_VERSION &amp; " executed successfully.")
If mblnDebug Then Call mobjDebugLog.WriteLine("Script " &amp; WScript.ScriptName &amp; " " &amp; SCRIPT_VERSION &amp; " executed successfully.")
Else
Call objMomScriptAPI.LogScriptEvent(mstrIdentifier &amp; " -- " &amp; WScript.ScriptName &amp; " " &amp; SCRIPT_VERSION, EVENT_ID_SCRIPTERROR, EVENT_TYPE_WARNING, _
"An error occurred while running script " &amp; WScript.ScriptName &amp; " " &amp; SCRIPT_VERSION)
If mblnDebug Then Call mobjDebugLog.WriteLine("An error occurred while running script " &amp; WScript.ScriptName &amp; " " &amp; SCRIPT_VERSION)
End If

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

End Sub

'******************************************************************************
' FUNCTION: UpdateCurrentMP
' DESCRIPTION: Gets the SMS_LocalMP instance (singleton) and updates the
' properties Name, SiteCode and MasterSiteCode
' PARAMETERS: IN String strMPNetBIOSName: NetBIOS name of the MP which the SMS client
' shall use.
' IN String strSMSSiteCode: SMS site code of the MP
' RETURNS: Boolean: True if successful
'******************************************************************************
Private Function UpdateCurrentMP(ByRef strMPNetBIOSName, ByRef strSMSSiteCode) 'As Boolean

Dim objSWbemServices 'As SWbemServices
Dim objSWbemObjectSet 'As SWbemObjectSet
Dim objSWbemObject 'As SWbemObject
Dim strCurrentLocalMP 'As String
Dim strCurrentSiteCode 'As String
Dim objSWbemObjectPath 'As SWbemObjectPath
Dim blnContinue 'As Boolean

If mblnDebug Then Call mobjDebugLog.WriteLine("")

'Connect to WMI namespace \\.\root\ccm
If ConnectToWbemNS(".", CCM_WMI_NAME_SPACE, objSWbemServices) Then

'Check if the computer is really managed by the given SMS site.
'The name of the SMS_Authority must match "SMS:&lt;given site code&gt;", e.g. SMS:EDC or SMS:STS
If mblnDebug Then Call mobjDebugLog.WriteLine("Connected!" &amp; vbCrLf &amp; "Retrieving instances of '" &amp; SMS_AUTHORITY_WMI_PATH &amp; "'.")
Set objSWbemObjectSet = objSWbemServices.Get(SMS_AUTHORITY_WMI_PATH).Instances_
'If WMI objectset cannot be retrieved exit function
If Not IsObject(objSWbemObjectSet) Then
If mblnDebug Then Call mobjDebugLog.WriteLine("Error! Could not retrieve any instance. Exiting.")
Exit Function
End If
If mblnDebug Then Call mobjDebugLog.WriteLine("Retrieved " &amp; objSWbemObjectSet.Count &amp; " instances of '" &amp; SMS_AUTHORITY_WMI_PATH &amp; "'.")
For Each objSWbemObject In objSWbemObjectSet
If mblnDebug Then Call mobjDebugLog.WriteLine("Instance name: " &amp; objSWbemObject.Name &amp; vbCrLf &amp; vbTab &amp; _
"Comparing current value with the given site code '" &amp; strSMSSiteCode &amp; "'...")
If Not blnContinue And UCase(objSWbemObject.Name) = UCase(SMS_AUTHORITY_NAME_PREFIX &amp; strSMSSiteCode) Then
blnContinue = True
If mblnDebug Then Call mobjDebugLog.WriteLine(vbTab &amp; UCase(objSWbemObject.Name) &amp; "=" &amp; UCase(SMS_AUTHORITY_NAME_PREFIX &amp; strSMSSiteCode))
Else
If mblnDebug Then Call mobjDebugLog.WriteLine(vbTab &amp; UCase(objSWbemObject.Name) &amp; "&lt;&gt;" &amp; UCase(SMS_AUTHORITY_NAME_PREFIX &amp; strSMSSiteCode))
End If
Next

If Not blnContinue Then
If mblnDebug Then Call mobjDebugLog.WriteLine(vbCrLf &amp; "The computer is not managed by a the SMS site '" &amp; _
strSMSSiteCode &amp; "'. Do nothing and return True.")
UpdateCurrentMP = True
Exit Function
End If
If mblnDebug Then Call mobjDebugLog.WriteLine(vbCrLf &amp; "The computer is managed by the given site '" &amp; _
strSMSSiteCode &amp; "'. Continue..." &amp; vbCrLf &amp; vbCrLf &amp; _
"Retrieving WMI object '" &amp; SMS_LOCAL_MP_WMI_PATH &amp; "'.")

'Get default instance of SMS_LocalMP. (class is singleton)
Set objSWbemObject = objSWbemServices.Get(SMS_LOCAL_MP_WMI_PATH)
'If WMI cannot be retrieved exit function
If Not IsObject(objSWbemObject) Then
If mblnDebug Then Call mobjDebugLog.WriteLine("Error! Could not retrieve the WMI object. Exiting.")
Exit Function
End If

If mblnDebug Then Call mobjDebugLog.WriteLine("Retrieved '" &amp; SMS_LOCAL_MP_WMI_PATH &amp; "'." &amp; vbCrLf &amp; _
"Current values:" &amp; vbCrLf &amp; _
vbTab &amp; "Master site code: " &amp; objSWbemObject.MasterSiteCode &amp; vbCrLf &amp; _
vbTab &amp; "Site code: " &amp; objSWbemObject.SiteCode &amp; vbCrLf &amp; _
vbTab &amp; "Local MP: " &amp; objSWbemObject.Name &amp; vbCrLf)
'Update property values
objSWbemObject.MasterSiteCode = strSMSSiteCode
objSWbemObject.SiteCode = strSMSSiteCode
objSWbemObject.Name = strMPNetBIOSName
'Save changes
Set objSWbemObjectPath = objSWbemObject.Put_
'Return true if save was successful
If IsObject(objSWbemObjectPath) Then
If mblnDebug Then Call mobjDebugLog.WriteLine("Updated values:" &amp; vbCrLf &amp; _
vbTab &amp; "Master site code: " &amp; objSWbemObject.MasterSiteCode &amp; vbCrLf &amp; _
vbTab &amp; "Site code: " &amp; objSWbemObject.SiteCode &amp; vbCrLf &amp; _
vbTab &amp; "Local MP: " &amp; objSWbemObject.Name &amp; vbCrLf)
UpdateCurrentMP = True
End If

Else
'If WMI cannot be connected exit function
If mblnDebug Then Call mobjDebugLog.WriteLine("Error! Could not connect to WMI namespace. Terminating script!")
End If

End Function

'******************************************************************************
' FUNCTION: ConnectToWbemNS
' DESCRIPTION: Connnects to a WMI namespace
' PARAMETERS: IN String strServerName: host part of WMI namespace. If null the local computer will be used.
' IN String strNameSpace: path to namespace
' OUT Object objSWbemServices: SWbemServices object connected to the given namespace
' RETURNS: Boolean: True if successful
'******************************************************************************
Private Function ConnectToWbemNS(ByRef strServerName, ByRef strNameSpace, ByRef objSWbemServices) 'As Boolean


Dim objSWbemLocator 'as WbemScripting.SWbemLocator


Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")

Set objSWbemServices = objSWbemLocator.ConnectServer(strServerName, strNameSpace)

If objSWbemServices Is Nothing Then
ConnectToWbemNS = False
Else
ConnectToWbemNS = True
End If

End Function

'******************************************************************************
' FUNCTION: GetScriptParameters
' DESCRIPTION: Reads the script's parameters
' and sets the global variables.
'
' PARAMETERS: OUT String strMPNetBIOSName: NetBIOS name of the MP which the SMS client
' shall use.
' OUT String strSMSSiteCode: SMS site code of the MP
' RETURNS: Boolean: True if successful
'******************************************************************************
Private Function GetScriptParameters(ByRef strMPNetBIOSName, ByRef strSMSSiteCode) 'As Boolean

Dim objArguments

On Error Resume Next

Set objArguments = WScript.Arguments
If objArguments.Count &lt; 6 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), "")
strMPNetBIOSName = Replace(objArguments(4), Chr(34), "")
strSMSSiteCode = Replace(objArguments(5), Chr(34), "")

GetScriptParameters = True

End Function

'**************************************************************
' 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: True if successful
'**************************************************************
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
</Script></ScriptBody>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="Script"/>
</Composition>
</Composite>
</ModuleImplementation>
<InputType>System!System.BaseData</InputType>
</WriteActionModuleType>