OpsMgr 2007: Writing Custom Scripts Demystified - Part 1
I had come across number of people who wanted to write custom scripts for Operations Manager to make their job easier, be creative or just have fun. So i just decided to pull some data, reference and code together to make everyone understand really how simple and easy it was to write simple scripts in OPSMGR to go that extra mile.
This blog is for people who want to begin writing OPSMGR scripts with no or minimal experience, of course there would be learning curve involved but with simple example and exercise you should be able to make it thru.
To begin with here is simple script that would probably submit a value associated with a specific machine, again why would i do that is something I’ll cover as we go thru( to make things easier there is always an example)
===================================
Set oAPI = CreateObject("MOM.ScriptAPI")
Set oBag = oAPI.CreatePropertyBag()
Call oBag.AddValue("Manufacturing-Year ",2008)
Call oAPI.Return(oBag)
CAll oAPI.LogScriptEvent("Manufacturing-Year =",100,0,2008)
===================================
Let’s explain what just happened up here
Set oAPI = CreateObject("MOM.ScriptAPI")
Line 1: We created an object of type ScriptApi which will provide with necessary function to implement the script
Set oBag = oAPI.CreatePropertyBag()
Line 2: We use the object to create another object called as PropertyBag
PropertyBag literally means you define certain properties and give them values, and of course then there are specific property bags for specific functions, and we will surely talk about them later.
Call oBag.AddValue("Manufacturing Year ",2008)
Line 3: We create a property called “Manufacturing-Year” and assign a value of 2008 (which really can be anything, just for this example, i used this one)
Call oAPI.Return(oBag)
Line 4: We submit the property back to OPSMGR for it to process it
CAll oAPI.LogScriptEvent("Manufacturing Year =",100,0,2008)
Line 5: It’s kind of optional where we are printing the value to the Operations Manager Event Logs
This Function takes 4 Parameters of course they all have their significance.
Details of every parameter is listed in https://msdn.microsoft.com/en-us/library/bb437630.aspx
Moment of Truth:
Let’s see one of the Examples where we can make more sense of this Post and see this in action.
So here's what I want to do, I want to report the number of network connection and if the number of network connections exceed 30 the health of Windows Computer should become critical and less than 30 then it should be healthy, this check should be performed at an interval of 1 minutes regularly.
Caution: there might be multiple ways of achieving this, but our intension is to use the above script snippet to get this going
What’s the plan ?
So the idea is I am going to use the netstat -na command from windows and count the number of lines returned which should be equal to number of network connections, then submit that dynamic value into the operations manager
====================================
Dim oAPI, oBag
set shellobj=WScript.CreateObject("WScript.Shell")
set execObj=shellobj.Exec("netstat -na")
OutData=execObj.StdOut.ReadAll()
val=execObj.StdOut.Line
Set oAPI = CreateObject("MOM.ScriptAPI")
Set oBag = oAPI.CreatePropertyBag()
Call oBag.AddValue("Connections",val)
Call oAPI.Return(oBag)
CAll oAPI.LogScriptEvent("Network Connections =",110,0,val)
====================================
So apparently once we have the script we are going to use the Timed Script TWO State Monitor, I have attached the screen shots to get this implemented.Hope this takes you one step ahead in terms of Scripting for OpsMgr 2007
Jeevan Bisht | Manageability Tech Lead