Solved

Powershell Script - Formatting

Posted on 2011-03-10
8
731 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
  • 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 70

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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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 70

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 70

Accepted Solution

by:
Chris Dent earned 500 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Both MMF (multi-mode fiber) and SMF (single-mode fiber) are types of optical fiber that can aid in communication applications. These thin strands of silica or glass will allow communication to occur between devices. The transmission of light between…
MS Outlook is a world-class email client application that is mainly used for e-communication globally.  In this article, we will discuss the basic idea about MS Outlook, its advanced features, and types of MS Outlook File formats.
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
This video discusses moving either the default database or any database to a new volume.

808 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