将上次修改日期发送到电子邮件的 Powershell 脚本
Powershell script to email date last modified
我目前是一名系统管理员,正在尝试使用 Powershell 脚本将文件夹的上次修改日期与今天的日期进行比较,以查看备份是否超过 7 天。如果是,则它会向我们的公司帐户发送电子邮件提醒我们。
电子邮件部分工作正常,但问题是将每个人的备份文件夹(在 NAS 上)放在一个数组中并被调用进行检查。到目前为止的代码是这样的:
$paths = ($backup1 = "Y:\DESKTOP-OQRSLAU\Backup Set*"),
($backup2 = "V:\DESKTOP-I6B29SG\Backup Set*")
$lastWrite = (get-item $paths).LastWriteTime
foreach($backup in $paths){
if ($lastWrite -ge (get-date).AddDays(-7).ToString("yyyy-MM-dd")){
Write-Output "Success!"
$message = new-object Net.Mail.MailMessage;
$message.From = $email_from_address;
foreach ($to in $email_to_addressArray) {
$message.To.Add($to);
}
$message.Subject = ("BACKUP WARNING: " + "Out of Date Backup");
$message.Body = "`r`n`r`n";
$message.Body += " ";
$message.Body += " ";
$message.Body += ("The following machines backup is out of date: " + $env:computername + "`r`n");
$message.Body += "`r`n";
$message.Body += "`r`n";
$message.Body += ("The latest backup for this machine is: " + $lastWrite + "`r`n");
$message.Body += "`r`n";
$message.Body += "`r`n";
$message.Body += ("***This warning will fire when a backup is older than seven days***");
$message.Body += ""
$smtp = new-object Net.Mail.SmtpClient($email_smtp_host, $email_smtp_port);
$smtp.EnableSSL = $email_smtp_SSL;
$smtp.Credentials = New-Object System.Net.NetworkCredential($email_username, $email_password);
$smtp.send($message);
$message.Dispose();
write-host "... E-Mail sent!" ;
}
else {
exit
}
}
我现在收到的电子邮件回复仅针对上面列出的第一个路径(Y: 驱动器)。知道我做错了什么吗?我在 Powershell 方面经验不足。提前致谢!
您需要在路径循环中获取 LastWriteTime。我还建议您以更标准的数组格式设置 $Paths。
$paths = @("Y:\DESKTOP-OQRSLAU\Backup Set*","V:\DESKTOP-I6B29SG\Backup Set*")
foreach ($backup in $paths) {
$lastWrite = (get-item $backup).LastWriteTime
if ($lastWrite -ge (get-date).AddDays(-7).ToString("yyyy-MM-dd")) {
# Do Stuff...
}
else {
# Some other action NOT exit!
}
}
我弄明白了,这是我的一个愚蠢的错误。部分代码被用于另一个目的,在消息正文的底部被称为脚本 运行 所在的机器名称,而不是路径名称。
$paths = @("Y:\DESKTOP-OQRSLAU\Backup Set*", "V:\DESKTOP-I6B29SG\Backup Set*")
foreach($backup in $paths){
$mostRecent = (get-date).AddDays(-365).ToString("yyyy-MM-dd")
$lastWrite = (get-item $backup).LastWriteTime
foreach($lastWriteDate in $lastWrite){
if ($lastWriteDate -ge $mostRecent) {
$mostRecent = $lastWriteDate;
}
};
if ($mostRecent -ge (get-date).AddDays(-50).ToString("yyyy-MM-dd")){
$message = new-object Net.Mail.MailMessage;
$message.From = $email_from_address;
$message.To.Add($email_to_address);
$message.Subject = ("BACKUP WARNING: " + "Out of Date Backup");
$message.Body = "`r`n`r`n";
$message.Body += ("THE CHURCH ONLINE BACKUP MONITOR");
$message.Body += " ";
$message.Body += "`r`n`r`n";
$message.Body += " ";
$message.Body += ("The following backup is out of date: " + $backup +
"`r`n");
$message.Body += "`r`n";
$message.Body += "`r`n";
$message.Body += ("The latest backup for this machine is: " + $mostRecent +
"`r`n");
$message.Body += "`r`n";
$message.Body += "`r`n";
$message.Body += ("***This warning will fire when a backup is older than seven
days***");
$message.Body += ""
$smtp = new-object Net.Mail.SmtpClient($email_smtp_host, $email_smtp_port);
$smtp.EnableSSL = $email_smtp_SSL;
$smtp.Credentials = New-Object System.Net.NetworkCredential($email_username,
$email_password);
$smtp.send($message);
$message.Dispose();
write-host "... E-Mail sent!" ;
}
}
感谢您的帮助!
我目前是一名系统管理员,正在尝试使用 Powershell 脚本将文件夹的上次修改日期与今天的日期进行比较,以查看备份是否超过 7 天。如果是,则它会向我们的公司帐户发送电子邮件提醒我们。
电子邮件部分工作正常,但问题是将每个人的备份文件夹(在 NAS 上)放在一个数组中并被调用进行检查。到目前为止的代码是这样的:
$paths = ($backup1 = "Y:\DESKTOP-OQRSLAU\Backup Set*"),
($backup2 = "V:\DESKTOP-I6B29SG\Backup Set*")
$lastWrite = (get-item $paths).LastWriteTime
foreach($backup in $paths){
if ($lastWrite -ge (get-date).AddDays(-7).ToString("yyyy-MM-dd")){
Write-Output "Success!"
$message = new-object Net.Mail.MailMessage;
$message.From = $email_from_address;
foreach ($to in $email_to_addressArray) {
$message.To.Add($to);
}
$message.Subject = ("BACKUP WARNING: " + "Out of Date Backup");
$message.Body = "`r`n`r`n";
$message.Body += " ";
$message.Body += " ";
$message.Body += ("The following machines backup is out of date: " + $env:computername + "`r`n");
$message.Body += "`r`n";
$message.Body += "`r`n";
$message.Body += ("The latest backup for this machine is: " + $lastWrite + "`r`n");
$message.Body += "`r`n";
$message.Body += "`r`n";
$message.Body += ("***This warning will fire when a backup is older than seven days***");
$message.Body += ""
$smtp = new-object Net.Mail.SmtpClient($email_smtp_host, $email_smtp_port);
$smtp.EnableSSL = $email_smtp_SSL;
$smtp.Credentials = New-Object System.Net.NetworkCredential($email_username, $email_password);
$smtp.send($message);
$message.Dispose();
write-host "... E-Mail sent!" ;
}
else {
exit
}
}
我现在收到的电子邮件回复仅针对上面列出的第一个路径(Y: 驱动器)。知道我做错了什么吗?我在 Powershell 方面经验不足。提前致谢!
您需要在路径循环中获取 LastWriteTime。我还建议您以更标准的数组格式设置 $Paths。
$paths = @("Y:\DESKTOP-OQRSLAU\Backup Set*","V:\DESKTOP-I6B29SG\Backup Set*")
foreach ($backup in $paths) {
$lastWrite = (get-item $backup).LastWriteTime
if ($lastWrite -ge (get-date).AddDays(-7).ToString("yyyy-MM-dd")) {
# Do Stuff...
}
else {
# Some other action NOT exit!
}
}
我弄明白了,这是我的一个愚蠢的错误。部分代码被用于另一个目的,在消息正文的底部被称为脚本 运行 所在的机器名称,而不是路径名称。
$paths = @("Y:\DESKTOP-OQRSLAU\Backup Set*", "V:\DESKTOP-I6B29SG\Backup Set*")
foreach($backup in $paths){
$mostRecent = (get-date).AddDays(-365).ToString("yyyy-MM-dd")
$lastWrite = (get-item $backup).LastWriteTime
foreach($lastWriteDate in $lastWrite){
if ($lastWriteDate -ge $mostRecent) {
$mostRecent = $lastWriteDate;
}
};
if ($mostRecent -ge (get-date).AddDays(-50).ToString("yyyy-MM-dd")){
$message = new-object Net.Mail.MailMessage;
$message.From = $email_from_address;
$message.To.Add($email_to_address);
$message.Subject = ("BACKUP WARNING: " + "Out of Date Backup");
$message.Body = "`r`n`r`n";
$message.Body += ("THE CHURCH ONLINE BACKUP MONITOR");
$message.Body += " ";
$message.Body += "`r`n`r`n";
$message.Body += " ";
$message.Body += ("The following backup is out of date: " + $backup +
"`r`n");
$message.Body += "`r`n";
$message.Body += "`r`n";
$message.Body += ("The latest backup for this machine is: " + $mostRecent +
"`r`n");
$message.Body += "`r`n";
$message.Body += "`r`n";
$message.Body += ("***This warning will fire when a backup is older than seven
days***");
$message.Body += ""
$smtp = new-object Net.Mail.SmtpClient($email_smtp_host, $email_smtp_port);
$smtp.EnableSSL = $email_smtp_SSL;
$smtp.Credentials = New-Object System.Net.NetworkCredential($email_username,
$email_password);
$smtp.send($message);
$message.Dispose();
write-host "... E-Mail sent!" ;
}
}
感谢您的帮助!