Solved

How can I get unread statistics from office 365.

Posted on 2016-09-01
5
48 Views
Last Modified: 2016-09-05
Hi, I am using the below powershell script to report on unread items for users in in house exchange, but am looking to do the same for users with email hosted on office 365. Is this possible and what would I need to do in order to get it working?

Thanks
 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\2.2\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 ($_)}
0
Comment
Question by:Anthony Goodwin
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:François Peroux
ID: 41780822
Hello,

This script won't run under Office365 since the cmdlets are not the same in this environment.

I'll let you know what can I find on my side regarding the same functionality under Office365 and PowerShell.
0
 
LVL 38

Accepted Solution

by:
Vasil Michev (MVP) earned 500 total points
ID: 41781077
The script should work against EO, you simply need to update your "connect" part. Instead of using AutodiscoverURL, use the predefined

$Service.Url = "https://outlook.office365.com/EWS/Exchange.asmx"

Open in new window


And make sure you run it with a user that has impersonation or Full access permissions to the cloud mailboxes.
0
 

Author Comment

by:Anthony Goodwin
ID: 41782207
Hi Vasil
Thanks for looking at this, I tried your suggestion, however I get an error shown below. I am following this article to connect to O365, https://technet.microsoft.com/en-us/library/jj984289(v=exchg.160).aspx. I am able to run other powershell commands successfully but not this script.
Thanks
Ant

ForEach-Object : Exception calling "Bind" with "2" argument(s): "The request failed. The remote server returned an
error: (401) Unauthorized."At C:\PS_Script\Unread.ps1:47 char:14
+ $addresses | % {GetUnreadInboxMails($_.MailAddress)}  | % {ProcessRes ...
+              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [ForEach-Object], MethodInvocationException
    + FullyQualifiedErrorId : ServiceRequestException,Microsoft.PowerShell.Commands.ForEachObjectCommand
0
 
LVL 38

Assisted Solution

by:Vasil Michev (MVP)
Vasil Michev (MVP) earned 500 total points
ID: 41782248
You dont really need those steps, they are for regular PowerShell, the script is using EWS. As mentioned previously, you need to have either impersonation permissions or Full access on the other mailboxes in order to be able to run the script. That's what the 401 error is for.
0
 

Author Comment

by:Anthony Goodwin
ID: 41785283
Thanks Vasil, Running the script edited as you suggested with an account with Full access permissions to the mailbox queried worked perfectly. Thanks for your help.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Error 450 sending internal mail 6 21
Circular Logging 2 35
Exchange 2010/2013 Admin audits 1 19
Need to test AD authentication 3 26
Marketers need statistics and metrics like everybody else needs oxygen. In this article we explain how to enable marketing campaign statistics for Microsoft Exchange mail.
Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
To show how to generate a certificate request in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Servers >> Certificates…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now