I am already using a script to watch a folder for when certain files show up and send an email. However, I had a user ask me, can I create something to monitor for when a file doesn’t show up?
We have a folder where a certain file with the name Dump-362-Month-Day-Year.xml is supposed to show up every night at 11:15. This file is then processed by another process at 2AM. A group would like to be notified if the file is missing.
I have tried figuring out how to do this, and am not sure where to really start to look for a missing file.
Is this possible and can someone give me some pointers on how to get started? I am thinking that somehow you look for a file with the days date (and maybe time stamp, since it’s always 11:15), and set a flag so that if the flag is false then it sends an email.
This is the script I am using to watch a folder when a file appears.
$folder = ‘c:\crash’ # Enter the root path you want to monitor.
$filter = ‘*.crs’ # You can enter a wildcard filter here.
In the following line, you can change 'IncludeSubdirectories to $true if required.
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]‘FileName, LastWrite’}
Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Send-MailMessage -To sql@domain.com -From FileSystemWatcher_OpsMgr@domain.com -Subject “File Added to Crash Directory” -Body “The file ‘$name’ was $changeType at $timeStamp” -SmtpServer smtp.domain.com
}