Monitor file count in folder

One of my key pieces of software has some custom print software on it I am needing to monitor this of printing issues. I have already found their service and am monitoring that but the issue is that this weekend that service got hung up it was still running so I go no alert. What would like to do is monitor the folder in the software where incoming print jobs are held until they are processed and alert if the number of files in there goes over a number that would alert us there may be an issue going on. Any thoughts on how I can do this?

Hi.

You can create a two state (PowerShell) script monitor and genreate unhealty state (and an alert) if Your threshold is breached. Adding some SCOM black Magic to my example below should do the trick :slight_smile:

$folder = x:\path
[int]$threshold = 100
$fileCount = (Get-ChildItem $folder).Count
if ($fileCount -gt $threshold){
Write-host "Too many files in $folder"}
1 Like

Kevin Holman has a walkthrough on creating a simple script monitor that does pretty much exactly what you want (and since it’s based on VBScript, it should perform suitably well that you can run it on a semi-frequent interval). It even has a sample of two recoveries that fire based off of the monitor to restart a service (print spooler in this case, but obviously you’d change that to be your print software).

https://blogs.technet.microsoft.com/kevinholman/2014/02/11/opsmgr-simple-example-script-based-monitor-with-script-based-recovery/

Hope that helps!

Using MP author and the included PowerShell will do the job, just adjust for your own requirements.
“Health” and “Count” are the property bag values I used but you can use whatever suits you.

$files = Get-ChildItem “X:\Folder Path”

$files.count

IF ($files.count -gt 30000){$Result = “ERROR”} ELSE {$Result = “SUCCESS”}

$api = new-object -comObject ‘MOM.ScriptAPI’

$bag = $api.CreatePropertyBag()

$bag.AddValue(‘Health’,$Result)

$bag.Addvalue(‘Count’,$files.count)

$api.return($bag)

You could even add a recovery task off the back of the monitor to run a script to restart the service or perform another task to resolve the issue :wink: