Solved

How can I get unread statistics from office 365.

Posted on 2016-09-01
5
55 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 39

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 39

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This is my first article on Expert Exchange on the Manual Method of Exporting Office 365 Mailboxes to PST format by using the eDiscovery mechanism of Office. Hope you will enjoy the article.
A brief introduction to what I consider to be the best editor for PowerShell.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …
This Experts Exchange lesson shows how to use VBA to loop through rows in Excel.  In order to sort, filter, and use database features, there needs to be a value in each column for every row. When data arrives with values missing, code to copy values…

932 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

13 Experts available now in Live!

Get 1:1 Help Now