Help with check write date and email send

I am running a script that checks if the write time of a file is the correct updated one to today’s date. Is there a way to run this on more than one file in the same script? this is what i have so far that does it for just one file:

#Model to Email for failed daily jobs

$Path = '\\(filelocation)'

$File = Get-Item -Path $Path

$today = (Get-Date).Date

$To = Get-content C:\Daily\List.txt

$From = 'email@email.com'

$Subject = 'Daily- ( ) Drop'

$Server = 'servernamefill'

If($File.LastWriteTime.Date -ne $today)
{

 $Body = '(Jobname) Job: Failed'

 $properties = @{

 to = $To

 from = $From

 subject = $Subject

Body = $Body

 smtpserver = $Server
}


Send-MailMessage @properties
}

 

What i need is it to check for 3 separate files

$Path = '\(filelocation)'

$Path2 = '\(filelocation)'

$Path3 = '\(filelocation)'

$File = Get-Item -Path $Path

$File2 = Get-Item -Path $Path2

$File3 = Get-Item -Path $Path3

then verify:

if any of the variable $File, $File2, $File3 .LastWriteTime.Date -ne $today)

it will then send the email with the original settings.
Could anyone help me figure out how to set that up ive tried a few things but so far i havent got it

Thank you in advance!

If you look at the documentation for Get-Item, Path accepts a string array, which means that you can pass multiple paths to the command. If all the files are in the same directory, then Get-ChildItem can be used as well. To send multiple files, the body should be HTML so that you can leverage ConvertTo-HTML:

$path = 'C:\Scripts\temp.csv',
        'C:\Scripts\temp.json',
        'C:\Scripts\temp.xml'

$files = Get-Item -Path $path |
         Where {$_.LastWriteTime -lt (Get-Date).AddDays(-1)}


$body = @"
<html>
    <head>
    </head>
    <body>
        <p>The following jobs failed:</p>
        $($files | ConvertTo-HTML -Property BaseName, Name, LastWriteTime -Fragment)
    </body>
</html>
"@

if ($files) {

    $params = @{
        To         = Get-content C:\Daily\List.txt
        From       = 'email@email.com'
        Subject    = 'Daily- ( ) Drop'
        Server     = 'servernamefill'
        Body       = $body
        BodyAsHtml = $true
    }

    Send-MailMessage @params
}
else {
    'No job failures'
}

In my example I am looking at 2 weeks old, but that can be easily modified.

Output:

PS C:\Users\rasim> Get-Item -Path $path


    Directory: C:\Scripts


Mode                LastWriteTime         Length Name                                                                                                                                                                                    
----                -------------         ------ ----                                                                                                                                                                                    
-a----        7/30/2020  10:05 AM            126 temp.csv                                                                                                                                                                                
-a----         8/6/2020  10:25 AM            192 temp.json                                                                                                                                                                               
-a----        4/14/2020  11:38 AM           1800 temp.xml                                                                                                                                                                                



PS C:\Users\rasim> Get-Item -Path $path |
         Where {$_.LastWriteTime -lt (Get-Date).AddDays(-14)}


    Directory: C:\Scripts


Mode                LastWriteTime         Length Name                                                                                                                                                                                    
----                -------------         ------ ----                                                                                                                                                                                    
-a----        7/30/2020  10:05 AM            126 temp.csv                                                                                                                                                                                
-a----        4/14/2020  11:38 AM           1800 temp.xml 

Bradley,

When you crosspost the same question at the same time to different forums you should at least post links to the other forums along with your question to avoid people willing to you help making their work twice or more.

https://www.reddit.com/r/PowerShell/comments/i9m5fv/help_with_email_and_date_check/

Maybe this example of processing files in a foreach loop with your script will help.

For this example, we assume there is a file that contains a list of files we want to check.

$filelist = 'c:\tmp\filelist.txt'
Get-Content $filelist

\\server\share\file1.txt
\\server\share\file2.txt
\\server\share\file3.txt

So we will take each line (filename) of that file with Get-Content and process each through the loop

Get-Content $filelist | foreach {

    $File = Get-Item -Path $_

    $today = (Get-Date).Date

    $To = Get-content C:\Daily\List.txt

    $From = 'email@email.com'

    $Subject = 'Daily- ( ) Drop'

    $Server = 'servernamefill'

    If($File.LastWriteTime.Date -ne $today)
    {
        $Body = '(Jobname) Job: Failed'

        $properties = @{

            to = $To

            from = $From

            subject = $Subject

            Body = $Body

            smtpserver = $Server
        }

        Send-MailMessage @properties
    }
}