Using EWS API in Orchestrator RRS feed

  • Question

  • Hi

    I need to use the EWS API from System Center Orchestrator. I have made a simple script to logon to a mailbox using impersonation. The script runs fine from Powershell ISE on the Orchestrator machine. But when I paste the code on a .net activity (powershell) I get the following error:

    Exception calling "Bind" with "2" argument(s): "The request failed. The remote server returned an error: (401) Unauthorized."

    This is the code:

    ## Define UPN of the Account that has impersonation rights
    $AccountWithImpersonationRights = ""
    ##Define the SMTP Address of the mailbox to impersonate
    $MailboxToImpersonate = ""

    ## Load Exchange web services DLL
    $dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"
    Import-Module $dllpath

    ## Set Exchange Version
    $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013_SP1

    ## Create Exchange Service Object
    $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion) 

    $KeyFile = "c:\sistemas\scripts\securestrings\AES.key"
    $Key = Get-Content $KeyFile
    $PasswordFile = "c:\sistemas\scripts\securestrings\admin_AES.txt"
    $secpasswd = Get-Content $PasswordFile | ConvertTo-SecureString -Key $key
    $creds = New-Object System.Net.NetworkCredential("contoso\admin",$secpasswd) 

    $service.Credentials = $creds 

    # Change the URL to point to your cas server
    $service.Url= new-object Uri("")

    ##Login to Mailbox with Impersonation

    $service.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress,$MailboxToImpersonate ); 

    #Connect to the Inbox and display basic statistics
    $InboxFolder= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$ImpersonatedMailboxName) 
    $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$InboxFolder)

    $ArchiveFolder= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::ArchiveMsgFolderRoot,$ImpersonatedMailboxName) 
    $Archive = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$ArchiveFolder)

    Tuesday, December 29, 2015 6:29 PM


All replies