OpsMgr: Creating a monitor to determine if a file exists (sample script and tutorial)

Recently a user in the SCC OpsMgr community forums wanted to monitor for the existence of a file. With many similar file system related scripts (file and folder size, count, age, etc.) but no exact match, I put together a quick sample to the job. The sample is VBScript, as this can be easily implemented in the Operations 2007 / 2012 consoles, where PowerShell requires time and authoring skills. The sample script below is intended for use in a two state script monitor in any version of OpsMgr 2007 or 2012.

Here’s a how-to for creating a two state script monitor
How to create a Script-based 2-State Monitor

And a relatively simple sample script and a few additional scripting resources for OpsMgr and System Center

Sample Script

'==========================================================================
'
' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 2009
'
' NAME: DoesFileExist
'
' AUTHOR: Pete Zerger, MVP (Cloud and Datacenter Admin)
' DATE  : 3/12/2012
'
'  COMMENT: Verifies a target file (including path) exists.
'           Intended for use with OpsMgr two state script monitor.
'
'==========================================================================
 
OPTION EXPLICIT
Call Main
Sub Main()
 
'Declare Variables
 
'File-related variables
Dim fso, folder, file, FilePath
 
'OpsMgr related variables
Dim ObjArgs, objMOM, ObjBag
 
 
Set objArgs = Wscript.Arguments
 
' Retrieve parameters
folder = CStr(ObjArgs.Item(0))
file = CStr(ObjArgs.Item(1))
FilePath = folder & "\" & file
WScript.Echo folder
WScript.echo file
WScript.Echo FilePath
 
' Instantiate File System Object
Set fso = CreateObject("Scripting.FileSystemObject")
 
' Instantiate MOM API
Set objMOM = CreateObject("MOM.ScriptAPI")
Set ObjBag = objMOM.CreatePropertyBag()
 
 
' Verify the path to the file exists xists
If (fso.FolderExists(folder)) Then
 
'Folder exists, submit property bag and continue
Call ObjBag.AddValue("FolderExists","Yes")
WScript.Echo "Folder exists"
Else
 
'Folder does not exist, submit property bag and exit
Call ObjBag.AddValue("FolderExists","No")
Call ObjBag.AddValue("FileExists","No")
WScript.Echo "Folder doesn't exist"
objMOM.AddItem(objBag)
Call objMOM.ReturnItems
Exit Sub
End If
 
' Verify the file exists
If (fso.FileExists(FilePath)) Then
'File exists, submit property bag and exit
Call ObjBag.AddValue("FileExists","Yes")
objMOM.AddItem(objBag)
Call objMOM.ReturnItems
Else
 
'File does not exist, submit property bag and exit
Call ObjBag.AddValue("FileExists","No")
WScript.Echo "File doesn't exist"
objMOM.AddItem(objBag)
Call objMOM.ReturnItems
Exit Sub
End If
 
 
End Sub

 

Additional Resources

We have quite a collection of scripts here on System Center Central from some of the communities best and brightest, which you can find at the links below:

Links: OpsMgr 2007 / 2012 Scripting Articles at System Center Central

Master Collection of System Center PowerShell

One thought on “OpsMgr: Creating a monitor to determine if a file exists (sample script and tutorial)

  1. Pingback: Create a script based monitor for the existence of a file, with recovery to copy file - Kevin Holman's System Center Blog - Site Home - TechNet Blogs

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.