Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How can I get unread statistics from office 365.

Posted on 2016-09-01
5
Medium Priority
?
142 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 5

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 43

Accepted Solution

by:
Vasil Michev (MVP) earned 2000 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 43

Assisted Solution

by:Vasil Michev (MVP)
Vasil Michev (MVP) earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Microsoft Office 365 is a subscriptions based service which includes services like Exchange Online and Skype for business Online. These services integrate with Microsoft's online version of Active Directory called Azure Active Directory.
Want to know how to use Exchange Server Eseutil command? Go through this article as it gives you the know-how.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

670 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