Exporting Exchange Shell script output to .txt
Hi guys, Having a bit of a confounding problem with the Exchange shell. I'm new to Powershell so I apologise if this is a dumb question but I've been searching all morning and come up with nothing. Basically I'm creating this script from this link where it emails the output of the script in txt file: http://www.msexchange.org/articles_tutorials/exchange-server-2007/management-administration/getting-mailbox-statistics-exchange-2007.html# Now everything seems to work in isolation. If I copy and paste the contents of the script directly in a shell prompt it will work fine if I change the directory as c:\MailboxScript (where the script is actually saved). The issue seems to be that if I run the script directly from the .ps1 file it will try and create the text file in the default shell location of "c:\windows\system32" and attach it, but that doesn't work.. as I'd expect. What I need to know is how do I modify the script so that it runs from c:\mailboxscript , creates the .txt within that folder and attaches the file from there? My script is as follows: ###Send mailbox statistics script ###First, the administrator must change the mail message values in this section $FromAddress = "MailboxReport@xxxxxxxx.com" $ToAddress = "xxxx@xxxxxxxx.com" $MessageSubject = "Mailbox Size Report" $MessageBody = "Attached is the current list of mailbox sizes." $SendingServer = "xxxxxxxxx.xxxxxxxx.local" ###Now get the stats and store in a text file .\Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | ft DisplayName,@{label="TotalItemSize (MB)";expression={$_.TotalItemSize.Value.ToMB()}},ItemCount > mailboxes.txt ###Create the mail message and add the statistics text file as an attachment $SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress, $MessageSubject, $MessageBody $Attachment = New-Object Net.Mail.Attachment("./mailboxes.txt") $SMTPMessage.Attachments.Add($Attachment) ###Send the message $SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer $SMTPClient.Send($SMTPMessage) Any help would be greatly appreciated! Regards, Pete
January 26th, 2012 6:57am

In the script, change every reference to "mailboxes.txt" to point to an exact path. > c:\MailboxScript\mailboxes.txt Attachment("c:\MailboxScript\mailboxes.txt") and so on.... Karl My Blog: http://unlockpowershell.wordpress.com My Book: Windows PowerShell 2.0 Bible My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
Free Windows Admin Tool Kit Click here and download it now
January 26th, 2012 10:13am

Have you tried C:\mailboxscript\mailboxes.txt (MB)";expression={$_.TotalItemSize.Value.ToMB()}},ItemCount > mailboxes.txt (MB)";expression={$_.TotalItemSize.Value.ToMB()}},ItemCount > C:\mailboxscript\mailboxes.txt***** This posting is provided "AS IS" with no warranties, and confers no rights.
January 26th, 2012 10:15am

###Now get the stats and store in a text file Get-MailboxStatistics -Server pbgsemail | Sort-Object TotalItemSize -Descending | ft DisplayName,@{label="TotalItemSize(MB)";expression={$_.TotalItemSize.Value.ToMB()}},ItemCount > C:\MailboxScript\mailboxes.txt ###Create the mail message and add the statistics text file as an attachment $SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress, $MessageSubject, $MessageBody $Attachment = New-Object System.Net.Mail.Attachment("C:\MailboxScript\mailboxes.txt") $SMTPMessage.Attachments.Add($Attachment) ###Send the message $SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer $SMTPClient.Send($SMTPMessage) ***** This posting is provided "AS IS" with no warranties, and confers no rights.
Free Windows Admin Tool Kit Click here and download it now
January 26th, 2012 10:53am

Thanks Karl, That works if I copy the script directly into an Exchange Management shell and run it, but when it's saved and run as the .ps1 file it doesn't seem to want to create the .txt file before adding it as an attachment. Cheers,
January 26th, 2012 11:12am

This works great for me, when calles as a .ps1 file: ###Send mailbox statistics script ###First, the administrator must change the mail message values in this section $FromAddress = "MailboxReport@xxxxxxxx.com" $ToAddress = "karlmitschke@contoso.com" $MessageSubject = "Mailbox Size Report" $MessageBody = "Attached is the current list of mailbox sizes." $SendingServer = "mail.contoso.com" ###Now get the stats and store in a text file Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | ft DisplayName,@{label="TotalItemSize (MB)";expression={$_.TotalItemSize.Value.ToMB()}},ItemCount > c:\scripts\mailboxes.txt ###Create the mail message and add the statistics text file as an attachment $SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress, $MessageSubject, $MessageBody $Attachment = New-Object Net.Mail.Attachment("c:\scripts\mailboxes.txt") $SMTPMessage.Attachments.Add($Attachment) ###Send the message $SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer $SMTPClient.Send($SMTPMessage) Out of curiosity, are you running the .ps1 from an Exchange Management Shell? Karl My Blog: http://unlockpowershell.wordpress.com My Book: Windows PowerShell 2.0 Bible My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
Free Windows Admin Tool Kit Click here and download it now
January 26th, 2012 11:30am

I'm literally running it by right-clicking and selecting "Run with PowerShell"... which I'm 95% sure is going to make you call me a plonker! See, ideally I need this to run on a schedule. As I say, if I copy and paste the contents into an Exchange Management Shell it seems to work fine...
January 26th, 2012 12:14pm

If you are running Exchange 2007, add this line to the top of the script: Add-PSSnapin *exchange* If you are running Exchange 2010, do this at the top of the script: $ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://<FQDN of an ExchgangeServer>/PowerShell/ -Authentication Kerberos -Name "ExchangeSession" Import-PSSession $ExchangeSession Karl My Blog: http://unlockpowershell.wordpress.com My Book: Windows PowerShell 2.0 Bible My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
Free Windows Admin Tool Kit Click here and download it now
January 26th, 2012 12:30pm

You know what you are, Karl? A genius, that's what! Thank you very much!
January 26th, 2012 12:37pm

Glad to help :) KarlMy Blog: http://unlockpowershell.wordpress.com My Book: Windows PowerShell 2.0 Bible My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
Free Windows Admin Tool Kit Click here and download it now
January 26th, 2012 6:29pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics