繁体   English   中英

Windows 批处理或 Powershell - 如果文件未更新,则发送警报

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

也许现在太晚了,我无法直接思考,但是如果基于 Windows 的系统上的特定文件在过去一小时内没有更新,我需要一种方法来发送警报。

我们过去曾经设置过类似的东西,但是在服务器迁移过程中脚本不知何故丢失了。 我认为我们有一个批处理脚本可以读取文件上的最后修改日期,如果超过一个小时,它会使用 bmail 发送警报。 批处理脚本设置为每小时或半小时作为 Windows 计划任务运行。

使用批处理脚本或 Powershell 执行此操作的任何快速而肮脏的方法? 我不是专业的 Windows 管理员,所以事实证明这比我想承认的要困难得多。 我们使用的是 Windows Server 2016。

谢谢!

我们有一个批处理脚本读取文件的最后修改日期,如果它超过一个小时,它会使用 bmail 发送警报。

PowerShell 中的等效项类似于:

# 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'
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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