How to Create Operations Manager Events by Using a Script

Updated: January 31, 2012

Applies To: System Center 2012 - Operations Manager

Runtime scripts that you use in Operations Manager can create events, which are added to the Operations Manager Event log. These events can provide information about the occurrence of anticipated script errors—for example, if a script is called with an incorrect number of arguments. Creating events is often useful when you are testing and debugging new scripts or management packs.

noteNote
To view the events created by a runtime script, you can use the Event Viewer Administrative Tool, which is provided with Windows Server 2008.

To create an event in a script, call the MOMScriptAPI.LogScriptEvent method, which you can use to define the name of the script, the event ID, the severity of the event, and a description of the event.

Example

The following script, written in VBScript, pings a target computer and submits the computer name and ping response time (in milliseconds) as performance data to the Operations Manager database. The script adds two events to the event log: an information event that occurs if the script is incorrectly called without a required argument, and a warning event that occurs if the specified computer cannot be reached.

Option Explicit

Dim oArgs
Set oArgs = WScript.Arguments
' Arg 0 is the FQDN of the computer to ping.

Dim oAPI, oBag
Set oAPI = CreateObject("MOM.ScriptAPI")

if oArgs.Count < 1 Then
	' Create an information event if the script is called without
	' the required argument.
	Call oAPI.LogScriptEvent("PingTarget.vbs",101,0,"PingTarget was called without any arguments and was not executed.")
	Wscript.Quit -1
End If

Dim targetComputer
targetComputer = oArgs(0)

Dim objPing, objStatus, timeToPing
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
	ExecQuery("select * from Win32_PingStatus where address = '"_
		& targetComputer & "'")
For Each objStatus in objPing
	If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then 
		' Create a warning event if the specified computer cannot be reached.
		Call oAPI.LogScriptEvent("PingTarget.vbs",101,2,"The target: " & targetComputer & " was not reachable.")
		Wscript.Quit -1
	End If
	timeToPing = objStatus.ResponseTime
Next

Set oBag = oAPI.CreatePropertyBag()
Call oBag.AddValue(targetComputer, timeToPing)
Call oAPI.Return(oBag)

See Also