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

%3CLINGO-SUB%20id%3D%22lingo-sub-1098876%22%20slang%3D%22en-US%22%3EException%20calling%20%22Bind%22%20with%20%222%22%20argument(s)%3A%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1098876%22%20slang%3D%22en-US%22%3E%3CP%3EI%20want%20to%20run%20a%20PowerShell%20Script%20that%20can%20read%20the%20number%20of%20unread%20mail%20in%20an%20Exchange%20Server.%3C%2FP%3E%3CP%3EHas%20installed%20Microsoft%20Exchange%20Web%20Service%20V2.2%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20title%3D%22https%3A%2F%2Fgallery.technet.microsoft.com%2Foffice%2FGet-unread-email-count-c815d66e%22%20href%3D%22https%3A%2F%2Fgallery.technet.microsoft.com%2Foffice%2FGet-unread-email-count-c815d66e%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgallery.technet.microsoft.com%2Foffice%2FGet-unread-email-count-c815d66e%3C%2FA%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3EMy%20script%20looks%20like%20this%3A%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3E%5Bvoid%5D%5BSystem.Reflection.Assembly%5D%3A%3ALoadWithPartialName('Microsoft.VisualBasic')%0A%0A%24AccountWithImpersonationRights%20%3D%20%5BMicrosoft.VisualBasic.Interaction%5D%3A%3AInputBox(%22Account%20With%20Impersonation%20Rights%3A%22)%0A%24MailboxToImpersonate%20%3D%20%5BMicrosoft.VisualBasic.Interaction%5D%3A%3AInputBox(%22Account%20that%20you%20want%20to%20check%3A%22)%0A%24dllpath%20%3D%20%22C%3A%5CProgram%20Files%5CMicrosoft%5CExchange%5CWeb%20Services%5C2.2%5CMicrosoft.Exchange.WebServices.dll%22%0AImport-Module%20%24dllpath%0A%24ExchangeVersion%20%3D%20%5BMicrosoft.Exchange.WebServices.Data.ExchangeVersion%5D%3A%3AExchange2013%0A%24service%20%3D%20New-Object%20Microsoft.Exchange.WebServices.Data.ExchangeService(%24ExchangeVersion)%0A%24psCred%20%3D%20Get-Credential%0A%24creds%20%3D%20New-Object%20System.Net.NetworkCredential(%24psCred.UserName.ToString()%2C%24psCred.GetNetworkCredential().password.ToString())%0A%24service.Credentials%20%3D%20%24creds%0A%24service.AutodiscoverUrl(%24AccountWithImpersonationRights%20%2C%7B%24true%7D)%0AWrite-Host%20'Using%20'%20%24AccountWithImpersonationRights%20'%20to%20Impersonate%20'%20%24MailboxToImpersonate%0A%24service.ImpersonatedUserId%20%3D%20New-Object%20Microsoft.Exchange.WebServices.Data.ImpersonatedUserId(%5BMicrosoft.Exchange.WebServices.Data.ConnectingIdType%5D%3A%3ASmtpAddress%2C%24MailboxToImpersonate%20)%3B%0A%24InboxFolder%3D%20new-object%20Microsoft.Exchange.WebServices.Data.FolderId(%5BMicrosoft.Exchange.WebServices.Data.WellKnownFolderName%5D%3A%3AInbox%2C%24ImpersonatedMailboxName)%0A%24Inbox%20%3D%20%5BMicrosoft.Exchange.WebServices.Data.Folder%5D%3A%3ABind(%24service%2C%24InboxFolder)%0AWrite-Host%20'Total%20Item%20count%20for%20Inbox%3A'%20%24Inbox.TotalCount%0AWrite-Host%20'Total%20Items%20Unread%3A'%20%24Inbox.UnreadCount%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EIt%20gives%20me%20the%20following%20error%3A%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3EException%20calling%20%22Bind%22%20with%20%222%22%20argument(s)%3A%20%22The%20account%20does%20not%20have%20permission%20to%20impersonate%20the%20requested%20user.%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EPS%20C%3A%5CWINDOWS%5Csystem32%26gt%3B%20C%3A%5CUsers%5CMyName%5CDesktop%5CUnreadEmailCoint.ps1%0Acmdlet%20Get-Credential%20at%20command%20pipeline%20position%201%0ASupply%20values%20for%20the%20following%20parameters%3A%0AUsing%20MyName%40bielefeldt.eu%20to%20Impersonate%20MyName%40bielefeldt.eu%0AException%20calling%20%22Bind%22%20with%20%222%22%20argument(s)%3A%20%22The%20account%20does%20not%20have%20permission%20to%20impersonate%20the%20requested%20user.%22%0AAt%20C%3A%5CUsers%5CMyName%5CDesktop%5CUnreadEmailCoint.ps1%3A16%20char%3A1%0A%2B%20%24Inbox%20%3D%20%5BMicrosoft.Exchange.WebServices.Data.Folder%5D%3A%3ABind(%24service%2C%20...%0A%2B%20~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%0A%2B%20CategoryInfo%20%3A%20NotSpecified%3A%20(%3A)%20%5B%5D%2C%20MethodInvocationException%0A%2B%20FullyQualifiedErrorId%20%3A%20ServiceResponseException%0A%0ATotal%20Item%20count%20for%20Inbox%3A%0ATotal%20Items%20Unread%3A%0A%0APS%20C%3A%5CWINDOWS%5Csystem32%26gt%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EWhat%20kind%20of%20permissions%20do%20I%20need%20to%20get%20ahead%20%3F%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1098876%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExchange%20-%20Unread%20mail%20count%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1099229%22%20slang%3D%22en-US%22%3ERe%3A%20Exception%20calling%20%22Bind%22%20with%20%222%22%20argument(s)%3A%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1099229%22%20slang%3D%22en-US%22%3E%3CP%3EYou%20need%20Full%20Access%20permissions%20on%20the%20mailbox%20or%20EWS%20impersonation%20permissions%20granted%20to%20run%20such%20scripts%20against%20users%20other%20than%20your%20own.%20And%20since%20you%20are%20using%20a%20custom%20script%2C%20best%20address%20such%20questions%20to%20the%20author%2C%20you%20can%20use%20the%20Gallery%20Q%26amp%3BA%20functionality%20for%20that.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Visitor

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
Highlighted

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.