Interesting question in the Exchange development forum today: How do I get the number of unread messages in the inbox folder from a number of mailboxes? The questioner wants to send a text message to each owner of a mailbox notifying him of the number of unread mails in his inbox. The requirement was to do this with PowerShell. So, here is a quick solution for this problem:
param ([string] $inputFile, [System.Management.Automation.PSCredential] $credential)
$ErrorActionPreference = "Stop"
if (($inputFile -eq [String]::Empty) -or (Test-Path $inputFile) -eq $false)
{
throw "Invalid file specified ({0})." -f $inputFile
}
[Reflection.Assembly]::LoadFrom("C:\Program Files\Microsoft\Exchange\Web Services\1.1\Microsoft.Exchange.WebServices.dll")
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService -ArgumentList([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1)
if ($credential -ne $null)
{
$service.Credentials = $credential.GetNetworkCredential()
}
else
{
$service.UseDefaultCredentials = $true;
}
function GetUnreadInboxMails([string] $emailAddress)
{
$service.AutodiscoverUrl($emailAddress, {$true});
$maibox = New-Object Microsoft.Exchange.WebServices.Data.Mailbox($emailAddress)
$folderId = New-Object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox, $mailbox)
$folder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service, $folderId);
$result = New-Object PSObject -Property @{
EmailAddress = $emailAddress
UnreadMailCount= $folder.UnreadCount;
}
return $result;
}
function ProcessResult($entry)
{
"Mailbox: {0}" -f $entry.EmailAddress
"Unread Mails: {0}" -f $entry.UnreadMailCount
"==============================================================================="
}
$addresses = Import-Csv $inputFile
$addresses | % {GetUnreadInboxMails($_.MailAddress)} | % {ProcessResult ($_)}
Just paste this script into an editor and save it as ps1 file.
The script mainly consists of two function: GetUnreadInboxMails and ProcessResult. The former method retrieves the number of unread mails in the inbox of the mailbox of the specified user. The number of unread mails can be retrieved from a Folder instance: The UnreadCount property. The method returns the number of unread mails along with the mail address of the current mailbox. The other important function is the ProcessResult method. In this script it merely dumps the result to the console. The questioner in the post linked above can use this function to call his HTTP service to send a text message.
The script has two parameters:
- The name of a CSV file containing the list of mailboxes to process. The first line should contain the column name “MailAddress” somewhere. The following lines should contain the email address of the mailbox in this column.
- A PSCredential object containing valid credentials to use for the Exchange access.
The credential used for the script needs read access to the inbox folder of each mailbox specified in the file.