SCOM 2007 Script to Monitor File Count inside a Directory
Below is a script that I wrote to monitor file count in a directory.
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
‘ Script Start
Dim oAPI, oBag, objFSO, objFldr, bSendError, strOut
bSendError = False
Set oAPI = CreateObject(“MOM.ScriptAPI”)
Set oBag = oAPI.CreateTypedPropertyBag(StateDataType)
Set eBag = oAPI.CreateTypedPropertyBag(StateDataType)
Set oArgs = WScript.Arguments
If oArgs.Count < 2 Then
strReturn = “Script aborted. Not enough parameters provided”
Call eBag.AddValue(“State”,”BAD”)
Call eBag.AddValue(“ret”,strReturn)
Call oAPI.Return(eBag)
WScript.Quit -1
End If
strOut = checkfolder(oArgs(0),int(oArgs(1)))
If bSendError Then
strReturn = strOut
Call oBag.AddValue(“State”,”BAD”)
Call oBag.AddValue(“ret2″, objFldr.Files.Count)
Call oBag.AddValue(“ret”,strReturn)
Else
’WScript.Echo(“GOOD “& vbNewLine & strOut)
Call oBag.AddValue(“State”,”GOOD”)
End If
if 0 <> Err.number Then
strReturn = “An Error occured: ” & Err.Description
Call eBag.AddValue(“State”,”BAD”)
Call eBag.AddValue(“ret”,strReturn)
Call oAPI.Return(eBag)
else
Call oAPI.Return(oBag)
end if
Function checkfolder(strfldname,numberfiles)
Set objFSO=CreateObject(“Scripting.FileSystemObject”)
Set objFldr=objFSO.GetFolder(strfldname)
If objFldr.Files.Count > numberfiles Then
checkfolder = numberfiles
bSendError = True
End If
End Function
‘Script End


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]$
Jim:
I think that the {0}
{1}
{2}
{3}”
part is because you need to re-trigger the alert. I had that happen to me too. It seems to also happen when you misspell a variable name.
27 August 2010, 11:43 amKris:
The script looks like it will do what I need it to but how can I scope this to a single server. I have one server that I need to keep an eye on is there a way to do this in the SCOM 2007 R2 Monitor or can this script be modified to look at only one server. I am very much a rookie at scripting so any help is appreciated.
1 September 2010, 11:17 amMichael:
I am still fighting with this. All i need is to monitor a folder on a network share, to see if a new file is getting added, but im not sure what to use in Unhealthy Expression. Do i need both?
Property[@Name'State'] eq BAD
Property[@Name'ret'] greater than 0
Thanks in advance!
22 October 2010, 6:42 amMichael:
The script is working using just Property[@Name="State"] eq BAD/GOOD, but the alert does not close it self. What do i use for HEALTHY expression?
27 October 2010, 6:04 amMike:
Will a UNC work as a parameter instead of a logical local drive path, provided run-as profile was configured correctly?
20 January 2011, 2:19 pmTim McFadden:
Yes as long as you have a run as account.
20 January 2011, 2:28 pmMike:
Just enclose the UNC in quotes as a parameter?
24 January 2011, 10:58 amDavide:
Hi to all,
24 March 2011, 8:04 amI’m a SCOM’ neophite and I’ve tried to setup this new monitor but at my console I don’t see any alert.
I’ve applied, as target,the monitor to the Windows2008 computer and created the monitor using a custom MP.
Do you have any idea how can I troubleshoot the problem?
thanks
Davide
EPA:
Hello,
I tried the script with an UNC (\\\C$\ and 1 (file count), RUN as PROFILE/ACCOUNT, although there are more the 1 file in the , so far I don’t see any alerts. How can I make sure my script is working properly.
Thank you,
8 August 2011, 3:51 pmEPA
Wasim:
Thanks a lot Jim…!
30 September 2011, 8:39 am