Link to home
Start Free TrialLog in
Avatar of K B
K BFlag for United States of America

asked on

PowerShell: EWS script works great if I have FullAccess permissions to each mailbox/ why not with impersonation?

I do not want to grant an account full access to each mailbox.  I get errors only when I try to run impersonating mailbox.

I validated with Get-ManagementRoleAssignment -Role applicationimpersonation that I have the proper Role

I am effectively using this script https://emg.johnshopkins.edu/?p=837 against Office 365

For Office 365, I have added this line:

$service.Credentials = new-object Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList "admin@contoso.onmicrosoft.com", "Password123"

Open in new window


I get this error if I don't have full access to the mailbox:


Exception calling "Bind" with "2" argument(s): "The specified folder could not be found in the store."
At C:\scripts.new\Get-EWSCalendar.ps1:39 char:9
+         $Calendar = [Microsoft.Exchange.WebServices.Data.Folder]::Bin ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ServiceResponseException

Exception calling "FindAppointments" with "2" argument(s): "The element at position 0 is invalid
Parameter name: parentFolderIds"
At C:\scripts.new\Get-EWSCalendar.ps1:51 char:9
+         $fiItems = $service.FindAppointments($Calendar.Id, $CalendarV ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

Open in new window



Thank you.
Avatar of K B
K B
Flag of United States of America image

ASKER

EDIT:  I think this line fixed the issue but I would be open to suggestions
$service.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $mailboxname)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Vasil Michev (MVP)
Vasil Michev (MVP)
Flag of Bulgaria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial