简体   繁体   中英

Windows batch or Powershell - send alert if file is not updated

Maybe it's just late and I can't think straight, but I need a way to send an alert if a specific file on a Windows based system has not been updated in the last hour.

We used to have something like this set up in the past, but the script was somehow lost during a server migration. I think we had a batch script that read the last modified date on the file, and if it was more than an hour old, it would send an alert using bmail. The batch script was set to run as a Windows scheduled task every hour or half hour.

Any quick and dirty way to do this using a batch script or Powershell? I'm not a Windows admin by trade, so this is proving much harder than I like to admit. We're on Windows Server 2016.

Thanks!

we had a batch script that read the last modified date on the file, and if it was more than an hour old, it would send an alert using bmail.

The equivalent in PowerShell would be something like:

# define a threshold
$threshold = New-TimeSpan -Hours 1

# fetch file info from disk
$file = Get-Item 'C:\path\to\file'

# calculate time since last modification
$timeSince = (Get-Date) - $file.LastWriteTime

# send mail if it's been too long
if($timeSince -gt $threshold){
  $emailbody = "ALERT! ALERT! It's been {0:hh\hmm\m} since the last modification!" -f $timeSince
  Send-MailMessage -Subject 'ALERT!' -Body $emailBody -From 'tom@domain.example' -To 'alert-recipient@domain.example' -SmtpServer 'mta.domain.example'
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM