INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Mailbox Size Script

Mailbox Size Script

(OP)
Hello, I have the following script on my Exchange server for determining mailbox sizes.  When I run it manually, it works fine.  However, when I schedule it to run daily, it sends the same file to me every time.  So, it's not writing over the text file in c:\scripts.  If I delete the file manually, then run the script I get a new result file.  I've tried to add a remove-item in the script, but I get the following error when I do:

Remove-Item : Cannot remove item C:\scripts\mailboxes.txt: The process cannot access the file 'C:\scripts\mailboxes.txt' because it is being used by another process.
At C:\scripts\SendMailboxStats.ps1:24 char:12
+ remove-item <<<<  c:\scripts\mailboxes.txt -force
    + CategoryInfo          : WriteError: (C:\scripts\mailboxes.txt:FileInfo)
   [Remove-Item], IOException
    + FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand

CODE

###Send mailbox statistics script

###First, the administrator must change the mail message values in this section
$FromAddress = "mailboxreport@foo.com"
$ToAddress = "alewis@foo.com"
$MessageSubject = "Mailbox Size Report"
$MessageBody = "Attached is the current list of mailbox sizes."
$SendingServer = "foo.net"

###Now get the stats and store in a text file
Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | ft DisplayName,@{label="TotalItemSize(KB)";expression={$_.TotalItemSize.Value.ToKB()}}, ItemCount -auto > 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)

remove-item c:\scripts\mailboxes.txt -force

exit

RE: Mailbox Size Script

Does the account used to run the scheduled task have at least Modify permission to the C:\Scripts folder? From your description it sounds like it only has permission to create files and not also delete, which would be required to overwrite the existing file.

RE: Mailbox Size Script

(OP)
The scheduled task runs as the SYSTEM account.   

RE: Mailbox Size Script

What's the need for the text file? Does it need to be an attachment? You should be able to gather your stats and just email them without using a text file.

Not able to test right now, but

CODE --> PowerShell

$stats = Get-MailboxStatistics | Sort TotalItemSize -Descending | select DisplayName,@{label="TotalItemSize(KB)";expression={$_.TotalItemSize.Value.ToKB()}}, ItemCount

Should give you the info you need.

Stop by the new Tek-Tips group at LinkedIn. Come say hi, look for a job, have some fun. http://www.linkedin.com/groups/Networking-TekTipscom-forums-members-3952395

Pat Richard MVP
http://www.exchangeblogs.com/
 

RE: Mailbox Size Script

Also, try

CODE --> PowerShell

Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | Select-Object DisplayName,@{label="TotalItemSize(KB)";expression={$_.TotalItemSize.Value.ToKB()}}, ItemCount -auto | Out-File -FilePath "c:\scripts\mailboxes.txt"

Stop by the new Tek-Tips group at LinkedIn. Come say hi, look for a job, have some fun. http://www.linkedin.com/groups/Networking-TekTipscom-forums-members-3952395

Pat Richard MVP
http://www.exchangeblogs.com/
 

RE: Mailbox Size Script

(OP)
Pat, in your 2nd option the file remains open and I get the same result as in my script.  However, I've solved it by running a batch file later in the day that deletes the mailboxes.txt file so that a new one is created and emailed each morning.  A bit of a kluge, but it works.

RE: Mailbox Size Script

(OP)
You were right, commenting out those two lines stopped the error.

RE: Mailbox Size Script

Okay, so it's the attachment part that's locking the file.

Trying throwing the following just above your "Remove-Item" line:

CODE --> PowerShell

Remove-Variable SMTPMessage
Remove-Variable FromAddress
Remove-Variable ToAddress
Remove-Variable MessageSubject
Remove-Variable MessageBody
Remove-Variable Attachment
Remove-Variable SMTPClient
Remove-Variable SendingServer

Stop by the new Tek-Tips group at LinkedIn. Come say hi, look for a job, have some fun. http://www.linkedin.com/groups/Networking-TekTipscom-forums-members-3952395

Pat Richard MVP
http://www.exchangeblogs.com/
 

RE: Mailbox Size Script

(OP)
Unfortunately, removing the variables didn't work either.  Since we know it's the attachment code locking it, I also tried your first option like so:

CODE

$stats = Get-MailboxStatistics | Sort TotalItemSize -Descending | select DisplayName,@{label="TotalItemSize(KB)";expression={$_.TotalItemSize.Value.ToKB()}}, ItemCount

$SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress,
$MessageSubject, $stats

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

However, the email contains nothing in the body.  $stats doesn't seem to be capturing the output of Get-MailboxStatistics like using the stdout to a text file.

RE: Mailbox Size Script

Okay. You should still leave the Remove-Variables in the script just to release the memory that those are using.

I found the issue. But first, let's clean up your code a little (for the sake of best practices).

First, define the file once in a variable, and call the variable in the various spots of your script, instead of the actual file name.

Second, use Select-Object, not ft. Ft is for screen output, and can cause some heartburn when trying to output data to a file.

Third, use Out-File to output to a file, not a redirection such as ">".

Fourth, Get-MailboxStatistics requires an identity value. We can feed it all of the mailboxes by pipelining a Get-Mailbox command before that.

Last, we can use .Dispose() to free and kill the file.

CODE --> PowerShell

###Send mailbox statistics script
###First, the administrator must change the mail message values in this section
[string]$FromAddress = "mailboxreport@foo.com"
[string]$ToAddress = "alewis@foo.com"
[string]$MessageSubject = "Mailbox Size Report"
[string]$MessageBody = "Attached is the current list of mailbox sizes."
[string]$SendingServer = "foo.net"
[string]$file = "c:\scripts\mailboxes.txt"

###Now get the stats and store in a text file
Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | Select-Object DisplayName,@{label="TotalItemSize(KB)";expression={$_.TotalItemSize.Value.ToKB()}}, ItemCount | Out-File $file

###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($file)
$SMTPMessage.Attachments.Add($Attachment)

###Send the message
$SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer
$SMTPClient.Send($SMTPMessage)
$Attachment.Dispose()
Remove-Variable SMTPMessage
Remove-Variable FromAddress
Remove-Variable ToAddress
Remove-Variable MessageSubject
Remove-Variable MessageBody
Remove-Variable Attachment
Remove-Variable SMTPClient
Remove-Variable SendingServer
Remove-Variable File

If the report is always going to go to the same person, from the same address, you don't need to define those variables. You can hard code those right into the $SMTPMessage line, such as

CODE --> PowerShell

# http://www.tek-tips.com/viewthread.cfm?page=1&qid=1652631
###Send mailbox statistics script
###First, the administrator must change the mail message values in this section
[string]$file = "e:\mailboxes.txt"

###Now get the stats and store in a text file
Get-Mailbox | Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | Select-Object DisplayName,@{label="TotalItemSize(KB)";expression={$_.TotalItemSize.Value.ToKB()}}, ItemCount | Out-File $file

###Create the mail message and add the statistics text file as an attachment
$SMTPMessage = New-Object System.Net.Mail.MailMessage "mailboxreport@foo.com", "alewis@foo.com", "Mailbox Size Report", "Attached is the current list of mailbox sizes."
$Attachment = New-Object Net.Mail.Attachment($file)
$SMTPMessage.Attachments.Add($Attachment)

###Send the message
$SMTPClient = New-Object System.Net.Mail.SMTPClient("foo.net")
$SMTPClient.Send($SMTPMessage)
$Attachment.Dispose()
Remove-Variable SMTPMessage
Remove-Variable SMTPClient
Remove-Variable Attachment
Remove-Variable File

Stop by the new Tek-Tips group at LinkedIn. Come say hi, look for a job, have some fun. http://www.linkedin.com/groups/Networking-TekTipscom-forums-members-3952395

Pat Richard MVP
http://www.exchangeblogs.com/
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close