none
Attaching a file to an email by filename+date RRS feed

  • Question

  • I'm trying to generate an email that appends an attachment (or imports the contents of a .log file to the body of) to an email.

    I have put together a quick test to make sure there are no restrictions on the host.

     Send-MailMessage -From sender@testmail.com  -To recipient@testmail.com -SmtpServer mail.testsender.com -Subject testsubject -Attachments "D:\nightly_exports\rman_backup_results_UK_20_oct_2011.log"

    This is successful. 

    The file is generated daily and has the current date appended to it (e.g rman_backup_results_UK_20_oct_2011.log) now im wondering is it possible to have a script that looks for the current date in a filename and will then attach it to an email.. Or alternatively will write the contents of the files into the body of the email. 

     

    Have seen a few scripts out there that do similar things just wondering if anyone has something specific or can point me in the right direction. I am fairly new to PS. 

    Thanks,

    Doug. 

     

     

     

    Friday, October 21, 2011 8:38 PM

Answers

  • Something like this?

    $filedate = (get-date).adddays(-1).tostring("dd_MMM_yyyy")
    $filename = "D:\nightly_exports\rman_backup_results_UK_$($filedate).log"
    
    $email = @{
    From = "sender@testmail.com"
    To = "recipient@testmail.com"
    SMTPServer = "mail.testsender.com"
    Subject = "testsubject"
    }
    
    if (test-path $filename){
    gci $filename | send-mailmessage @email
    
    

     


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Friday, October 21, 2011 8:56 PM