none
Find last modified file in folder and send it by email RRS feed

  • Question

  • Hi,

    I've been trying to put bits and pieces of code together where the script would find the last modified file with a .csv extension and send it by email.

    This is the code I've been playing around with but to no avail for the moment:

    This next one is giving me errors in the param section so I don't even know if it would send anything even if the syntax was fixed:

    function Send-RecentFile {
    
        param(
            [ValidateScript({Test-path $_ })]
            [&"C:\Users\idiotcantcode\Dropbox\Canadian Bonds\Portfolio\Position Files"] $Path
            )
    
        $file = Get-ChildItem -Path $Path -File | Sort CreationTime | Select -Last 1
        Write-Output "The most recently created file is $($file.Name)"
    
    
        $messageParameters = @{
            From        = "idiotcantcode@Canadian.ca"
            To          = "idiotcantcode@gmail.com"
            Subject     = "Reconciliation Files"
            Body        = "Dear all,
    
    Please see attached reconciliation files.
    Kind regards,
    
    "
            SMTPServer  = "mail.Canadian.ca"
            Attachments = $file.FullName
        }
    
        Send-MailMessage @messageParameters -Credential (Get-Credential "idiotcantcode@Canadian.ca")
    }

    This next version doesn't give me any errors in PS but it doesn't find the last modified file nor send any email at all, so I have no clue what is the problem:

    function Send-RecentFile {
    
        $Path = "C:\Users\idiotcantcode\Dropbox\Canadian Bonds\Portfolio\Position Files\";
        $file = Get-ChildItem -Path $Path -File | Sort CreationTime | Select -Last 1
        Write-Output "The most recently created file is $($file.Name)"
    
    
        $messageParameters = @{
            From        = "idiotcantcode@canadian.ca"
            To          = "idiotcantcode@gmail.com"
            Subject     = "Reconciliation Files"
            Body        = "Dear all,
    
    Please see attached reconciliation files. 
    
    Kind regards,
    
    "
            SMTPServer  = "mail.canadian.ca"
            Attachments = $file.FullName
        }
    
        Send-MailMessage @messageParameters -Credential (Get-Credential "idiotcantcode@canadian.ca")
    }

    Any idea what could be causing an issue? In the first <g class="gr_ gr_196 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" data-gr-id="196" id="196">one</g> I'm thinking it's a parenthesis issue in terms of syntax but not sure what I'm doing wrong. In the second one, I thought I had it nailed by looking at some codes in the repository but I seem to be lacking something for the email to be sent.


    • Edited by gcefaloni Tuesday, March 26, 2019 1:59 AM
    • Moved by Bill_Stewart Wednesday, September 4, 2019 7:42 PM This is not "fix/debug/rewrite script for me" forum
    Tuesday, March 26, 2019 1:57 AM

All replies

  • This would be cleaner and easier to maintain.  You might want to add some error management too.

    $body = @'
    Dear all,
    
    Please see attached reconciliation files. 
    
    Kind regards,
    '@
    
    $messageParameters = @{
        From        = 'idiotcantcode@canadian.ca'
        To          = 'idiotcantcode@gmail.com'
        Subject     = 'Reconciliation Files'
        SMTPServer  = 'mail.canadian.ca'
        Body        = $body
        Credential = Get-Credential 'idiotcantcode@canadian.ca'
    }
    
    function Send-RecentFile {
        
        $Path = 'C:\Users\idiotcantcode\Dropbox\Canadian Bonds\Portfolio\Position Files\*.csv'
        $file = Get-ChildItem -Path $Path -File | Sort-Object CreationTime | Select-Object -Last 1
        Write-Output "The most recently created file is $($file.Name)"
        
        Send-MailMessage @messageParameters -Attachments $file.FullName
    }
    
    Send-RecentFile
    


    \_(ツ)_/

    Tuesday, March 26, 2019 2:36 AM
  • This is how to do it with a parameter:

    $body = @'
    Dear all,
    
    Please see attached reconciliation files. 
    
    Kind regards,
    '@
    
    $messageParameters = @{
        From        = 'idiotcantcode@canadian.ca'
        To          = 'idiotcantcode@gmail.com'
        Subject     = 'Reconciliation Files'
        SMTPServer  = 'mail.canadian.ca'
        Body        = $body
        Credential = Get-Credential 'idiotcantcode@canadian.ca'
    }
    
    function Send-RecentFile {
        param (
            [ValidateScript({ Test-path $_ })]
            [string]$Path
        )
        $file = Get-ChildItem -Path $Path -File | Sort-Object CreationTime | Select-Object -Last 1
        Write-Output "The most recently created file is $($file.Name)"
        
        Send-MailMessage @messageParameters -Attachments $file.FullName
    }
    
    Send-RecentFile 'C:\Users\idiotcantcode\Dropbox\Canadian Bonds\Portfolio\Position Files\*.csv'
    


    \_(ツ)_/

    Tuesday, March 26, 2019 2:42 AM
  • Thank you jrv.

    It doesn't seem to return any errors in PS and it does the write-output but it doesn't seem to be making the connection with Outlook to actually send out the email. It just runs and the email doesn't go out.

    Are there any other checks or parameters I could add to make the connection with outlook so it actually sends out the email? If not, what else could be causing the problem of email not being sent even if no errors occur in PShell?

    $body = @'
    Dear all,
    
    Please see attached reconciliation files.
    
    Kind regards,
    '@
    
    $messageParameters = @{
        From        = 'idiotcantcode@canadian.ca'
        To          = 'idiotcantcode@canadian.ca'
        Subject     = 'Reconciliation Files'
        SMTPServer  = 'server999.web-hosting.com'
        Body        = $body
    }
    
    function Send-RecentFile {
        
        $Path = 'C:\Users\idiotcantcode\Dropbox\Canadian Bonds\Portfolio\Position Files\*.csv'
        $file = Get-ChildItem -Path $Path -File | Sort-Object CreationTime | Select-Object -Last 1
        Write-Output "The most recently created file is $($file.Name)"
        
        Send-MailMessage @messageParameters -Attachments $file.FullName
    }
    
    Send-RecentFile



    I've also tried an alternate route including SMTP details with the following code, but it also seems to not make the connection with Outlook to actually send the email.

     
    $Body = “get the information here to show the data with attachement”  
        $dir = "C:\Users\idiotcantcode\Dropbox\Canadian Bonds\Portfolio\Position Files\*.csv"
        $latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1
        $latest.Fullname
        $file = $latest.Fullname
        $EmailFrom = “idiotcantcode@canadian.ca”
        $EmailTo = “idiotcantcode@gmail.com”
        $SMTPServer = “mail.canadian.ca”
        $EmailSubject = “Test”
        $att = new-object Net.Mail.Attachment($file)
        $mailmessage = New-Object system.net.mail.mailmessage
        $mailmessage.from = ($EmailFrom)
        $mailmessage.To.add($EmailTo)
        $mailmessage.Subject = $EmailSubject
        $mailmessage.Body = $Body
        $mailmessage.IsBodyHTML = $true
        $mailmessage.Attachments.Add($att)
        $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 465)
        $SMTPClient.EnableSsl = $true
        $SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“idiotcantcode@canadian.ca”, “idiotcantcode”);
        $SMTPClient.Send($mailmessage)
        $att.Dispose()

    Thanks again for your help!


    • Edited by gcefaloni Tuesday, March 26, 2019 5:46 PM
    Tuesday, March 26, 2019 5:45 PM
  • What does this have to do with Outlook.  Outlook is an MS Office program.  The code is PowerShell.  There is no Outlook here.


    \_(ツ)_/

    Tuesday, March 26, 2019 7:03 PM
  • Nevermind about Outlook. I was making a link with Outlook because that is the program I use to email. I thought it might make a link to Outlook as my default email manager to send the email. From your response, I'm understanding that is not the case and it is just using the SMTPserver itself to send the email. I guess my question is: How does powershell connect to the SMTP server and how do I troubleshoot it if the code above doesn't end up sending the email?
    • Edited by gcefaloni Thursday, March 28, 2019 2:30 PM
    Wednesday, March 27, 2019 1:04 AM
  • Please fix your last post. It is unreadable.  Either turn of Grammarly or upgrade Grammarly to the fixed version.


    \_(ツ)_/

    Wednesday, March 27, 2019 4:31 AM
  • Apologies, wierd that grammarly causes that error. Anyways, here was the message as you can also see above:

    "Nevermind about Outlook. I was making a link with Outlook because that is the program I use to email. I thought it might make a link to Outlook as my default email manager to send the email. From your response, I'm understanding that is not the case and it is just using the SMTPserver itself to send the email. I guess my question is: How does powershell connect to the SMTP server and how do I troubleshoot it if the code above doesn't end up sending the email?"

    Thursday, March 28, 2019 2:31 PM
  • You have to debug your code. Start by reading all errors carefully.  THen run in debugger and inspect each step.


    \_(ツ)_/

    Thursday, March 28, 2019 3:16 PM