SCOM 2007 Script to Monitor File Count inside a Directory
Below is a script that I wrote to monitor file count in a directory. You
To Implement this please reference my earlier post on how to setup a Script-Based monitor. http://www.scom2k7.com/create-a-script-based-unit-monitor-in-opsmgr2007-via-the-gui/
In the Parameters: Dialog box you will need to specify a directory path in quotes then a space and the type in the number of files you what to have in your directory before you want to alarm.
For Unhealthy Expression Property[@Name='State'] Equals BAD
For Healthy Expresion Property[@Name='State'] Equals GOOD
On Error Resume Next
Dim oAPI, oBag, objFSO, objFldr
Set oAPI = CreateObject(“MOM.ScriptAPI“)
Set oArgs = WScript.Arguments
WScript.Quit -1
End If
NumberOfFiles = int(oArgs(1))
Set objFSO=CreateObject(“Scripting.FileSystemObject”)
Set objFldr=objFSO.GetFolder(strFldr)
If objFldr.Files.Count > NumberOfFiles Then
strReturn = “Number of Files in “ & strFldr& ” is greater than “ & NumberOfFiles
Call oBag.AddValue(“State”,“BAD”)
Call oBag.AddValue(“ret“,strReturn)
Else
End If
http://www.scom2k7.com/downloads/countnumberoffileswithscom.zip
[ad]

Anonymous:
This sample works great on a local folder but does not work on a network shared folder. If I put anything in the network folder an alert is generate no matter what the parameter count. Any ideas on using a script on a network shared folder?
7 March 2008, 7:03 pmTimothy McFadden:
That is because you will need to use a run-as account to connect to a network share. Everything by default runs as local system. Local system doesn’t have access to your network shares.
8 March 2008, 12:05 amAnonymous:
How do I configure the run as account to work with this monitor? THe network share is on an AS400 that is not part of the domain.
10 March 2008, 12:47 pmDoug:
I’m getting the following error when the script runs.
The process started at 2:52:48 PM failed to create System.PropertyBagData. Errors found in output:
C:\Program Files\System Center Operations Manager 2007\Health Service State\Monitoring Host Temporary Files 39\546\CheckFolder.vbs(13, 25) Microsoft VBScript compilation error: Invalid character
I think it’s referencing the following line:Set oBag = oAPI.CreateTypedPropertyBag(StateDataType)
Here’s what I have in my script. Any ideas?
‘ Enter a script that outputs a property bag
‘ Example VBScript:
‘
‘ Dim oAPI, oBag
‘ Set oAPI = CreateObject(“MOM.ScriptAPI”)
‘ Set oBag = oAPI.CreatePropertyBag()
‘ Call oBag.AddValue(“Status”,”OK”)
‘ Call oAPI.Return(oBag)
On Error Resume Next
Dim oAPI, oBag, objFSO, objFldr
Set oAPI = CreateObject(“MOM.ScriptAPI“)
Set oBag = oAPI.CreateTypedPropertyBag(StateDataType)
Set oArgs = WScript.Arguments
MessageText = “”
If oArgs.Count Then
Call oAPI.LogScriptEvent(“Check.Folder.vbs”, 500, 0, “Script aborted. Not enough parameters provided.”)
WScript.Quit -1
End If
strFldr = oArgs(0)
NumberOfFiles = int(oArgs(1))
Set objFSO=CreateObject(“Scripting.FileSystemObject”)
Set objFldr=objFSO.GetFolder(strFldr)
If objFldr.Files.Count > NumberOfFiles Then
strReturn = “Number of Files in “ & strFldr& ” is greater than “ & NumberOfFiles
Call oBag.AddValue(“State”,“BAD”)
Call oBag.AddValue(“ret“,strReturn)
Else
Call oBag.AddValue(“State”,“GOOD”)
End If
24 August 2009, 5:58 pmCall oAPI.Return(oBag)
Tim:
Not sure there are a few things wrong with it.
Download this one. http://www.scom2k7.com/downloads/countnumberoffileswithscom.zip
24 August 2009, 9:01 pmnix:
Hi,
Thx for your script, i could adapt it and test it in my MP.
But i have a question :
In the Alerting part, in the alert description box, I tried this :
“Test :
$Property[@Name='ret']$
$Data/Context/Property[@Name='ret']$
$Property[@Name='State']$
$Data/Context/Property[@Name='State']$
”
but in the alert description, I only get
“Test :
{0}
{1}
{2}
{3}”
Do you know how I can get strings contained in the oBag ??
Regards.
15 February 2010, 5:23 amTim:
In the script you must use something like
Call oBag.AddValue(“ret”,strReturn)
Call oBag.AddValue(“ret2″,strReturn2)
Then in the description you would add somthing like this
27 April 2010, 7:23 pm$Data/Context/Property[@Name='ret']$
$Data/Context/Property[@Name='ret'2]$