Powershell script to email date last modified

Viewed 824

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!

2 Answers

You need to get the LastWriteTime in the loop for paths. I would also recommend you set $Paths in a more standard array format.

$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!
  }
}

I figured out the issue, it was a bone-head mistake on my part. Part of the code was used for another purpose and towards the bottom in the message body is called for the machine name the script was running on, not the path name.

$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!" ; 
 }      
}

Thanks for the help otherwise!

Related