Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Send .ps1 script output as e-mail body

Closed Thread
 
Thread Tools Display Modes
Old 02-08-2007   #1 (permalink)
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
Old 02-08-2007   #2 (permalink)
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

Old 02-08-2007   #3 (permalink)
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

Old 02-08-2007   #4 (permalink)
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

>

Old 02-08-2007   #5 (permalink)
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
>

Old 02-08-2007   #6 (permalink)
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

Old 02-08-2007   #7 (permalink)
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
>

Old 02-08-2007   #8 (permalink)
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

Old 02-08-2007   #9 (permalink)
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
>

Old 02-09-2007   #10 (permalink)
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

Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Show output of steps of PS script LordFox PowerShell 5 05-09-2008 09:05 AM
Paging output in a Powershell Script Kevin PowerShell 10 10-18-2007 03:44 AM
output .ps1 script to text file Aaron PowerShell 2 07-11-2007 05:30 PM
Hide console and capture output from script? Brian Vallelunga PowerShell 4 04-03-2007 01:43 PM
PS script prints output instead of executing commands Andy Webster PowerShell 4 12-29-2006 10:52 AM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50