Solved

Powershell Script - Formatting

Posted on 2011-03-10
8
728 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
 

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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

You may have a outside contractor who comes in once a week or seasonal to do some work in your office but you only want to give him access to the programs and files he needs and keep privet all other documents and programs, can you do this on a loca…
Scam emails are a huge burden for many businesses. Spotting one is not always easy. Follow our tips to identify if an email you receive is a scam.
In this video we show how to create a User Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >> Mailb…
In this video we show how to create a Distribution Group in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >>…

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now