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

Formating reports and e-mailing via PowerShell

Closed Thread
 
Thread Tools Display Modes
Old 05-13-2008   #1 (permalink)
Steven
Guest


 

Formating reports and e-mailing via PowerShell

I've run into this problem quite a few times and I figured I'd give in
and see if any of you can tell me what I'm doing wrong. When I write
PowerShell scripts I often have them e-mail me the results. Here is an
example script that I'm working on right now:

####################################################
$servers = Get-ExchangeServer | where {$_.site -like "*<sitename>" -and
$_.IsMailboxServer -like $true -and $_.Name -like "<servername>*"}

$staleU = $servers | Get-Mailbox -ResultSize:unlimited | where
{$_.RecipientTypeDetails -like "UserMailbox" -and
$_.HiddenFromAddressListsEnabled -like $false} | Get-MailboxStatistics |
where {$_.LastLogonTime -lt $(get-date).addDays(-30)}

$report = $staleU | sort LastLogonTime | ft
displayName,LastLogonTime,LastLoggedOnUserAccount

#Build SMTP mail objects and configure them
$SMTPServer = "<smtp server name>"
$Msg = new-object system.net.mail.MailMessage
$SMTPClient = new-object system.net.mail.smtpClient
$SMTPClient.host = $SMTPServer

$Msg.From = "<from address>"
$Msg.To.Add("<recipient address>")
$Msg.Subject = "Suspected stale Exch2007 accounts"
$Msg.Body = $report
$SmtpClient.Send($Msg)
####################################################

The problem that I'm running into is that what ends up being stored in
$msg.body is not what you see if you were return $report (I realize it's
a collection of objects, I'm just not sure how to make it so that it's
formated text). It ends up being this (snipped to keep it short, but
its pages of it):

------------snip-------------------
Microsoft.PowerShell.Commands.Internal.Format.FormatStartData
Microsoft.PowerShell.Comman
ds.Internal.Format.GroupStartData
Microsoft.PowerShell.Commands.Internal.Format.FormatEnt
ryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
Microsoft.PowerShell
..Commands.Internal.Format.FormatEntryData
Microsoft.PowerShell.Commands.Internal.Format.F
ormatEntryData
Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.Po
werShell.Commands.Internal.Format.FormatEntryData
Microsoft.PowerShell.Commands.Internal.
------------snip-------------------

The way I've worked around it is to output $report as a file ($report |
out-file report.txt) and then use that as the body:

$msg.body = get-content report.txt

How can I do this without sending $report to a file first just so I can
send it via e-mail?

Thanks for the help as always!
Old 05-13-2008   #2 (permalink)
Kiron
Guest


 

Re: Formating reports and e-mailing via PowerShell

# [Net.Mail.MailMessage]'s Body takes a [String]
$msg = new-object system.net.mail.MailMessage
$msg | gm body

# Format-Table outputs an array of formatted objects
$ft = ls $env:winDir Note* | ft
$ft.getType().name
$ft | % {$_.getType()} | sort -u

# when you assign this [Object[]] to $msg.body it's cast as [String]
[string]$ft

# pipe the formatted objects to Out-String to get the formatted report
# as [String]; the only side effect to this is a couple of extra
# linebreaks at the start and end that can be trimmed
$ft = ls $env:winDir Note* | ft | out-string
$ft
$ft = $ft.trim()
$ft

# you can save the extra step
$ft = (ls $env:winDir Note* | ft | out-string).trim()
$ft

# try it
$report = ($staleU | sort LastLogonTime |
ft displayName,LastLogonTime,LastLoggedOnUserAccount | out-string).trim()

--
Kiron
Old 05-13-2008   #3 (permalink)
Steven
Guest


 

Re: Formating reports and e-mailing via PowerShell

Perfect...thanks!

Kiron wrote:
Quote:

> # [Net.Mail.MailMessage]'s Body takes a [String]
> $msg = new-object system.net.mail.MailMessage
> $msg | gm body
>
> # Format-Table outputs an array of formatted objects
> $ft = ls $env:winDir Note* | ft
> $ft.getType().name
> $ft | % {$_.getType()} | sort -u
>
> # when you assign this [Object[]] to $msg.body it's cast as [String]
> [string]$ft
>
> # pipe the formatted objects to Out-String to get the formatted report
> # as [String]; the only side effect to this is a couple of extra
> # linebreaks at the start and end that can be trimmed
> $ft = ls $env:winDir Note* | ft | out-string
> $ft
> $ft = $ft.trim()
> $ft
>
> # you can save the extra step
> $ft = (ls $env:winDir Note* | ft | out-string).trim()
> $ft
>
> # try it
> $report = ($staleU | sort LastLogonTime |
> ft displayName,LastLogonTime,LastLoggedOnUserAccount | out-string).trim()
>
> --
> Kiron
Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Formating HD ArtThib Vista installation & setup 1 04-16-2008 10:37 PM
HDD Formating Bogdan Vista hardware & devices 1 01-20-2008 05:33 AM
!!Bug Reports!!--What Community would Microsoft prefer Windows Vista Business Bug Reports to be sent??? Richard L. Matthis Vista General 2 11-21-2007 04:31 PM
Formating my PC bassam Vista hardware & devices 6 09-14-2007 06:42 AM
Announce: powershell-users mailing list Marco Shaw PowerShell 5 08-14-2007 04:46 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