I had a request from a colleague today whether we could monitor a file and alert him if it grows above a certain size.
The context around this request involves an old, legacy process that calls an Access db file and automatically runs some statistics scripts which are then fed into a SQL database.
Sounds awful I know, but the author is long gone and the workflow hasn’t been transposed elsewhere yet. Every now and then, for some reason the file size of the mdf grows from the usual ~~700MB to the maximum 2GB due to some corruption. At which time it gets restored and it works for a while. Yeah, I get it, it’s awful !
Anyhow, this corruption happens infrequently and can go unnoticed. So this little snippet checks the file size, and if it’s over .5GB, sends an email to the user so they can take action.
I’ve no doubt I’ll use this somewhere else sometime, so here it is for posterity !
$targetFile = "path to file\file.extension"
$fileSize = ((Get-ItemProperty $targetfile).length/1GB)
$friendlySize = ([math]::Round($fileSize, 2))
if ($fileSize -gt .5)
Send-MailMessage -To email@example.com -From firstname.lastname@example.org -SMTPServer smtpserver.domain.com -Subject "file.mdb is OVERSIZE" -Body "file.mdb is $friendlysize GB and may be corrupted, please check"
Change the $targetFile and the growth tolerance (here 1.5) and email details as required save as a scheduled task.
When the trigger size is exceeded, the following email is despatched.