locked
Check Creation Date on File and Send Email if it Doesn't Match Current Date RRS feed

  • Question

  • Hello,

    I recently wrote a script on my firewall to backup the config to my FTP server daily.  I was wondering if, using Powershell, there was a way to check the creation date of the file against the current date, and if the creation date does not match the current date, have it send an email alerting me of this.  We do have an exchange server in our organization we can use for this alert.

    I'm familiar with the basics of batch, but Powershell is outside of my wheelhouse, so any help is appreciated.

    Best,

    Leisy

    Friday, September 15, 2017 3:27 PM

Answers

  • Hi Leisy,

    Based on my research, I wrote a script to check if there is a file’s creation date matches current date, and if not, it will send an email message (Please replace <String> to your actual value). You could run this script once a day by creating a schedule task.
    For your reference, hope it is helpful to you:
    $today = (Get-Date).ToString('yyyyMMdd')
    $creationdates = Get-ChildItem -Path <String> | Select-Object -ExpandProperty CreationTime
    foreach($cd in $creationdates)
    {
        if($cd.ToString('yyyyMMdd') -eq $today)
        {
        }
        else
        {
            Send-MailMessage -SmtpServer <String> -Subject <String> -From <String> -To <String>
        }
    }

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    • Marked as answer by leisy123 Monday, September 18, 2017 5:56 PM
    Monday, September 18, 2017 6:25 AM

All replies

  • Hi Leisy,

    Based on my research, I wrote a script to check if there is a file’s creation date matches current date, and if not, it will send an email message (Please replace <String> to your actual value). You could run this script once a day by creating a schedule task.
    For your reference, hope it is helpful to you:
    $today = (Get-Date).ToString('yyyyMMdd')
    $creationdates = Get-ChildItem -Path <String> | Select-Object -ExpandProperty CreationTime
    foreach($cd in $creationdates)
    {
        if($cd.ToString('yyyyMMdd') -eq $today)
        {
        }
        else
        {
            Send-MailMessage -SmtpServer <String> -Subject <String> -From <String> -To <String>
        }
    }

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    • Marked as answer by leisy123 Monday, September 18, 2017 5:56 PM
    Monday, September 18, 2017 6:25 AM
  • This is the correct way to test a DateTime for the date part against the current date.

    $file = "<path of file>"
    if($file = Get-Item $file -ErrorAction 0){
    	if($file.CreationTime.Date -ne [datetime]::Today) {
    		Send-MailMessage -SmtpServer <String> -Subject <String> -From <String> -To <String>
    	}else{
    		Write-Host 'File is from today'
        }
    }else{
    	Write-Host 'File not found'
    }

    Note that there is no reason to enumerate a single file and no need to convert things to strings.  The Net Framework is designed to manage all of that.

    Also we should attempt to avoid managing "negative" logical expressions as they are harder to read and can lead to confusion.  We can just use "-ne" which is a logically positive assertion and it is the one we are looking for.  Our purpose is to know when the date is "not equal" to todays date.  We are not asking for it to be equal so we can skip the positive results in favor of the negative.  THe code reads better and follows our requested assertion.

    Remember we a programming in an object system.  Linear code and turning things into strings is nearly always unnecessary and may cause unexpected results.


    \_(ツ)_/


    • Edited by jrv Monday, September 18, 2017 6:47 AM
    Monday, September 18, 2017 6:43 AM
  • Hi jrv,

    Thank you for your suggestions. I learned a lot from your reply.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Monday, September 18, 2017 8:52 AM
  • Albert - You are welcome.  I thought it might get you pointed into new things and ways of thinking.  Good luck.


    \_(ツ)_/

    Monday, September 18, 2017 9:15 AM
  • This was very helpful.  Thank you.
    Monday, September 18, 2017 5:55 PM