Menu Close

SCOM 101: Using custom scripts to write events to the OpsMgr Event Log with MOMScriptAPI.LogScriptEvent

When converting MOM 2005 scripts…. on of the common things to do is to change the way events are written.

In MOM 2005, we would most commonly use a custom method, ScriptContext.CreateEvent, which would create custom events via script in the MOM channel, that other event workflows could pick up on (Script Generated Data Source).

Alternatively – we would use the native VBscript methods to write a generic event to the application event log.

A GREAT write-up on this whole process – breaking it down step by step is located here:

Updating MOM 2005 Scripts for Operations Manager 2007:

This MOM 2005 method has been deprecated in OpsMgr 2007.  It still is possible to utilize via converted MOM 2005 MP’s… but the best practice is to convert these to native SCOM scripts, and use the MOMScriptAPI.LogScriptEvent method, which will write a real event to the OperationsManager event log.  You will see many Microsoft MP’s do this… such as the ADMP, DHCP, and Exchange MP’s, even the core test scripts.  Here are some examples:

Event Type:    Information
Event Source:    Health Service Script
Event Category:    None
Event ID:    6022
Date:        7/21/2009
Time:        8:00:03 PM
User:        N/A
Computer:    EX07A
LogEndToEndEvent.js : This event is logged to the Windows Event Log periodically to test a event collection.

Event Type:    Information
Event Source:    Health Service Script
Event Category:    None
Event ID:    19960
Date:        7/21/2009
Time:        11:06:30 PM
User:        N/A
Computer:    EXCH1
Check service(s) state : All specified services are running. List of specified services: MSExchangeIS, MSExchangeMGMT, MSExchangeMTA, MSExchangeSA, SMTPSVC, W3SVC

Event Type:    Information
Event Source:    Health Service Script
Event Category:    None
Event ID:    1112
Date:        7/21/2009
Time:        7:51:34 PM
User:        N/A
Computer:    DC01
DHCP2003ComponentDiscovery.vbs : Discovery of DHCP components started. Portion 1

The MOMScriptAPI.LogScriptEvent method is documented here:

To start – I am just going to write a timed event rule, which will run a script that will create a simple event every minute – using the sample provided at the link above.

New Rule > Timed Commands > Execute a Script

Give it a Rule name according to your documented Rule naming standard.  I will target my favorite generic target – “Windows Server Operating System”.

Set it to run every 60 seconds (only for testing in a lab!!!)

For the File Name – I am using LogScriptEvent.vbs, set the timeout to 30 seconds (less than the interval!) and paste in the sample script:

Option Explicit
Dim oAPI, oArgs
Set oAPI = CreateObject("MOM.ScriptAPI")
Set oArgs = WScript.Arguments

' Check to see if the required script arguments are there.
' In this example, at least three arguments are required.
' If the arguments do not exist, log a script event.
If oArgs.Count <3 Then
    ' If the script is called without the required arguments,
    ' create an information event and then quit.
    Call oAPI.LogScriptEvent("LogScriptEvent.vbs", 101, 0, "LogScriptEvent script was called with fewer than three arguments and was not executed.")
    WScript.Quit -1
End If

' When it passes the required arguments check satisfactorily, the
' remaining script, located here, is run.

All this script does – is log an error – if there not at least 3 arguments (script parameters) passed to the script – it logs an error in the OpsMgr event log.

Create it, and here is what I start seeing every minute in my OpsMgr event log:

Log Name:          Operations Manager
Source:               Health Service Script

Date:                   7/21/2009 11:32:15 PM

Event ID:            101

Task Category:   None

Level:                 Information

Keywords:          Classic

User:                  N/A



LogScriptEvent.vbs : LogScriptEvent script was called with fewer than three arguments and was not executed.

So – it works very simply.  See the link above for what you can customize with this.  Now – any custom VBscript you like – you can take the output of those scripts, and log events using OpsMgr.

What is even cooler… is testing your SCOM scripts is easier than ever… you can simply run them on any machine that has an OpsMgr agent installed.  If they submit data – such as propertybags, you will see those in the command window.  If they log events – they will log the event immediately in the OpsMgr event log.

Here is another very simple script… which will take the script parameters as arguments, and log them in the event description:

Option Explicit
Dim oAPI, oArgs

Set oAPI = CreateObject(“MOM.ScriptAPI”)

Set oArgs = WScript.Arguments

Call oAPI.LogScriptEvent(“LogScriptEventArgs.vbs”, 102, 2, oArgs(0) & ” ” & oArgs(1) & ” ” & oArgs(2))
   WScript.Quit -1

When you call the script – you must pass parameters to the script:

cscript.exe LogScriptEventArgs.vbs scriptparam1 scriptparam2 scriptparam3

This creates the following event:

Log Name:            Operations Manager
Source:                 Health Service Script

Date:                    7/22/2009 12:07:09 AM

Event ID:              102

Task Category:    None

Level:                   Warning

Keywords:            Classic

User:                    N/A



LogScriptEventArgs.vbs : scriptparam1 scriptparam2 scriptparam3

There are a few things we would LIKE to have with this – but don’t:

1.  We cannot change the Event Source… it is hard coded to “Health Service Script”

2.  We cannot have event parameters in the event description.  This method is hard coded to 2 event parameters.  The script Name is Params/Param[1], and the “bstrDescription” string value is a single parameter, Params/Param[2]

Still – this method is very useful, when your complex VBScripts have multiple possible outputs… you can use the LogScriptEvent method to log distinct events, and then have other rules and monitors alert, or respond to, those custom events.

Leave a Reply

Your email address will not be published.