簡體   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