Solved

How can I get unread statistics from office 365.

Posted on 2016-09-01
5
96 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 40

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 40

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Adoption of Microsoft’s Enterprise Mobility and Security solution and Office 365 will re-order the File Sync and Share market Microsoft has stated that its Enterprise Mobility + Security (EMS) is the fastest growing product in the history of the …
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
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…

820 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