Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Baffled, need to send email with output of command.

Reply
 
Old 06-12-2008   #1 (permalink)
LordFox


 
 

Baffled, need to send email with output of command.

Hi,

I'm trying to get some dir sizing and have that e-mailed. However, the
body of the message stays empty - it will not put the info in at all.
It will send regular text , and it does collect the info I want (I
checked by outputting the content of $body to the screen. I've checked
several example scripts, but I cannot see what I am doing wrong. I
hope you can help.

param(
$MailServer = 'mailserver.domain.local',
$MailTo = 'mailbox@xxxxxx',
$Mailfrom= 'script@xxxxxx',
$Subject = 'Diskspace for ' + (Get-Date)
)

$body = get-childitem \\server\share\ | where-object
{$_.PSisContainer} |
select-object Name, @{ Name="Size (GB)";
Expression={[math]::round((($_ |
Get-ChildItem -Recurse | Measure-Object -Sum Length).Sum + 0)/1GB,
2) }}


$email = new-object system.net.mail.mailmessage

$email.to.add($Mailto)
$email.from = $Mailfrom
$email.subject = $Subject
$email.isbodyhtml = $False
$email.body = $body

$client = new-object system.net.mail.smtpclient $mailserver
$client.send($email)


My guess is, I'm overlooking something very basic, but I've been
staring at it too long I suppose...

Thanks in advance,

LF

My System SpecsSystem Spec
Old 06-12-2008   #2 (permalink)
Shay Levi


 
 

Re: Baffled, need to send email with output of command.

Hi LordFox,

Try to pipe $body to Out-String



---
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com

L> Hi,
L>
L> I'm trying to get some dir sizing and have that e-mailed. However,
L> the body of the message stays empty - it will not put the info in at
L> all. It will send regular text , and it does collect the info I want
L> (I checked by outputting the content of $body to the screen. I've
L> checked several example scripts, but I cannot see what I am doing
L> wrong. I hope you can help.
L>
L> param(
L> $MailServer = 'mailserver.domain.local',
L> $MailTo = 'mailbox@xxxxxx',
L> $Mailfrom= 'script@xxxxxx',
L> $Subject = 'Diskspace for ' + (Get-Date)
L> )
L> $body = get-childitem \\server\share\ | where-object
L> {$_.PSisContainer} |
L> select-object Name, @{ Name="Size (GB)";
L> Expression={[math]::round((($_ |
L> Get-ChildItem -Recurse | Measure-Object -Sum Length).Sum + 0)/1GB, 2)
L> }}
L>
L> $email = new-object system.net.mail.mailmessage
L>
L> $email.to.add($Mailto)
L> $email.from = $Mailfrom
L> $email.subject = $Subject
L> $email.isbodyhtml = $False
L> $email.body = $body
L> $client = new-object system.net.mail.smtpclient $mailserver
L> $client.send($email)
L>
L> My guess is, I'm overlooking something very basic, but I've been
L> staring at it too long I suppose...
L>
L> Thanks in advance,
L>
L> LF
L>


My System SpecsSystem Spec
Old 06-12-2008   #3 (permalink)
LordFox


 
 

Re: Baffled, need to send email with output of command.

Thanks a million!

Pretty obvious solution :-) But as I said, have been staring at it too
long I suppose.

Have a great day,

LF



On Jun 12, 11:34*am, LordFox <rick.harderw...@xxxxxx> wrote:
Quote:

> Hi,
>
> I'm trying to get some dir sizing and have that e-mailed. However, the
> body of the message stays empty - it will not put the info in at all.
> It will send regular text , and it does collect the info I want (I
> checked by outputting the content of $body to the screen. I've checked
> several example scripts, but I cannot see what I am doing wrong. I
> hope you can help.
>
> param(
> * * * * $MailServer = 'mailserver.domain.local',
> * * * * $MailTo = 'mail...@xxxxxx',
> * * * * $Mailfrom= 'scr...@xxxxxx',
> * * * * $Subject = 'Diskspace for ' + (Get-Date)
> )
>
> $body = get-childitem \\server\share\ | where-object
> {$_.PSisContainer} |
> * * * * select-object Name, @{ Name="Size (GB)";
> Expression={[math]::round((($_ |
> Get-ChildItem -Recurse | Measure-Object -Sum Length).Sum + 0)/1GB,
> 2) }}
>
> $email = new-object system.net.mail.mailmessage
>
> $email.to.add($Mailto)
> $email.from = $Mailfrom
> $email.subject = $Subject
> $email.isbodyhtml = $False
> $email.body = $body
>
> $client = new-object system.net.mail.smtpclient $mailserver
> $client.send($email)
>
> My guess is, I'm overlooking something very basic, but I've been
> staring at it too long I suppose...
>
> Thanks in advance,
>
> LF
My System SpecsSystem Spec
Old 06-12-2008   #4 (permalink)
Marco Shaw [MVP]


 
 

Re: Baffled, need to send email with output of command.

LordFox wrote:
Quote:

> Thanks a million!
>
> Pretty obvious solution :-) But as I said, have been staring at it too
> long I suppose.
Not always so obvious, so a good question/issue.

I've cut-off stuff to the right below.

PS > $email = new-object system.net.mail.mailmessage
PS > $email|get-member body

TypeName: System.Net.Mail.MailMessage

Name MemberType Definition
---- ---------- ----------
Body Property System.String Body {get;set;}

Comments: This property is defined as a string object. So you need to
pass it a string specifically.

PS > $body = get-childitem c:\testing | where-object {$_.PSisContainer} | `
Quote:
Quote:

>> select-object Name, @{ Name="Size (GB)";
>> Expression={[math]::round((($_ | `
>> Get-ChildItem -Recurse | Measure-Object -Sum Length).Sum + 0)/1GB, 2)
>> }}
>>
PS > $body.gettype().fullname
System.Object[]

Comments: We don't have a string object, but an array of "generic objects".

PS > ($body|out-string).gettype().fullname
System.String
PS >

Comments: We've converted our array of objects to a string object. (I'd
hoped $body.tostring() might have worked also, but it doesn't.)

So, it is just a matter of trying to squeeze the wrong kind of object
into the body property. We just need to change the object type, and
then it worked.

Marco


--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Re: why can you not send an email from a command line Live Mail
Send command line via WMI - Pipe output to text file on remote mac PowerShell
Re: Send command line via WMI - Pipe output to text file on remote PowerShell
Split output to command PowerShell
Command output to file Vista file management


Vista Forums 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 Ltd

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