UnreadEmailCount.ps1 - Exchange 2010

I am using the following .ps1 file an old colleague created but getting no luck.

The file creates two text documents. One with SMTP addresses, and the other to have a list of unread emails per mailbox.

It creates the two documents, but only supplied SMTP addresses. The txt file with the figures just shows a ‘0’.

Please help me! :slight_smile: Thank you.

#########################################################################

Scope: User & Unread E-mail Report Generation from AD Security Group

Use: Research

Author: Jason Shitu

Date: 13/06/2014 (Last Revision)

#########################################################################

param ([string] $inputFile = “users.csv”, [System.Management.Automation.PSCredential] $credential)
$ErrorActionPreference = “Stop”
add-pssnapin exchange -erroraction SilentlyContinue
import-module ActiveDirectory
[Reflection.Assembly]::LoadFrom(“C:\Program Files\Microsoft\Exchange\Web Services\2.0\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});

$mailbox = 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)
{
“{0}” -f $entry.UnreadMailCount
}
Write-Host “`n”;Write-Host “Unread E-mails Report, Jason Shitu, 06/2014.`n”
Write-Warning `a`a`a`a"!! FOR RESEARCH PURPOSES ONLY, NOT FOR SALE OR DISTRIBUTION !!`n`n"
$group = Read-Host “`nWhich Active Directory Group would you like to run the report against?”
Write-Host “`nCompiling report, please wait…`n”
Get-ADGroup “$group” | Get-ADGroupMember -Recursive | Get-ADUser -Properties * | Select EmailAddress | Export-Csv users.csv -Delimiter “`t” -NoTypeInformation
(Get-Content users.csv) | % {$_ -replace ‘"’, “”} | % {$_ -replace “Email”, “Mail”} | ? {$.trim() -ne “” } | out-file -FilePath users.csv -Force -Encoding ascii
if (($inputFile -eq [String]::Empty) -or (Test-Path $inputFile) -eq $false)
{
throw “Invalid file specified ({0}).” -f $inputFile
}
$addresses = Import-Csv $inputFile
$addresses | % {GetUnreadInboxMails($
.MailAddress)} | % {ProcessResult ($_)} > unread_emails.csv

So, you’re saying it isn’t retrieving the unread email count?

Are you able to do so for a single mailbox, just from the console?

I am not too sure how to run it from just the console. Please advise.

It retrieves the SMTP addresses in a one text file. If then creates a second text file which should have the unread email count but just has a 0 in the file.

Thanks.