![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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! |
| | #2 (permalink) |
| 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 |
| | #3 (permalink) | ||||||||||||
| Guest | Re: Formating reports and e-mailing via PowerShell Perfect...thanks! Kiron wrote:
| ||||||||||||
| |
| |
![]() |
| 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 |