Script - Sending an email if file modified time is greater than 60 minutes

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Sam Boutros Sam Boutros 1 week, 1 day ago.

  • Author
    Posts
  • #73123
    Profile photo of Matt Cameron
    Matt Cameron
    Participant

    Hi,

    I am involved with a radio station that has its national hourly news sent from another radio station via FTP to our local server every hour between 6am-6pm. Sometimes older news bulletins are being replayed because of technical problems in getting the bulletin to us via FTP from the other radio station.

    I was hoping somebody might be able to help me out with a PS script that performs the following tasks.

    1. Checks the time stamp on the news file on our server (g:\audio\nrn\news.mp3)
    2. If the current time is greater than 60 minutes from the modified time on the news.mp3 file then send an email to the Program Director advising that the news did not update, then close script.
    3. If the current time is less than 60 minutes from the modified time on the news.mp3 file (means an updated bulletin exists) do nothing then close script.

    I guess I could setup a task in Windows to run the PS script every day between 6am-6pm.

    I would appreciate any help.

    Cheers,
    Matt

  • #73135
    Profile photo of Sam Boutros
    Sam Boutros
    Participant

    replace (less than sign) and (greater than sign) with the corresponding single characters..

    
    
    #region Input
    $FileName        = 'g:\audio\nrn\news.mp3'
    $EmailSender     = 'Hourly news file monitor (less than sign)DoNotReply@myDomain.com(greater than sign)'
    $EmailRecipients = @( # Add recipients email addresses below:
        'Sam Boutros (less than sign)myname@mydomain.com(greater than sign)'
    )
    $SMTPServer      = 'mymailserver.mydomain.com'
    #endregion
    
    #region Process
    try {
        $FileTimeStamp = (Get-Item -Path $FileName -ErrorAction Stop).LastWriteTime
    } catch {
        throw $_
    }
    if ((Get-Date).AddHours(-1) -gt $FileTimeStamp) {
        $Email = "File '$FileName' time stamp '$FileTimeStamp' is older than 60 minutes"
        $Email
        try { 
            Send-MailMessage -From $EmailSender -To $EmailRecipients -Body $Email -SmtpServer $SMTPServer -Subject $Email -Priority High -ErrorAction Stop
            "Email sent successfully"
        } catch {
            throw $_
        }
    } else {
        "File '$FileName' time stamp '$FileTimeStamp' is NOT older than 60 minutes"
    }
    #endregion
    

You must be logged in to reply to this topic.