Reporting on the Item Age (Count and Size) in a Mailbox using EWS and Powershell

Reporting on the Item Age (Count and Size) in a Mailbox using EWS and Powershell

This article has two really good scripts:
http://gsexdev.blogspot.com/2012/10/reporting-on-item-age-count-and-size-in.html

1. A summary script for collecting an ItemCount and Size by Year for message items in a mailbox.
2. A detailed script for collecting information on ItemCount and Size by Year and by Mailbox Folder for message items in a mailbox.

Here are some tips I had to do to get it to run:
1. I used mailboxname like this:  $MailboxName = “user@domain.com” in place of $args[0].
2. I had to change the path of the Microsoft.Exchange.WebServices.dll.
3. I changed the ExchangeVersion to match my current version.
4. I had to give the account I was authenticating full permissions to the mailbox I was running against:  add-mailboxpermission mbox -user username -accessrights fullaccess
5. When running the script and getting prompted for authentication, I had to authenticate with a upn (user@domain.com) and not using domainuser.
6. I had to adjust the out-file to a writable location.

Powershell Script to check how many emails were Sent and Received by a specific user

Powershell Script to check how many emails were Sent and Received by a specific user:

[Int] $intSent = $intRec = 0

Get-TransportServer | Get-MessageTrackingLog -ResultSize Unlimited -Start “03/09/2014” -End “03/10/2014” -Sender “user@domain.com” -EventID RECEIVE | ? {$_.Source -eq “STOREDRIVER”} | ForEach { $intSent++ }

Get-TransportServer | Get-MessageTrackingLog -ResultSize Unlimited -Start “03/09/2014” -End “03/10/2014” -Recipients “user@domain.com” -EventID DELIVER | ForEach { $intRec++ }

Write-Host “E-mails sent:    “, $intSent

Write-Host “E-mails received:”, $intRec

Disable Outlook Auto-Mapping with Mailboxes

If you wish to give a user full access to a mailbox, but do not want to have the mailbox auto-map into the users Outlook profile, assign the full access permission with with the -AutoMapping parameter.

Add-MailboxPermission mailboxname -user username -accessrights fullaccess -AutoMapping $false

Cannot install the Exchange 2013 Management Tools after Removing the Exchange 2010 Management Tools.

Scenario:  When attempting to install the Exchange 2013 Management Tools, the Exchange 2013 setup GUI shows the Management Tools as checked and grayed out even though the Exchange 2010 management tools are no longer installed on the computer.

Resolution:  Install the Management Tools from Powershell. From the Exchange Install directory, execute the following:

.setup.exe /Role:ManagementTools /IacceptExchangeServerLicenseTerms

Receiving “You don’t have sufficient permissions” when editing Distribution List membership/owner

Scenario:  You receive the following error message when editing the members/owner of a group in powershell:
You don’t have sufficient permissions. This operation can only be performed by a manager of the group.


Resolution:  Use -BypassSecurityGroupManagerCheck in the powershell command.

Adding member to Distribution List
Add-DistributionGroupMember “<Distribution Group Name>” -Member <User Name>
-BypassSecurityGroupManagerCheck

Setting the owner of a Distribution List
Set-DistributionGroup -Identity “<Distribution Group Name>” –ManagedBy <User Name>
-BypassSecurityGroupManagerCheck

Exchange Activesync Monitor for Specific Devices

Scenario:  Monitor specific ActiveSync Devices and report when a device has not made a successful ActiveSync connection for over an hour.  Report the time in local time and not Greenwich.  

Script: I ran the following Exchange PS script every hour . Depending on your requirements, you may need to manipulate or move the script around.

#Format Date to Greenwich
$currentdate = get-date
$currentdate = $currentdate.Addhours(-1)
$currentdate = $currentdate.touniversaltime()

#Pull the devices that have not connected to LastSuccessSync in over an hour
$devices = get-activesyncdevicestatistics DeviceID  | Where {$_.LastSuccessSync -lt $currentdate} | Sort LastSuccessSync | Select DeviceID, DeviceOS, deviceFriendlyName, LastSuccessSync, LastSyncAttemptTime, DeviceModel, Identity

#For the device(s) found, format the information
ForEach ($entry in $devices){
$Device = “Device: “+$entry.DeviceFriendlyName
$DeviceOS = “Device OS:   “+$entry.DeviceOS
$DeviceLastAttempt = “Last Sync Attempt (EST):   “+$entry.LastSyncAttemptTime.ToLocalTime()
$DeviceLastSync = “Last Success Sync (EST):   “+$entry.LastSuccessSync.ToLocalTime()
$DeviceModel = “Device Model:   “+$entry.DeviceModel
$DeviceIdentity = “DeviceID:   “+$entry.Identity
$DeviceIdentity = $DeviceIdentity -replace “Domain/OU/”,””
$DeviceIdentity = $DeviceIdentity -replace “/ExchangeActiveSyncDevices/”,”_”
}

#Email the results if there is a device that has not reported in over 1 hour.
If ($Devices -ne $null){
$SmtpClient = new-object system.net.mail.smtpClient 
$MailMessage = New-Object system.net.mail.mailmessage 
$SmtpClient.Host = “smtp.domain.com” 
$mailmessage.from = (“EASMonitoring@domain.com”) 
#$mailmessage.To.add(“User@domain.com”) 
$mailmessage.Subject = “Alert: A mobile device has not connected to e-mail in over 60 minutes.”
$mailmessage.Body = “The mobile device below has not connected to e-mail in over 60 minutes.
$DeviceIdentity
$Device
$DeviceOS
$DeviceLastAttempt
$DeviceLastSync

$smtpclient.Send($mailmessage)
}

Some Powershell One-Liners

​Run this one liner command to disable client access to a mailbox
Set-CASMailbox “testmbox” -EwsEnabled $false -ActiveSyncEnabled $false -MAPIEnabled $false -OWAEnabled $false -ImapEnabled $false -PopEnabled $false
Disable single item recovery and remove the mailbox from litigation hold.
 
Set-Mailbox “Mickey Mouse” -SingleItemRecoveryEnabled $false -LitigationHoldEnabled $false
 
Copy items from the Recoverable Items folder to a folder in the Discovery Search Mailbox and delete the contents from the source mailbox.
 
Search-Mailbox -Identity “testmbox” -SearchDumpsterOnly -TargetMailbox “Discovery Search Mailbox” -TargetFolder “GurinderSingh-RecoverableItems” -DeleteContent
  
If you need to delete only messages that match specified conditions, use the SearchQuery parameter to specify the conditions. This example deletes messages that have the string “card statement” in the Subject field.
 
Search-Mailbox -Identity “testmbox” -SearchQuery “Subject:’card statement'” -SearchDumpsterOnly -TargetMailbox “Discovery Search Mailbox” -TargetFolder “testmbox-RecoverableItems” -DeleteContent

Out of Office with PowerShell

Setting OOF for a mailbox. Run this command
Simple OOF
Set-MailboxAutoReplyConfiguration USERID -AutoReplyState Enabled –ExternalMessage “Message that you want to go out.” –InternalMessage “Message that you want to go out.”
To schedule run
Set-MailboxAutoReplyConfiguration USERID –AutoReplyState Scheduled –StartTime “12/19/2013” –EndTime “4/30/2014” –ExternalMessage “Message that you want to go out.” –InternalMessage “Message that you want to go out.”
To Check run
Get-MailboxAutoReplyConfiguration USERID
More Information

Reporting Mailbox Folder sizes with Powershell

​Reporting Mailbox Folder sizes with Powershell
report on individual folders and sizes:
Get-MailboxFolderStatistics testmbox
get folder stats and display folder size and items in folder:
Get-MailboxFolderStatistics testmbox | Ft name,FolderSize,ItemsinFolder
look at specific folders and sub folders:
Get-MailboxFolderStatistics testmbox -FolderScope Inbox | Select Name,FolderSize,ItemsinFolder

Exchange Script: Find ActiveSync Device Statistics for users in a Distribution Group

Scenario: You want to Find ActiveSync Device Statistics for users that are in a Distribution Group. If the Distribution Group does not contain members, it will not send the email. If it contains members, it will send an email for each member. Copy the content below and paste it into a .ps1 file and execute from Exchange Mangaement Shell.   
$mbox = Get-DistributionGroup “group-name”| Get-DistributionGroupMember
If ($mbox -ne $null)
{
$email = $mbox | ForEach {
$name = $_ | Select Name |Out-String
$body = get-activesyncdevicestatistics -mailbox $_.name | Sort DeviceFriendlyName | FT DeviceFriendlyName, DeviceModel, LastSyncAttemptTime, LastSuccessSync | Out-string
$SmtpClient = new-object system.net.mail.smtpClient
$MailMessage = New-Object system.net.mail.mailmessage
$SmtpClient.Host = “servername”
$mailmessage.from = (“EASMonitoring@domain.com”)
$mailmessage.To.add(“easstatistics@domain.com“)
$mailmessage.Subject = “EAS Statistics”
$mailmessage.Body = “
EAS Statistics for:$name
$body

$smtpclient.Send($mailmessage)
}
}