Exception calling "Bind" with "2" argument(s):

Copper Contributor

I want to run a PowerShell Script that can read the number of unread mail in an Exchange Server.

Has installed Microsoft Exchange Web Service V2.2

 

https://gallery.technet.microsoft.com/office/Get-unread-email-count-c815d66e

My script looks like this:

 

 

 

[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')

$AccountWithImpersonationRights = [Microsoft.VisualBasic.Interaction]::InputBox("Account With Impersonation Rights:")
$MailboxToImpersonate = [Microsoft.VisualBasic.Interaction]::InputBox("Account that you want to check:")
$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
Import-Module $dllpath
$ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
$psCred = Get-Credential
$creds = New-Object System.Net.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())
$service.Credentials = $creds
$service.AutodiscoverUrl($AccountWithImpersonationRights ,{$true})
Write-Host 'Using ' $AccountWithImpersonationRights ' to Impersonate ' $MailboxToImpersonate
$service.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress,$MailboxToImpersonate );
$InboxFolder= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$ImpersonatedMailboxName)
$Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$InboxFolder)
Write-Host 'Total Item count for Inbox:' $Inbox.TotalCount
Write-Host 'Total Items Unread:' $Inbox.UnreadCount

 

 

 

 

It gives me the following error:

Exception calling "Bind" with "2" argument(s): "The account does not have permission to impersonate the requested user."

 

PS C:\WINDOWS\system32> C:\Users\MyName\Desktop\UnreadEmailCoint.ps1
cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Using MyName@bielefeldt.eu to Impersonate MyName@bielefeldt.eu
Exception calling "Bind" with "2" argument(s): "The account does not have permission to impersonate the requested user."
At C:\Users\MyName\Desktop\UnreadEmailCoint.ps1:16 char:1
+ $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service, ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ServiceResponseException

Total Item count for Inbox:
Total Items Unread:

PS C:\WINDOWS\system32>

 

 

 

What kind of permissions do I need to get ahead ?

1 Reply

You need Full Access permissions on the mailbox or EWS impersonation permissions granted to run such scripts against users other than your own. And since you are using a custom script, best address such questions to the author, you can use the Gallery Q&A functionality for that.