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  -To -SmtpServer -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. 






    Friday, October 21, 2011 8:38 PM


  • Something like this?

    $filedate = (get-date).adddays(-1).tostring("dd_MMM_yyyy")
    $filename = "D:\nightly_exports\rman_backup_results_UK_$($filedate).log"
    $email = @{
    From = ""
    To = ""
    SMTPServer = ""
    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