Solved

How can I get unread statistics from office 365.

Posted on 2016-09-01
5
111 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 41

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 41

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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As cyber crime continues to grow in both numbers and sophistication, a troubling trend of optimization has emerged over the last year.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

738 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