Windows Vista Forums

Send .ps1 script output as e-mail body
  1. #1


    Stephen Merkel Guest

    Send .ps1 script output as e-mail body

    My script uses format-table as follows:
    Format-Table @{Label="Disk Drive"; Expression={$_.DeviceID+'\'}},`
    @{Label="Size(GB)"; Expression={[math]::round$_.size/1024/1024/1024,2)}},`
    @{Label="Free(GB)";
    Expression={[math]::round$_.freespace/1024/1024/1024,2)}}` -a



    I can redirect this output to a .txt file and send the file as an attachment.
    When I atttempt to use this file as the e-mail body, all the formating
    (columns) are lost and the message appears as one long string.
    I am hoping there is a way to capture the output from the format-table in
    such a way that it can be used as the e-mail body.
    Thanks in advance....

    --
    Merkel-DBA

      My System SpecsSystem Spec

  2. #2


    Keith Hill Guest

    Re: Send .ps1 script output as e-mail body

    "Stephen Merkel" <StephenMerkel@discussions.microsoft.com> wrote in message
    news:0D176EB7-0471-4C72-9A86-BFFC0D58E32F@microsoft.com...
    > My script uses format-table as follows:
    > Format-Table @{Label="Disk Drive"; Expression={$_.DeviceID+'\'}},`
    > @{Label="Size(GB)"; Expression={[math]::round$_.size/1024/1024/1024,2)}},`
    > @{Label="Free(GB)";
    > Expression={[math]::round$_.freespace/1024/1024/1024,2)}}` -a
    >
    > I can redirect this output to a .txt file and send the file as an
    > attachment.
    > When I atttempt to use this file as the e-mail body, all the formating
    > (columns) are lost and the message appears as one long string.
    > I am hoping there is a way to capture the output from the format-table in
    > such a way that it can be used as the e-mail body.
    > Thanks in advance....


    Hmm, when I execute:

    gwmi Win32_LogicalDisk |
    Format-Table @{Label="Disk Drive"; Expression={$_.DeviceID+'\'}},
    @{Label="Size(GB)"; Expression={[math]::round($_.size/1GB,2)}},
    @{Label="Free(GB)";
    Expression={[math]::round($_.freespace/1GB,2)}} -a

    and redirect it to foo.txt I get multiple lines and as long as I use a fixed
    pitch font, all the columns line up. BTW you don't have to repeatedly
    divide by 1024 to get to GB units. Just divide by 1GB. There's also MB and
    KB available.

    --
    Keith


      My System SpecsSystem Spec

  3. #3


    Gaurhoth Guest

    Re: Send .ps1 script output as e-mail body

    Pipe to out-string... example:

    First without out-string... now you can see the type is an object.
    PS ps:\savsweep> $s = get-process | format-table -prop Handles,ProcessName
    PS ps:\savsweep> $s.gettype()

    IsPublic IsSerial Name BaseType
    -------- -------- ---- --------
    True True Object[] System.Array

    Now with out-string... now $s is a string that can be included anywhere you need a string (email body, etc).

    PS ps:\savsweep> $s = get-process | format-table -prop Handles,ProcessName | out-string
    PS ps:\savsweep> $s.gettype()

    IsPublic IsSerial Name BaseType
    -------- -------- ---- --------
    True True String System.Object



    "Stephen Merkel" <StephenMerkel@discussions.microsoft.com> wrote in message news:0D176EB7-0471-4C72-9A86-BFFC0D58E32F@microsoft.com...
    > My script uses format-table as follows:
    > Format-Table @{Label="Disk Drive"; Expression={$_.DeviceID+'\'}},`
    > @{Label="Size(GB)"; Expression={[math]::round$_.size/1024/1024/1024,2)}},`
    > @{Label="Free(GB)";
    > Expression={[math]::round$_.freespace/1024/1024/1024,2)}}` -a
    >
    > I can redirect this output to a .txt file and send the file as an attachment.
    > When I atttempt to use this file as the e-mail body, all the formating
    > (columns) are lost and the message appears as one long string.
    > I am hoping there is a way to capture the output from the format-table in
    > such a way that it can be used as the e-mail body.
    > Thanks in advance....
    >
    > --
    > Merkel-DBA


      My System SpecsSystem Spec

  4. #4


    Stephen Merkel Guest

    Re: Send .ps1 script output as e-mail body

    using out-string,,, this is what shows up in the body of the e-mail
    2/8/2007 3:32 PM SERVER! Disk Space Report Disk Drive Size(GB) Free(GB)
    ---------- -------- -------- C:\ 8.01 2.98 E:\ 60.36
    54.7 F:\ 150 143.41 SERVER2 Disk Space Report Disk
    Drive Size(GB) Free(GB) ---------- -------- -------- C:\ 7.96
    4.61 E:\ 60.35 31.14 F:\ 68.35 48.03 SERVER3
    Disk Space Report Disk Drive Size(GB) Free(GB) ---------- -------- --------
    C:\ 7.81 4.61 E:\ 60.51 46.44 F:\ 136.7
    111.81 G:\ 136.7 128.64 H:\ 136.7 66.13 I:\
    136.7

    i.e. the formating of the columns is gone.
    BTW this is also how it looks if I don't use out-string.

    Thanks



    --
    Merkel-DBA


    "Gaurhoth" wrote:

    > Pipe to out-string... example:
    >
    > First without out-string... now you can see the type is an object.
    > PS ps:\savsweep> $s = get-process | format-table -prop Handles,ProcessName
    > PS ps:\savsweep> $s.gettype()
    >
    > IsPublic IsSerial Name BaseType
    > -------- -------- ---- --------
    > True True Object[] System.Array
    >
    > Now with out-string... now $s is a string that can be included anywhere
    > you need a string (email body, etc).
    >
    > PS ps:\savsweep> $s = get-process | format-table -prop Handles,ProcessName
    > | out-string
    > PS ps:\savsweep> $s.gettype()
    >
    > IsPublic IsSerial Name BaseType
    > -------- -------- ---- --------
    > True True String System.Object
    >
    >
    >
    > "Stephen Merkel" <StephenMerkel@discussions.microsoft.com> wrote in
    > message news:0D176EB7-0471-4C72-9A86-BFFC0D58E32F@microsoft.com...
    > > My script uses format-table as follows:
    > > Format-Table @{Label="Disk Drive"; Expression={$_.DeviceID+'\'}},`
    > > @{Label="Size(GB)";

    > Expression={[math]::round$_.size/1024/1024/1024,2)}},`
    > > @{Label="Free(GB)";
    > > Expression={[math]::round$_.freespace/1024/1024/1024,2)}}` -a
    > >
    > > I can redirect this output to a .txt file and send the file as an

    > attachment.
    > > When I atttempt to use this file as the e-mail body, all the formating
    > > (columns) are lost and the message appears as one long string.
    > > I am hoping there is a way to capture the output from the format-table

    > in
    > > such a way that it can be used as the e-mail body.
    > > Thanks in advance....
    > >
    > > --
    > > Merkel-DBA

    >


      My System SpecsSystem Spec

  5. #5


    Stephen Merkel Guest

    Re: Send .ps1 script output as e-mail body

    Yes, the columns line up in the text file that you redirect it to, but if I
    then send that file as the body of an e-mail, the formating is shot.

    Thanks

    --
    Merkel-DBA


    "Keith Hill" wrote:

    > "Stephen Merkel" <StephenMerkel@discussions.microsoft.com> wrote in message
    > news:0D176EB7-0471-4C72-9A86-BFFC0D58E32F@microsoft.com...
    > > My script uses format-table as follows:
    > > Format-Table @{Label="Disk Drive"; Expression={$_.DeviceID+'\'}},`
    > > @{Label="Size(GB)"; Expression={[math]::round$_.size/1024/1024/1024,2)}},`
    > > @{Label="Free(GB)";
    > > Expression={[math]::round$_.freespace/1024/1024/1024,2)}}` -a
    > >
    > > I can redirect this output to a .txt file and send the file as an
    > > attachment.
    > > When I atttempt to use this file as the e-mail body, all the formating
    > > (columns) are lost and the message appears as one long string.
    > > I am hoping there is a way to capture the output from the format-table in
    > > such a way that it can be used as the e-mail body.
    > > Thanks in advance....

    >
    > Hmm, when I execute:
    >
    > gwmi Win32_LogicalDisk |
    > Format-Table @{Label="Disk Drive"; Expression={$_.DeviceID+'\'}},
    > @{Label="Size(GB)"; Expression={[math]::round($_.size/1GB,2)}},
    > @{Label="Free(GB)";
    > Expression={[math]::round($_.freespace/1GB,2)}} -a
    >
    > and redirect it to foo.txt I get multiple lines and as long as I use a fixed
    > pitch font, all the columns line up. BTW you don't have to repeatedly
    > divide by 1024 to get to GB units. Just divide by 1GB. There's also MB and
    > KB available.
    >
    > --
    > Keith
    >


      My System SpecsSystem Spec

  6. #6


    Keith Hill Guest

    Re: Send .ps1 script output as e-mail body

    "Stephen Merkel" <StephenMerkel@discussions.microsoft.com> wrote in message
    news:E5A09E2A-7C55-4640-A7BB-29F83E076407@microsoft.com...
    > Yes, the columns line up in the text file that you redirect it to, but if
    > I
    > then send that file as the body of an e-mail, the formating is shot.
    >


    Perhaps you could say more about how you are getting the output of the file
    into the body of the email. Is it clipboard copy/paste?

    --
    Keith


      My System SpecsSystem Spec

  7. #7


    Stephen Merkel Guest

    Re: Send .ps1 script output as e-mail body

    I am using get-content to take the output file 'c:\space.txt' and put it into
    $emailbody.

    <SNIP>

    $emailbody = (get-content "c:\space.txt")

    $mailer = new-object Net.Mail.SMTPclient($SMTPserver)
    $msg = new-object Net.Mail.MailMessage($from, $to, $subject, $emailbody)
    $attachment = new-object Net.Mail.Attachment($fileattachment)
    $msg.attachments.add($attachment)
    $mailer.send($msg)
    --
    Merkel-DBA


    "Keith Hill" wrote:

    > "Stephen Merkel" <StephenMerkel@discussions.microsoft.com> wrote in message
    > news:E5A09E2A-7C55-4640-A7BB-29F83E076407@microsoft.com...
    > > Yes, the columns line up in the text file that you redirect it to, but if
    > > I
    > > then send that file as the body of an e-mail, the formating is shot.
    > >

    >
    > Perhaps you could say more about how you are getting the output of the file
    > into the body of the email. Is it clipboard copy/paste?
    >
    > --
    > Keith
    >


      My System SpecsSystem Spec

  8. #8


    Keith Hill Guest

    Re: Send .ps1 script output as e-mail body

    "Stephen Merkel" <StephenMerkel@discussions.microsoft.com> wrote in message
    news:0CEE34E4-FC45-4288-AEFA-4D3C3799D98A@microsoft.com...
    >I am using get-content to take the output file 'c:\space.txt' and put it
    >into
    > $emailbody.
    >
    > <SNIP>
    >
    > $emailbody = (get-content "c:\space.txt")
    >
    > $mailer = new-object Net.Mail.SMTPclient($SMTPserver)
    > $msg = new-object Net.Mail.MailMessage($from, $to, $subject, $emailbody)
    > $attachment = new-object Net.Mail.Attachment($fileattachment)
    > $msg.attachments.add($attachment)
    > $mailer.send($msg)


    get-content pulls out the text in separe string (one per line) and remove
    the newline. This should work:

    $emailbody = [io.file]::ReadAllText('C:\space.txt')

    With the PowerShell community extension installed you could also do this:

    $emailbody = gc 'C:\space.txt' | Join-String -NewLine

    --
    Keith


      My System SpecsSystem Spec

  9. #9


    Stephen Merkel Guest

    Re: Send .ps1 script output as e-mail body

    Works Great
    Thanks for keeping at this with me.

    I guess there will always be another .net class to become aquainted with
    --
    Merkel-DBA


    "Keith Hill" wrote:

    > "Stephen Merkel" <StephenMerkel@discussions.microsoft.com> wrote in message
    > news:0CEE34E4-FC45-4288-AEFA-4D3C3799D98A@microsoft.com...
    > >I am using get-content to take the output file 'c:\space.txt' and put it
    > >into
    > > $emailbody.
    > >
    > > <SNIP>
    > >
    > > $emailbody = (get-content "c:\space.txt")
    > >
    > > $mailer = new-object Net.Mail.SMTPclient($SMTPserver)
    > > $msg = new-object Net.Mail.MailMessage($from, $to, $subject, $emailbody)
    > > $attachment = new-object Net.Mail.Attachment($fileattachment)
    > > $msg.attachments.add($attachment)
    > > $mailer.send($msg)

    >
    > get-content pulls out the text in separe string (one per line) and remove
    > the newline. This should work:
    >
    > $emailbody = [io.file]::ReadAllText('C:\space.txt')
    >
    > With the PowerShell community extension installed you could also do this:
    >
    > $emailbody = gc 'C:\space.txt' | Join-String -NewLine
    >
    > --
    > Keith
    >


      My System SpecsSystem Spec

  10. #10


    Lance Guest

    Re: Send .ps1 script output as e-mail body

    On Feb 8, 3:40 pm, Stephen Merkel
    <StephenMer...@discussions.microsoft.com> wrote:
    > i.e. the formating of the columns is gone.
    > BTW this is also how it looks if I don't use out-string.


    You should convert the text to html, and send an html capable sender
    (the sender will have to be able to mime encode in order to be able to
    send an html email). Here is one example using NetCmdlets:

    send-email -server monkey -from lancer@monkey -to lancer@monkey -
    subject "NetCmdlets test" -message $htmlstring

    Lance


      My System SpecsSystem Spec

Send .ps1 script output as e-mail body problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to output single HTTP GET ? Vilius Mockûnas VB Script 5 10 Oct 2009
Send a text file as message body? Barry Holmes VB Script 1 23 Sep 2009
Can script configure itself to log all output? Todd Beaulieu PowerShell 3 11 Jun 2009
Re: script output Pegasus [MVP] VB Script 1 29 May 2009
Output script to email simon22 PowerShell 2 08 Dec 2008