Jump to content

Recommended Posts

Posted

Hi all,

 

I've been looking for hours and cannot find a working solution.

 

My director wants a report/CSV showing all mailboxes and number of total unread emails per mailbox.

 

A previous employee had created a ps1 but for some reason it doesn't output the correct files. It exports 2 files, users.csv which is a list of all mailboxes and unread_emails.csv which for some reason is blank?

 

Thank you!

 

The code is:

 

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


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`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

Posted (edited)

If its the latter...here you go

 

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});
    
   $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) 
{
   "Mailbox: {0}" -f $entry.EmailAddress
   "Unread Mails: {0}" -f $entry.UnreadMailCount
   "==============================================================================="
}

$addresses = Import-Csv $inputFile

$addresses | % {GetUnreadInboxMails($_.MailAddress)}  | % {ProcessResult ($_)}

 

The script has two parameters:

 

1. 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.

2. The credential used for the script needs read access to the inbox folder of each mailbox specified in the file.

Edited by featured_spectre
Posted

I ran the above as a .ps1 but got an error and the powershell closed.

 

Parameter 1, could you explain again please? Do I need to provide a CSV with MailAddress of the accounts I wish to target?

 

Thanks!!!!

 

 

The script has two parameters:

 

1. 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.

2. The credential used for the script needs read access to the inbox folder of each mailbox specified in the file.

Posted

I have the file which the other .ps1 creates. Does it need to have a specific file name to import it and use?

 

Thanks,

 

Yeah you need to create the csv containing the mailboxes you want to target.
Posted

param ([C:\Count\users.csv] $inputFile, [system.Management.Automation.PSCredential] $credential)

 

Sorry I have no idea what I am doing. Thanks for your help

 

The top [string] should be your path to the file.
Posted

Thanks for your help but the script doesn't seem to work. It pops up for 2 seconds and closes :(

 

C:\Count\users.csv has a list of addresses under MailAddress

 

Yea if that's where it's stored on your server

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...