每周我们都会将备份文件复制到文件服务器上的文件夹中。我正在寻找一种方法来通知我,如果一个文件在7天内没有写入该文件夹。
我在网上找到了这个脚本(我向作者道歉,因为我没有信用),我觉得这让我走上正轨。我真正想要的是某种输出会告诉我文件是否完全没有被写入。如果备份成功,我不需要确认。
$lastWrite = (get-item C:\ExampleDirectory).LastWriteTime
$timespan = new-timespan -days 7
if (((get-date) - $lastWrite) -gt $timespan) {
# older
} else {
# newer
}
答案 0 :(得分:3)
希望您能够抓取目录中的所有文件,按LastWriteTime
排序,然后将最新文件的文件与7天前的文件进行比较:
$LastWriteTime = (Get-ChildItem C:\ExampleDirectory |Sort LastWriteTime)[-1].LastWriteTime
if($LastWriteTime -gt [DateTime]::Now.AddDays(-7))
{
# File newer than 7 days is present
}
else
{
# Something is wrong, time to alert!
}
对于提醒部分,请查看Send-MailMessage
或Write-EventLog