Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

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

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...What you have done for people like me is immeasurably helpful."

Geography

Where in the world do Tek-Tips members come from?
Aaron333 (TechnicalUser)
22 Jun 11 13:19
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
crobin1 (MIS)
23 Jun 11 9:05
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.
Aaron333 (TechnicalUser)
23 Jun 11 11:34
The scheduled task runs as the SYSTEM account.   
58sniper (MIS)
27 Jun 11 10:22
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/
 

58sniper (MIS)
27 Jun 11 10:37
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/
 

Aaron333 (TechnicalUser)
30 Jun 11 13:39
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.
58sniper (MIS)
30 Jun 11 14:04
Just for giggles, comment out the two attachment related lines and see if you still have the problem of the file being held.

Also, you can remove the "exit" line at the bottom.

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/
 

Aaron333 (TechnicalUser)
30 Jun 11 14:09
You were right, commenting out those two lines stopped the error.
58sniper (MIS)
30 Jun 11 14:18
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/
 

Aaron333 (TechnicalUser)
30 Jun 11 14:46
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.
58sniper (MIS)
1 Jul 11 21:39
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/
 

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!

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