Now we have all needed function (see PART I of this article), we can create a new Management Pack by using the authoring console.
Create a new MP with the Authoring Console : MyNewMP.xml witha "Microsoft.Windows.Local Application" Class nammed MyNewMP.CLS1.
Add a new property to this class : DebugMod
Then create a new discovery MyNewMP.DSC1 that target Microsoft.Windows.Server.Computer and discover the class MyNewMP.CLS1 and all its attributes.
Choose the disovery type : Microsoft.Windows.TimedScript.DiscoveryProvider and schedule it once a day and call your script MyNewMP.DSC1.script.vbs. Click on Parameters and add 3 parameters like :
$MPElement$ $Target/Id$ $Target/Property[Type="Windows!Microsoft.Windows.Computer"]/PrincipalName$
In the Script field you will have to paste the discovery script below :
- First part of the discovery script is to declare and set the variable
- Option Explicit
- SetLocale("en-us")
- '-----------------------------------------------------------------------------------------------------
- ' DEFAULT VARIABLES AND CONST - Used by the debuging functions and sub
- '-----------------------------------------------------------------------------------------------------
- Const REGKEYPATH = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft Operations Manager\Debugging"
- Const REGISTRYDEFVALUE = "FALSE"
- Const MOMEVENTLOGERROR = "1"
- Const MOMEVENTLOGWARNING = "2"
- Const MOMEVENTLOGINFORMATION = "4"
- Dim intIndex
- Dim strRegValue
- Dim strRegKeyName
- Dim strMPScriptName
- Dim strCurrentDebugModeValue
- Dim oAPI
- Dim oInst
- Dim oArgs
- Dim oDiscoveryData
- '-----------------------------------------------------------------------------------------------------
- ' SET THE FOLLOWING VALUES ACCORDING TO YOUR MANAGEMENT PACK
- '----------------------------------------------------------------------------------------------------
- strMPScriptName = "MyNewMP.DSC1.vbs"
- strRegKeyName = "Verbose.MyNewMP"
- '-----------------------------------------------------------------------------------------------------
- ' Create the object that connect to MOM API
- Set oAPI = CreateObject("MOM.ScriptAPI")
The second part of the discovery will be dedicated to all function seens in Part I of this article :
- '-----------------------------------------------------------------------------------------------------
- ' DEFAULT FUNCTIONS - Used for Management Pack debugging
- '-----------------------------------------------------------------------------------------------------
- Private Function fct_ReadRegistryKey(ByVal strKeyPath, ByVal strKeyName)
- 'Read Registry key located on KeyPath\KeyName
- 'Returns the value of a registry key and eventually a no-value to identify that the key does not exists.
- Dim oWshShell
- Dim strRegReadValue 'Contains the value of the Registry Key
- 'Try to read the registry key located to strKeyPath\strKeyName
- On Error Resume Next
- Set oWshShell = CreateObject("Wscript.Shell")
- strRegReadValue = oWshShell.RegRead(strKeyPath &"\" & strKeyName)
- 'If an error is raised Then we a no-value to identify that the Registry Key does not exist.
- If err.number <> 0 Then
- strRegReadValue = ""
- End if
- On error goto 0
- fct_ReadRegistryKey = UCase(strRegReadValue)
- End Function
- '-----------------------------------------------------------------------------------------------------
- Private Function fct_WriteRegistryKey(ByVal strKeyPath, ByVal strKeyName, ByVal strKeyValue)
- ' Write a Registry value to strKeyPath\strKeyName with value strKeyValue
- ' blnKeyEdit is set to True or false in case of error.
- Dim blnKeyEdit
- Dim oWshShell
- blnKeyEdit = false
- 'Try to set strKeyValue to the registry key strKeyPath\strKeyName
- On Error Resume Next
- Set oWshShell = WScript.CreateObject("WScript.Shell")
- oWshShell.RegWrite strKeyPath & "\" & strKeyName , UCase(strKeyValue)
- 'If setting the registry key strKeyValue is OK Then the function will return a string TRUE.
- If err.number = 0 Then
- blnKeyEdit = true
- End if
- On error goto 0
- fct_WriteRegistryKey = blnKeyEdit
- End Function
- '-----------------------------------------------------------------------------------------------------
- Private Sub sub_LogMPScriptEvent(ByVal intErrNumber, ByVal strEventLogMessage, ByRef objError, ByVal strDebugMod, ByVal strScriptName, ByVal strMomEventLevel)
- ' Log an event in the OperationManager eventLog of the server if it is in debug mode
- ' It will allow to trace the debugging process of the script of the Management Pack by logging errors and parameters values
- Dim strMessage
- strMessage=""
- 'If the debug mode is ON, an event is logged in the Operation Manager eventLog.
- If ucase(strDebugMod) = "TRUE" Then
- strMessage = strEventLogMessage & vbCrLf & " " & vbCrLf & _
- "Error number:" & vbTab & CStr(objError.Number) & vbCrLf & _
- "Error description:" & vbTab & objError.Description
- Call oAPI.LogScriptEvent(strScriptName, intErrNumber, strMomEventLevel, strMessage)
- End if
- End Sub
- '-----------------------------------------------------------------------------------------------------
- Private function fct_LogRunningAccount(ByVal strDebugMode)
- ' Log an event in the OperationManager eventLog of the server if it is in debug mode
- ' It allows to know under which credentials the script is executed
- Dim owshNetwork
- Dim strRunningUserID
- On Error Resume Next
- Set owshNetwork = WScript.CreateObject("WScript.Network")
- 'Assign user name returned to a variable
- strRunningUserID = owshNetwork.UserName
- call sub_LogMPScriptEvent (4005, "sub_LogMPScriptEvent. Script running as user: " & strRunningUserID , Err, strDebugMode,strMPScriptName,MOMEVENTLOGINFORMATION)
- On error goto 0
- fct_LogRunningAccount = strRunningUserID
- End Function
- '-----------------------------------------------------------------------------------------------------
- private sub sub_LogScriptStartInfo(byRef oArgs, byVal strDebugModeValue, byval strMomLevelEvent, byVal strScriptName)
- ' Log an event with the default informations. The event contains :
- ' - The account used to run the script
- ' - The number of script parameters
- ' - The values of the parameters
- ' - The value of the debug mode
- Dim owshNetwork
- Dim strLogMessage
- Dim strRunAsAccount
- On Error Resume Next
- Set owshNetwork = WScript.CreateObject("WScript.Network")
- 'Assign user name returned to a variable
- strRunAsAccount = owshNetwork.UserName
- strLogMessage=""
- ' Create the start log Script with launch information data RunAs account, debugmode, arguments)
- strLogMessage = vbcrlf & "The script has been launched under following credentials : " & strRunAsAccount & "." & vbcrlf & vbcrlf & oArgs.count & " arguments have been passed in parameter : "
- For intIndex = 0 To oArgs.Count-1
- strLogMessage = strLogMessage & vbcrlf & " - " & oArgs(intIndex)
- Next
- strLogMessage = strLogMessage & vbcrlf & vbcrlf & "The debug mode value for the Management Pack is set to : " & strDebugModeValue
- Call oAPI.LogScriptEvent(strScriptName, 10212, strMomLevelEvent, strLogMessage)
- End sub
- '-----------------------------------------------------------------------------------------------------
And the next part of the discovery will be the main part that will test the number of arguments and create / read the debugging registry key :
- '-----------------------------------------------------------------------------------------------------
- ' MAIN CODE START HERE
- '-----------------------------------------------------------------------------------------------------
- ' Gets the arguments passed in parameters of the script
- Set oArgs = Wscript.Arguments
- ' If the minimals arguments are not set then we exit the script
- if oArgs.Count < 3 Then
- Wscript.Quit -1
- End If
- ' Check the value of the registry key on the server to know if the debug mode is ON or not
strRegValue = ""
- 'By default the registry key is set to : DOES NOT EXIST. See fct_ReadRegistryKey for more information
- strRegValue = fct_ReadRegistryKey(REGKEYPATH, strRegKeyName)
- 'If the registry key is TRUE XOR FALSE then the debugmodevalue has a good value
- If ((strRegValue = "TRUE") Xor (strRegValue = "FALSE")) Then
- strCurrentDebugModeValue = strRegValue
- Else
- 'If the registry key is empty or not correctly set then a attempt to set to default value is made.
- If fct_WriteRegistryKey(REGKEYPATH, strRegKeyName, REGISTRYDEFVALUE) Then
- ' write event 10211 that says the registry key has been created
- call sub_LogMPScriptEvent (10211, "Registry Key " & REGKEYPATH & "\" & strRegKeyName & " With Value = " & REGISTRYDEFVALUE & " has been created", Err, True,strMPScriptName,MOMEVENTLOGINFORMATION)
- Else
- ' write event 10210 that says the registry key has not been created
- call sub_LogMPScriptEvent (10210, "Error in creation of the Registry Key " & REGKEYPATH & "\" & strRegKeyName & " With Value = " & REGISTRYDEFVALUE , Err, True,strMPScriptName,MOMEVENTLOGERROR)
- End if
- strCurrentDebugModeValue = REGISTRYDEFVALUE
- End if The discovery itself : 'Return Data to the Management PackCall oDiscoveryData.AddInstance(oInst)
Call oAPI.Return(oDiscoveryData)
- Dim intSourceType
- Dim strSourceId0
- Dim strTargetComputer
- Dim strManagedEntityId
- intSourceType =
- 'Set the arguments for creating the default Management Pack modules.strSourceId = oArgs(0)
- strManagedEntityId = oArgs(1)
- strTargetComputer = oArgs(2)
- 'Create the default management pack classSet oDiscoveryData = oAPI.CreateDiscoveryData(intSourceType, strSourceId, strManagedEntityId)
- Set oInst = oDiscoveryData.CreateClassInstance("$MPElement[Name='MyNewMP.CLS1']$")
- call oInst.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", strTargetComputer)
- call oInst.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", "MyNewMP")
- call oInst.AddProperty("$MPElement[Name='MyNewMP.CLS1']/DebugMod$", strCurrentDebugModeValue)
- call sub_LogMPScriptEvent (10213, "Server "& strTargetComputer & " has been discovered. The debug mode value on this server is set to " & strCurrentDebugModeValue, Err, strCurrentDebugModeValue,strMPScriptName,MOMEVENTLOGINFORMATION)
- '-----------------------------------------------------------------------------------------------------
- ' PUT YOUR DISCOVERY CODE AFTER
- '-----------------------------------------------------------------------------------------------------
- 'Return Data to the Management PackCall oDiscoveryData.AddInstance(oInst)
- Call oAPI.Return(oDiscoveryData)
'Log the Management Pack default informations with runas account, debugmode and parameters info call sub_LogScriptStartInfo(oArgs, strCurrentDebugModeValue, MOMEVENTLOGINFORMATION, strMPScriptName)
How to use the debugging functionnality will be explained in PART III
End of PART II
Back to Part I Go to Part III
This posting is provided "AS IS" with no warranties.
No comments:
Post a Comment