Thursday, December 1, 2011

[SCOM 2007] Authoring Management Pack - Create a VBS discovery with a debugging functionnality - PART II

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 :

  1. First part of the discovery script is to declare and set the variable
  2. Option Explicit
  3. SetLocale("en-us")
  4. '-----------------------------------------------------------------------------------------------------
  5. ' DEFAULT VARIABLES AND CONST - Used by the debuging functions and sub
  6. '-----------------------------------------------------------------------------------------------------
  7. Const REGKEYPATH = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft Operations Manager\Debugging"
  8. Const REGISTRYDEFVALUE = "FALSE"
  9. Const MOMEVENTLOGERROR = "1"
  10. Const MOMEVENTLOGWARNING = "2"
  11. Const MOMEVENTLOGINFORMATION = "4"
  12. Dim intIndex
  13. Dim strRegValue
  14. Dim strRegKeyName
  15. Dim strMPScriptName
  16. Dim strCurrentDebugModeValue
  17. Dim oAPI
  18. Dim oInst
  19. Dim oArgs
  20. Dim oDiscoveryData
  21. '-----------------------------------------------------------------------------------------------------
  22. ' SET THE FOLLOWING VALUES ACCORDING TO YOUR MANAGEMENT PACK
  23. '----------------------------------------------------------------------------------------------------
  24. strMPScriptName = "MyNewMP.DSC1.vbs"
  25. strRegKeyName = "Verbose.MyNewMP"
  26. '-----------------------------------------------------------------------------------------------------
  27. ' Create the object that connect to MOM API
  28. Set oAPI = CreateObject("MOM.ScriptAPI")
The second part of the discovery will be dedicated to all function seens in Part I of this article :

  1. '-----------------------------------------------------------------------------------------------------
  2. ' DEFAULT FUNCTIONS - Used for Management Pack debugging
  3. '-----------------------------------------------------------------------------------------------------
  4. Private Function fct_ReadRegistryKey(ByVal strKeyPath, ByVal strKeyName)
  5. 'Read Registry key located on KeyPath\KeyName
  6. 'Returns the value of a registry key and eventually a no-value to identify that the key does not exists.
  7.            Dim oWshShell
  8.             Dim strRegReadValue     'Contains the value of the Registry Key
  9.             'Try to read the registry key located to strKeyPath\strKeyName
  10.             On Error Resume Next
  11.             Set oWshShell = CreateObject("Wscript.Shell")
  12.             strRegReadValue = oWshShell.RegRead(strKeyPath &"\" & strKeyName)
  13.             'If an error is raised Then we a no-value to identify that the Registry Key does not exist.
  14.             If err.number <> 0 Then
  15.                         strRegReadValue = ""
  16.             End if
  17.             On error goto 0
  18.             fct_ReadRegistryKey = UCase(strRegReadValue)
  19. End Function
  20. '-----------------------------------------------------------------------------------------------------
  21. Private Function fct_WriteRegistryKey(ByVal strKeyPath, ByVal strKeyName, ByVal strKeyValue)
  22. ' Write a Registry value to strKeyPath\strKeyName with value strKeyValue
  23. ' blnKeyEdit is set to True or false in case of error.
  24.             Dim blnKeyEdit
  25.             Dim oWshShell
  26.             blnKeyEdit = false
  27.             'Try to set strKeyValue to the registry key strKeyPath\strKeyName
  28.             On Error Resume Next
  29.             Set oWshShell = WScript.CreateObject("WScript.Shell")
  30.             oWshShell.RegWrite strKeyPath & "\" & strKeyName , UCase(strKeyValue)
  31.             'If setting the registry key strKeyValue is OK Then the function will return a string TRUE.
  32.             If err.number = 0 Then
  33.                         blnKeyEdit = true
  34.             End if
  35.             On error goto 0
  36.             fct_WriteRegistryKey = blnKeyEdit
  37. End Function
  38. '-----------------------------------------------------------------------------------------------------
  39. Private Sub sub_LogMPScriptEvent(ByVal intErrNumber, ByVal strEventLogMessage, ByRef objError, ByVal strDebugMod, ByVal strScriptName, ByVal strMomEventLevel)
  40. ' Log an event in the OperationManager eventLog of the server if it is in debug mode
  41. ' It will allow to trace the debugging process of the script of the Management Pack by logging errors and parameters values
  42.             Dim strMessage
  43.              strMessage=""
  44.              'If the debug mode is ON, an event is logged in the Operation Manager eventLog.
  45.             If ucase(strDebugMod) = "TRUE" Then
  46.                         strMessage = strEventLogMessage & vbCrLf & " " & vbCrLf & _
  47.                         "Error number:" & vbTab & CStr(objError.Number) & vbCrLf & _ 
  48.                         "Error description:" & vbTab & objError.Description
  49.             Call oAPI.LogScriptEvent(strScriptName, intErrNumber, strMomEventLevel, strMessage)
  50.             End if
  51. End Sub
  52. '-----------------------------------------------------------------------------------------------------
  53. Private function fct_LogRunningAccount(ByVal strDebugMode)
  54. ' Log an event in the OperationManager eventLog of the server if it is in debug mode
  55. ' It allows to know under which credentials the script is executed
  56.             Dim owshNetwork
  57.             Dim strRunningUserID
  58.             On Error Resume Next
  59.             Set owshNetwork = WScript.CreateObject("WScript.Network")
  60.             'Assign user name returned to a variable
  61.             strRunningUserID = owshNetwork.UserName
  62.             
  63.             call sub_LogMPScriptEvent (4005, "sub_LogMPScriptEvent. Script running as user: " & strRunningUserID , Err, strDebugMode,strMPScriptName,MOMEVENTLOGINFORMATION)
  64.             On error goto 0
  65.             fct_LogRunningAccount = strRunningUserID
  66. End Function
  67. '-----------------------------------------------------------------------------------------------------
  68. private sub sub_LogScriptStartInfo(byRef oArgs, byVal strDebugModeValue, byval strMomLevelEvent, byVal strScriptName)
  69. ' Log an event with the default informations. The event contains :
  70. ' - The account used to run the script
  71. ' - The number of script parameters
  72. ' - The values of the parameters
  73. ' - The value of the debug mode
  74.             Dim owshNetwork
  75.             Dim strLogMessage
  76.             Dim strRunAsAccount
  77.             On Error Resume Next
  78.             Set owshNetwork = WScript.CreateObject("WScript.Network")
  79.             'Assign user name returned to a variable
  80.             strRunAsAccount = owshNetwork.UserName
  81.              strLogMessage=""
  82.             ' Create the start log Script with launch information data RunAs account, debugmode, arguments)
  83.             strLogMessage = vbcrlf & "The script has been launched under following credentials : " &             strRunAsAccount & "." & vbcrlf & vbcrlf & oArgs.count & " arguments have been passed in parameter : "
  84.             For intIndex = 0 To oArgs.Count-1
  85.                         strLogMessage = strLogMessage & vbcrlf & " - " & oArgs(intIndex)
  86.             Next
  87.             strLogMessage = strLogMessage & vbcrlf & vbcrlf & "The debug mode value for the Management Pack is set to : " & strDebugModeValue
  88.             Call oAPI.LogScriptEvent(strScriptName, 10212, strMomLevelEvent, strLogMessage)
  89. End sub
  90. '-----------------------------------------------------------------------------------------------------
 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 :
  1. '-----------------------------------------------------------------------------------------------------
  2. ' MAIN CODE START HERE
  3. '-----------------------------------------------------------------------------------------------------
  4. ' Gets the arguments passed in parameters of the script
  5. Set oArgs = Wscript.Arguments
  6. ' If the minimals arguments are not set then we exit the script
  7. if oArgs.Count < 3 Then
  8.    Wscript.Quit -1
  9. End If
  10. ' Check the value of the registry key on the server to know if the debug mode is ON or not
strRegValue = ""
  1. 'By default the registry key is set to : DOES NOT EXIST. See fct_ReadRegistryKey for more information
  2. strRegValue = fct_ReadRegistryKey(REGKEYPATH, strRegKeyName)
  3. 'If the registry key is TRUE XOR FALSE then the debugmodevalue has a good value
  4. If ((strRegValue = "TRUE") Xor (strRegValue = "FALSE")) Then
  5.             strCurrentDebugModeValue = strRegValue
  6. Else
  7.             'If the registry key is empty or not correctly set then a attempt to set to default value is made.
  8.             If fct_WriteRegistryKey(REGKEYPATH, strRegKeyName, REGISTRYDEFVALUE) Then
  9.                         ' write event 10211 that says the registry key has been created
  10.                         call sub_LogMPScriptEvent (10211, "Registry Key " & REGKEYPATH & "\" & strRegKeyName & " With Value = " & REGISTRYDEFVALUE & " has been created", Err, True,strMPScriptName,MOMEVENTLOGINFORMATION)
  11.             Else
  12.                         ' write event 10210 that says the registry key has not been created
  13.                         call sub_LogMPScriptEvent (10210, "Error in creation of the Registry Key " & REGKEYPATH & "\" & strRegKeyName & " With Value = " & REGISTRYDEFVALUE , Err, True,strMPScriptName,MOMEVENTLOGERROR)
  14.             End if
  15.             strCurrentDebugModeValue = REGISTRYDEFVALUE
  16. End if The discovery itself : 'Return Data to the Management PackCall oDiscoveryData.AddInstance(oInst)
Call oAPI.Return(oDiscoveryData)
  1.  
  2. Dim intSourceType
  3. Dim strSourceId0
  4. Dim strTargetComputer
  5. Dim strManagedEntityId
  6. intSourceType =
  7. 'Set the arguments for creating the default Management Pack modules.strSourceId = oArgs(0)
  8. strManagedEntityId = oArgs(1)
  9. strTargetComputer = oArgs(2)
  10. 'Create the default management pack classSet oDiscoveryData = oAPI.CreateDiscoveryData(intSourceType, strSourceId, strManagedEntityId)
  11. Set oInst = oDiscoveryData.CreateClassInstance("$MPElement[Name='MyNewMP.CLS1']$")
  12. call oInst.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", strTargetComputer)
  13. call oInst.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", "MyNewMP")
  14. call oInst.AddProperty("$MPElement[Name='MyNewMP.CLS1']/DebugMod$", strCurrentDebugModeValue)
  15.  
  16. call sub_LogMPScriptEvent (10213, "Server "& strTargetComputer & " has been discovered. The debug mode value on this server is set to " & strCurrentDebugModeValue, Err, strCurrentDebugModeValue,strMPScriptName,MOMEVENTLOGINFORMATION)
  17. '-----------------------------------------------------------------------------------------------------
  18. ' PUT YOUR DISCOVERY CODE AFTER
  19. '-----------------------------------------------------------------------------------------------------
  20. 'Return Data to the Management PackCall oDiscoveryData.AddInstance(oInst)
  21. 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