I'm currently a sysadmin trying to use a Powershell script to compare the Last Modified date of a folder to todays date to see if a backup is more than seven days old. If it is, then it send an email to our company account alerting us.
The email portion works fine, but the issue is having everyone's backup folder (on a NAS) in an array and be called to be checked. The code is have so far is this:
$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
}
}
The response I'm getting right now as an email is only for the first path listed above (the Y: drive). Any idea what I'm doing wrong? I'm not too experienced in Powershell. Thanks in advance!