?
Solved

Powershell Script - Formatting

Posted on 2011-03-10
8
Medium Priority
?
741 Views
Last Modified: 2012-05-11
I have a script that used to be formatted correctly (The text file had all the headers lined up properly) in Exchange 2007.  Now in Exchange 2010 the formatting is off and I cannot figure out how to get it to line up.

The Script is Below and the sample text file with the header formatting is also attached.

Any Help would be great
###Send mailbox statistics script

###Variables - change these to match what you want
$FromAddress = 'aa.com'
$ToAddress = ab.com'
$MessageSubject = "Mailbox Size Report"
$MessageBody = "Attached is the current list of mailbox sizes."
$SendingServer = "some.mail.server.com"
$filename = "C:\test\mailboxes-$(get-date -f MMddHHssyyyy).txt"

Get-PSSnapIn -Reg | Add-PSSnapin -ea 0

###This gets the mailbox stats
Get-Mailbox | Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | ft -autosize | 
  Select-Object DisplayName, @{name="TotalItemSize(MB)";expression={$_.TotalItemSize.Value.ToMB()}}, ItemCount | out-file $filename -width 1000

###This creates the mail message and adds the stats text file as an attachment
$SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress, 
$MessageSubject, $MessageBody
$Attachment = New-Object Net.Mail.Attachment($filename)
$SMTPMessage.Attachments.Add($Attachment)

###This section sends the message
$SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer
$SMTPClient.Send($SMTPMessage)

Open in new window

mailboxes-031012062011.txt
0
Comment
Question by:Travis Hahn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 12

Expert Comment

by:prashanthd
ID: 35106186
Hi,

Try the following...change line no 14

Get-Mailbox | Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | ft DisplayName, @{name="TotalItemSize(MB)";expression={$_.TotalItemSize.Value.ToMB()}}, ItemCount -autosize | out-file $filename
0
 

Author Comment

by:Travis Hahn
ID: 35108876
Almost - but it added a "last Logon: colum as well...

See Below
mailboxes-031107272011.txt
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 35115934

Why not use Export-Csv?

Or even ConvertTo-Html if you want it inline in the mail.

Chris
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

Author Comment

by:Travis Hahn
ID: 35128655
Because I dont want my inbox clogged up with HTML or CSV files, and I don't want a CSV or HTML file sitting on my email server storage.  A text file takes up far less size.  Plus it used to work just fine in Exchange 2007
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 35128782

CSV is plain text, it should take up less space than your TXT file simply because it doesn't have all the white space required to justify the columns.

Chris
0
 

Author Comment

by:Travis Hahn
ID: 35131521
okay so would the code then be | Export-CSV $filename  

Get-Mailbox | Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | ft DisplayName, @{name="TotalItemSize(MB)";expression={$_.TotalItemSize.Value.ToMB()}}, ItemCount -autosize | Export-Csv $filename

or would I have to give the exact location c:\test\ $filename?
0
 
LVL 71

Accepted Solution

by:
Chris Dent earned 2000 total points
ID: 35132301
Relative names are fine, but for this example I've used a full name as you did originally, then I'd use Send-MailMessage to send the mail rather than the longer way you have from older versions (Send-MailMessage is new with PowerShell 2).
$FromAddress = 'aa.com'
$ToAddress = 'ab.com'
$MessageSubject = "Mailbox Size Report"
$MessageBody = "Attached is the current list of mailbox sizes."
$SendingServer = "some.mail.server.com"
$Filename = "C:\test\mailboxes-$(get-date -f MMddHHssyyyy).txt"

Get-Mailbox | Get-MailboxStatistics | 
  Sort-Object TotalItemSize -Descending | 
  Select-Object DisplayName, @{name="TotalItemSize(MB)";expression={ $_.TotalItemSize.Value.ToMB() }}, ItemCount |
  Export-Csv $Filename -NoTypeInformation

Send-MailMessage -To $ToAddress -From $FromAddress -SmtpServer $SendingServer -Subject $MessageSubject `
  -Body $MessageBody -Attachments $FileName

Open in new window

The only thing I should note is the ` character on the Send-MailMessage line. That lets it carry the parameters onto the next line, it's important that it remains.

HTH

Chris
0
 

Author Comment

by:Travis Hahn
ID: 35132367
I changed line 6 to .CSV and it was all good.  And I went from 24 lines to 14 lines of code.  Thank you Chris; much appreciated.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Multi-threading long-running processes can have a significant increase in overall performance and drastically decrease over time it takes for a process to complete. Unfortunately, not all applications support native multi-threading, some by design a…
Introducing Priority Question, our latest feature.
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Suggested Courses
Course of the Month10 days, 23 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question