This script was created after a business requirement to ensure an important shared mailbox be regularly checked. The requester wanted to be sure that someone manually checked the mailbox for new mail at least every quarter of an hour, and an e-mail be sent to a distribution group if this did not happen. After a lot of searching I was unable to find a script that performed the task, I therefore decided to whip one up.
PowerShell doesn’t have the most advanced mailbox querying features, and you can’t check for aspects such as whether the e-mail has been read. The user had to change their manual processes somewhat in order to be sure that once the e-mail had been read that it was moved to a subfolder.
I broke the process down into 6 steps, these are:
1. Retrieve the current date, set it into a manageable format and store this info as a variable.
2. Provide some variables which you can amend around e-mail delivery and message text.
3. Query mailbox to retrieve date of oldest item in Inbox.
4. Create a variable with oldest item value in same format as the current date variable
5. Compare the time of the oldest mail in the Inbox to current time.
6. Send a mail if more than 10 minutes has elapsed.
The main issue I had with this script was comparing the current date with the date returned by the Get-MailBoxFolderStatistics command.
Running get-date returns the date in the FullDateTimePattern format.
get-date
Whereas the Get-MailboxFolderStatistics command returns the date in the General format.
Get-MailboxFolderStatistics -Identity mailbox -FolderScope Inbox -IncludeOldestAndNewestItems | where-object {$_.FolderPath -eq "/Inbox"} | Select-Object -Property OldestItemReceivedDate
The fix for this is to change the value returned from get-date to a General type with the code
(get-date).ToString('G')
It may sound look like I am a compendium of knowledge around time formats, but the information actually came from the extremely handy article on Technet.
The other issue I had was around the filtering of the Get-MailboxFolderStatistics command, so that only the Inbox (and no subfolders) were queried. After a bit of trial and error I isolated that the filter | where-object {$_.FolderPath -eq “/Inbox”} did the trick.
The complete script is below, let me know if you find it useful.
####################################################################### # Author : Tom Anderson # Date : 18-03-2013 # Comment : This script checks the Oldest Item Receive Date for mails # : in the specified Mailbox. If a mail older than 10 minutes # : is present an e-mail notification is sent. # History : ####################################################################### #### Add Exchange PowerShell Snap-In Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 #### Set current date and correct format $date = (get-date).ToString('G') ### Set Mailbox identity $mailbox = "yourmailbox" #### E-mail variables $smtpServer = "smtp.yourcompany.com" $recipient = "[email protected]" $sender = "[email protected]" $Subject = "The process has fallen over" $Body = "There is an e-mail older than 10 minutes in your $mailbox inbox." #### Query Inbox of mailbox to retrieve date of oldest item $OIRD = Get-MailboxFolderStatistics -Identity $mailbox -FolderScope Inbox -IncludeOldestAndNewestItems | where-object {$_.FolderPath -eq "/Inbox"} #### Create variable with above value in same format as the $date variable $OIRDInfo = $OIRD.oldestitemreceiveddate #### Compare the time of the oldest mail in the Inbox to current time. #### A mail is sent if more than 10 minutes has elapsed. $compare = new-timespan -start $OIRDInfo -end $date if ($compare.TotalMinutes -gt 10) {Send-MailMessage -From "$sender" -To "$recipient" -Subject "$Subject" -Body "$body" -smtpserver "$smtpServer"}
This script is also listed on the Technet Script Repository.
I do not understand how this scripts it is active?
Work up to : Get-MailboxFolderStatistics -Identity $mailbox -FolderScope Inbox -IncludeOldestAndNewestItems | where-object {$_.FolderPath -eq “/Inbox”}…
I can not send email
What version of Exchange are you running?
Also long as you have the $mailbox variable set as Firstname.Lastname it should work.
Tom
I have an Exchange 2010
From PS I see my last message, but if you launch the script does not generate the message to the recipient. I think correctly entered parameters
Also for exemple i modified /inbox with “posta in arrivo” for italian Language..
I hope to make it work because it’s a great script
You may need to add this to the start of the script:
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
The best way to troubleshoot it is to break it down a bit. If you open PowerShell and run
$mailbox = "firstname.lastname"
Get-MailboxFolderStatistics -Identity $mailbox -FolderScope Inbox -IncludeOldestAndNewestItems | where-object {$_.FolderPath -eq "/posta in arrivo"} | Select-Object -Property
Do you get a result?
Tom
from mailboxserver not work. I can find a result only if I change the script like this
Get-MailboxFolderStatistics -Identity “[email protected]” -FolderScope Inbox -IncludeOldestAndNewestItems | where-object {$_.FolderPath -eq “/posta in arrivo”}
I have a outpu with the result
OldestItemReceivedDate : 08/01/2014 16:46:33
NewestItemReceivedDate : 28/02/2014 23:06:43
but now? What can I do? however thank you very much for your support
OK you should be able to strip out some of the extra e-mail bits to ensure it works as desired.
Does running the above return a number? if so the script is working as planned.
If I launch the script from Exchange shell i have this error message:
Select-Object : Missing an argument for parameter ‘Property’. Specify a parameter of type ‘System.Object[]’ and try aga
in.
At E:\exch2010\Scripts\Checkmaildroso.ps1:2 char:202
+ $OIRD = Get-MailboxFolderStatistics -Identity [email protected] -FolderScope Inbox -IncludeOldestAndNewestIt
ems | where-object {$_.FolderPath -eq “/posta in arrivo”} | Select-Object -Property <<<<
+ CategoryInfo : InvalidArgument: (:) [Select-Object], ParameterBindingException
+ FullyQualifiedErrorId : MissingArgument,Microsoft.PowerShell.Commands.SelectObjectCommand
New-TimeSpan : Cannot bind parameter 'Start' to the target. Exception setting "Start": "Object reference not set to an
instance of an object."
At E:\exch2010\Scripts\Checkmaildroso.ps1:4 char:31
+ $compare = new-timespan -start <<<< $OIRDInfo -end $date
+ CategoryInfo : WriteError: (:) [New-TimeSpan], ParameterBindingException
+ FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.NewTimeSpanCommand
If i remove | Select-Object -Property and write the result to .txt i have a number which increases
..sorry i modified with
$compare.minutes -gt 5 > e:\test.txt
on txt always appears “true”
OK not sure what’s going on here. It has been some time since I wrote this script. If I remove “Select-Object -Property” from the end then it still returns the date, however it’s GMT (this may or may not be an issue for you). I can see how the select statement would change any date so I think you’re safe to remove that.
I will do some testing to confirm.
OK I have updated the script and it should now work. I have changed $compare.minutes to $compare.totalminutes to more accurately reflect any older mails.
Thanks for your feedback and testing, sorry not sure how that erroneous select statement got in there.
Hi,
With the same command I am not able to find out oldest mail in Inplace Archive of Exchnage 2013 mailbox.The result is comming as a blank date.
Any Advise?
Hi ,
I am running the below command to get the last received mail date for the Archived mailbox. But this is not returning any result.Can you please help on this.We are using exchange 2013.
Get-MailboxFolderStatistics -Identity [email protected] -Archive -FolderScope Inbox -IncludeOldestAndNewestItems | where-object {$_.FolderPath -eq “/Inbox”} | Select-Object -Property OldestItemReceivedDate
Hi Deb,
I don’t have a 2013 instance to test on, but
Get-MailboxFolderStatistics -Identity [email protected] -IncludeOldestAndNewestItems | Where {$_.Name -match “Inbox”} | Select-Object -Property OldestItemReceivedDate
Should work, according to this: http://www.oxfordsbsguy.com/2014/04/14/exchange-powershell-how-to-check-the-number-of-items-in-the-inbox-sent-items-deleted-items-junk-email/