Powershell to send email with attachment only if file is full

  • Hi All,

    Hope All is Well.

    I am new to powershell. I am trying to attach a txt file using the following in a ps file and it works well.

    Send-MailMessage -To <> -From <> -Subject "System Events" -Attachments "c:\scripts\sysevents.txt" -SmtpServer <>

    I need help in writing my logic using powershell where if the sysevents.txt is empty then it should not send email. It should only send email if the txt file has data in it.

    Thanks Much for your inputs.

    “If your actions inspire others to dream more, learn more, do more and become more, you are a leader.” -- John Quincy Adams

  • I think I figured it out.

    Thanks GUys

    “If your actions inspire others to dream more, learn more, do more and become more, you are a leader.” -- John Quincy Adams

  • This may work.

    If ((Get-Content "Filenam.txt") -eq $Null) {

    "File is empty"

    }

    java[/url]

  • This may work.

    If ((Get-Content "Filenam.txt") -eq $Null) {

    "File is empty"

    }

    Exactly thats what I did and it worked..Thanks anyways

    “If your actions inspire others to dream more, learn more, do more and become more, you are a leader.” -- John Quincy Adams

  • another powershell addict 😀

    Me, myself and Powershell[/url]

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • 🙂

    “If your actions inspire others to dream more, learn more, do more and become more, you are a leader.” -- John Quincy Adams

  • Glad you worked it out but I thought I'd give you another option.

    if ($(Get-Item -Path c:\scripts\sysevents.txt).Length -gt 0) { "Send mail" }

    else { "Don't send" }

    And I agree with ALXDBA, welcome to Powershell.

  • This is my code:

    if((Get-Content "c:\scripts\appevents.txt") -eq $Null)

    {

    Send-MailMessage -To<> -From <> -Subject " " -body "There are no Errors or Warnings" -SmtpServer <>

    }

    else

    {

    Send-MailMessage -To <> -From <> -Subject "" -Attachments "c:\scripts\appevents.txt" -SmtpServer <>

    }

    clear-content "C:\scripts\appevents.txt"

    “If your actions inspire others to dream more, learn more, do more and become more, you are a leader.” -- John Quincy Adams

  • If it's a small file that's fine but when working with larger files you'll find a difference in performance between using Get-Content and just looking at the file size.

  • o..ok. The file shdnt grow bigger than 10 Mb, currently its at 388kb and it doesnt append to previously exisiting data as I am clearing the text file at the end. I will keep an eye on the file size and will change the function accordingly.

    Thanks again Bruce.

    “If your actions inspire others to dream more, learn more, do more and become more, you are a leader.” -- John Quincy Adams

  • Indeed, this would cause less system overhead ...

    get-item c:\temp\ibn\IBN_1_56_4960135.sqlplan | foreach -process {if ( $_.length -gt 0 ) { send mail here }}

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • ALZDBA,

    I am new to powershell. May I know what this statement does please. Thanks a bunch.

    “If your actions inspire others to dream more, learn more, do more and become more, you are a leader.” -- John Quincy Adams

  • Sapen (12/16/2011)


    ALZDBA,

    I am new to powershell. May I know what this statement does please. Thanks a bunch.

    Clear-Host

    #first doing just get-item to pull over file details

    get-item c:\temp\ibn\IBN_1_56_4960135.sqlplan

    <# result

    Directory: C:\temp\ibn

    Mode LastWriteTime Length Name

    ---- ------------- ------ ---

    -a--- 16/12/2011 14:13 302975 IBN_1_56_4960135.sqlplan

    #>

    #now piping the output directly to a foreach to check the file length (in bytes) and show its length if it is larger than zero

    get-item c:\temp\ibn\IBN_1_56_4960135.sqlplan | foreach -process {if ( $_.length -gt 0 ) { $_.length }}

    # result: 302975

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • Thanks Alzdba.

    “If your actions inspire others to dream more, learn more, do more and become more, you are a leader.” -- John Quincy Adams

  • actually the get-item cmdlet is one you'll use quite frequent.

    The problem is to think of it, not only in file context, but in item context, whatever that item may be.

    (filesystem, wmi sqlserver connection, ...)

    As stated in my little article[/url], there are some free ebooks which can get you started quickly.

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

Viewing 15 posts - 1 through 15 (of 19 total)

You must be logged in to reply to this topic. Login to reply