Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

UnreadEmailCount.ps1 - Exchange 2010

$
0
0
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! :) 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



Viewing all articles
Browse latest Browse all 21975


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>