'========================================================================== ' ' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 4.0 ' ' NAME: File Count Script - MOM VERSION ' ' AUTHOR: Pete Zerger ' DATE : 6/2/2006 ' ' COMMENT: Accepts a comma-separated list of directories and counts files in each. ' Logs an event if file count breaches a user-defined threshold ' ' PARAMETERS: TargetDirs - Comma-separated list of directories to watch for file count. ' LogSuccessEvent - Boolean value 1=LogEventOnSuccess 0=LogOnFailOnly ' ` FileCountThreshold - Number above which file count threshold alert will be raised '========================================================================== 'Event Constants Const EVENT_TYPE_SUCCESS = 0 Const EVENT_TYPE_ERROR = 1 Const EVENT_TYPE_WARNING = 2 Const EVENT_TYPE_INFORMATION = 4 Dim sHosts, sServer, bLogSuccessEvent 'Script Parameters sTargetDirs = Split(ScriptContext.Parameters.Get("TargetDirs"), ",") bLogSuccessEvent = CBool(ScriptContext.Parameters.Get("LogSuccessEvent")) intMaxFileCount = CInt(ScriptContext.Parameters.Get("FileCountThreshold")) 'sTargetDirs= Split("c:\documentation\scripts,c:\windows\system32", ",") 'Call the FileCount function for each host in the list above For Each sTarget In sTargetDirs 'On Error Resume Next 'WScript.Echo sTarget FileCount sTarget, intMaxFileCount Next Function FileCount(sDir, intFileCount) strComputer = "localhost" Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colFileList = objWMIService.ExecQuery _ ("ASSOCIATORS OF {Win32_Directory.Name='" & sDir & "'} Where " & "ResultClass = CIM_DataFile") If colFileList.Count =< intFileCount And bLogSuccessEvent Then CreateEvent 51003,EVENT_TYPE_INFORMATION,"File Count Threshold Script","File count of " & colFileList.Count & " is below threshold for " & sDir & "." ElseIf colFileList.Count > intFileCount Then CreateEvent 51004,EVENT_TYPE_ERROR,"File Count Threshold Script","File count threshold of " & intFileCount & " breached for " & sDir & ". Current file count is" & colFileList.Count & "." Else CreateEvent 51005,EVENT_TYPE_ERROR,"File Count Threshold Script","File check failed for " & sDir & "." End If End Function 'Standard Event Sub-routine) ' Standard Event creation subroutine Sub CreateEvent(iEventNumber,iEventType,sEventSource,sEventMessage) Set oEvent = ScriptContext.CreateEvent() oEvent.EventNumber = iEventNumber oEvent.EventType = iEventType oEvent.EventSource = sEventSource oEvent.Message = sEventMessage ScriptContext.Submit oEvent End Sub