Send an email based using Powershell based on an Output

Mike R 81 Reputation points
2022-01-07T13:02:20.817+00:00

Hello,

I am looking to send an email based upon an Output and was just wondering what is best practice for this?

In this case its to send an email when a file has not been modified in over 24 hours. The current script is this;

$fileObj = Get-Item -Path C:\Public$\certconfig.txt

Last Modified Date

if (($fileObj.LastWriteTime) -lt (Get-Date).AddHours(-24)) {Write-Output "Old file"}
else {Write-Output "New file"}

I have sent an email using PowerShell before but never in a script related to an Output. Thanks in advance.

Windows for business Windows Server User experience PowerShell
{count} votes

Accepted answer
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2022-01-07T17:31:13.767+00:00

    Hi @Mike R ,

    maybe this helps to get it started (not tested by myself):

        $smtphost = "mailserver.demo.local"   
        $to = "Administrator@demo.local"   
        $from = "Helpdesk@demo.local"   
        function Send-Mail {   
            param($From, $To, $Subject, $Body)   
            $smtp = New-Object system.net.mail.smtpClient($smtphost)   
            $mail = New-Object System.Net.Mail.MailMessage   
            $mail.from = $From   
            $mail.to.add($To)   
            $mail.subject = $Subject   
            $mail.body = $Body   
            $mail.isbodyhtml = $true   
            $smtp.send($mail)   
        }  
          
        $Subject = "New file found"  
        $Body = "New file found .... some more text"  
          
        $fileObj = Get-Item -Path C:\Public$\certconfig.txt  
        # Last Modified Date  
        if (($fileObj.LastWriteTime) -lt (Get-Date).AddHours(-24)) { Write-Output "Old file" }  
        else {  
            Write-Output "New file"  
            Send-Mail $from $to $subject $body   
        }  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


2 additional answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2022-01-07T15:30:45.067+00:00

    The best practice is to not fill a mailbox with meaningless notifications. If you tell someone "the sky is falling" too often they'll just ignore you.

    1 person found this answer helpful.
    0 comments No comments

  2. MotoX80 36,291 Reputation points
    2022-01-07T14:05:14.903+00:00

    What are your requirements?

    Are you monitoring a single file or multiple files in a folder? Do the emails go to one address or different addresses based on the file name? How often do you need to check the file age? Once a day? Every few hours? Do you want to send the email only one time, or every time the script executes?

    You'll want to develop the script interactively so that you can see the errors and get it working. Then use the task scheduler to execute the script at the desired interval.

    Your script may also need to provide credentials if your email server requires authentication.

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage?view=powershell-5.1

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.