Share via


Retrieve Exchange Online Task using EWS Managed API 2.2 and PowerShell


Retrieve Exchange Online Task using EWS Managed API 2.2 and PowerShell


Summary

This TechNet Wiki article is to demo PowerShell script which retrieves Exchange Online task items using EWS Managed API 2.2 and PowerShell.

Reference

Solution

To meet the requirement, let us create a small PowerShell function and name it as Get-xTask like shown below:

function Get-xTask 
{
 
}

Ensure you include comment-help which helps others to understand the base functionality of the script. Take a look at the code below and refer to the usage for help:

<#
.SYNOPSIS
    A PowerShell function to retrieve task items (Exchange Online)
.DESCRIPTION
    A PowerShell function to retrieve task items (Exchange Online). This script impersonate and 
    get the task information from other user mailbox if permission is delegated. 
.EXAMPLE
    PS C:\> Get-xTask -Identity "user@tenant.onmicrosoft.com" -ItemCount 10
    Retrieves 10 task item from the id user@tenant.onmicrosoft.com
.EXAMPLE 
    PS C:\> Get-xTask -Identity "user@tenant.onmicrosoft.com" -ItemCount 10 | ? {$_.}
    Retrieves 10 task item from the id user@tenant.onmicrosoft.com
.EXAMPLE 
    PS C:\> "user@tenant.onmicrosoft.com" , "user1@tenant.onmicrosoft.com" | Get-xTask -ItemCount 10
    Retrieves 10 task item from the id user@tenant.onmicrosoft.com and "user1@tenant.onmicrosoft.com"
.EXAMPLE
    PS C:\> Get-xTask -Identity "user1@tenant.onmicrosoft.com" -ItemCount 10 -Credential "admin@tenant.onmicrosoft.com"
    Retrieves 10 task item from the id user1@tenant.onmicrosoft.com with credential parameter
.EXAMPLE 
    PS C:\> Get-xTask -Identity "user@tenant.onmicrosoft.com" -ItemCount 10 | Select Subject , PercentComplete
    Retrieves 10 task item from the id user@tenant.onmicrosoft.com and display subject and PercentComplete properties
.NOTES
    @ChendrayanV
    PSBUG Using EWS API Demo (January 21, 2017)
#>

And then define the parameters as you need. In our case, we used three parameters named $Identity, $ItemCount and $Credential:

param
 (
     # Mailbox identity (user@tenant.onmicrosoft.com)
     [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
     $Identity,
 
     # Number of items to be retrieved
     [Parameter()]
     $ItemCount,
 
     # Impersonated or admin credential (By default currently logged on user credential is used)
     [Parameter()]
     [System.Management.Automation.CredentialAttribute()]
     [pscredential]
     $Credential
 
 )

Now, we will add three blocks i.e. begin{}, process{} and end{} like shown below:

Begin Block

# This block is used to provide optional one-time pre-processing for the function. 
 begin 
 {
     # Load the assembly Microsoft.Exchange.WebServices.dll from the location it is saved or installed. 
     Import-Module 'C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll'
 }

Process Block

# This block is used to provide record-by-record processing for the function
 process
 {
     # Instantiate the ExchangeService Class
     $ExchangeService = [Microsoft.Exchange.WebServices.Data.ExchangeService]::new()
     
     # If Credential parameter is used (Get Alternate Credential)
     if($PSBoundParameters.ContainsKey('Credential'))
     {
         $ExchangeService.Credentials = [System.Net.NetworkCredential]::new($Credential.UserName,$Credential.Password)
     }
     
     # Else use default credential 
     else 
     {
         $ExchangeService.UseDefaultCredentials = $true
     }
     
     # Use ImpersonatedUserID class to access other user task folder
     $ExchangeService.ImpersonatedUserId = [Microsoft.Exchange.WebServices.Data.ImpersonatedUserId]::new([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress,$Identity)
     
     # Set the URL Property - Common url for Exchange Online 
     $ExchangeService.Url = "https://outlook.office365.com/EWS/Exchange.asmx"
     
     #Define the View - $ItemCount is of type int
     $View = [Microsoft.Exchange.WebServices.Data.ItemView]::new($ItemCount)
 
     # Using FindItems method retrieve the task item
     $ExchangeService.FindItems([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Tasks,$View)
 }

End Block

# This block is used to provide optional one-time post-processing for the function.
 end 
 {
     # No need to dispose the $ExchangeService - In EWS Managed API FindItems will do the object dispose job. You can add some code if required. 
 }

Usage

help Get-xTask

help Get-xTask -Examples

Note: Refer to examples for more usage of the function Get-xTask.

Output

Full Code

function Get-xTask 
{
<#
.SYNOPSIS
    A PowerShell function to retrieve task items (Exchange Online)
.DESCRIPTION
    A PowerShell function to retrieve task items (Exchange Online). This script impersonate and 
    get the task information from other user mailbox if permission is delegated. 
.EXAMPLE
    PS C:\> Get-xTask -Identity "user@tenant.onmicrosoft.com" -ItemCount 10
    Retrieves 10 task item from the id user@tenant.onmicrosoft.com
.EXAMPLE 
    PS C:\> Get-xTask -Identity "user@tenant.onmicrosoft.com" -ItemCount 10 | ? {$_.}
    Retrieves 10 task item from the id user@tenant.onmicrosoft.com
.EXAMPLE 
    PS C:\> "user@tenant.onmicrosoft.com" , "user1@tenant.onmicrosoft.com" | Get-xTask -ItemCount 10
    Retrieves 10 task item from the id user@tenant.onmicrosoft.com and "user1@tenant.onmicrosoft.com"
.EXAMPLE
    PS C:\> Get-xTask -Identity "user1@tenant.onmicrosoft.com" -ItemCount 10 -Credential "admin@tenant.onmicrosoft.com"
    Retrieves 10 task item from the id user1@tenant.onmicrosoft.com with credential parameter
.EXAMPLE 
    PS C:\> Get-xTask -Identity "user@tenant.onmicrosoft.com" -ItemCount 10 | Select Subject , PercentComplete
    Retrieves 10 task item from the id user@tenant.onmicrosoft.com and display subject and PercentComplete properties
.NOTES
    @ChendrayanV
    PSBUG Using EWS API Demo (January 21, 2017)
#>
 param
 (
     # Mailbox identity (user@tenant.onmicrosoft.com)
     [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
     $Identity,
 
     # Number of items to be retrieved
     [Parameter(Mandatory)]
     $ItemCount,
 
     # Impersoanted or admin credential (By default currently logged on user credential is used)
     [Parameter(Mandatory)]
     [System.Management.Automation.CredentialAttribute()]
     [pscredential]
     $Credential
 
 )
 
 # This block is used to provide optional one-time pre-processing for the function. 
 begin 
 {
     # Load the assembly Microsoft.Exchange.WebServices.dll from the location it is saved or installed. 
     Import-Module 'C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll'
 }
 
 # This block is used to provide record-by-record processing for the function
 process
 {
     # Instantiate the ExchangeService Class
     $ExchangeService = [Microsoft.Exchange.WebServices.Data.ExchangeService]::new()
     
     # If Credential parameter is used (Get Alternate Credential)
     if($PSBoundParameters.ContainsKey('Credential'))
     {
         $ExchangeService.Credentials = [System.Net.NetworkCredential]::new($Credential.UserName,$Credential.Password)
     }
     
     # Else use default credential 
     else 
     {
         $ExchangeService.UseDefaultCredentials = $true
     }
     
     # Use ImpersonatedUserID class to access other user task folder
     $ExchangeService.ImpersonatedUserId = [Microsoft.Exchange.WebServices.Data.ImpersonatedUserId]::new([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress,$Identity)
     
     # Set the URL Property - Common url for Exchange Online 
     $ExchangeService.Url = "https://outlook.office365.com/EWS/Exchange.asmx"
     
     #Define the View - $ItemCount is of type int
     $View = [Microsoft.Exchange.WebServices.Data.ItemView]::new($ItemCount)
 
     # Using FindItems method retrieve the task item
     $ExchangeService.FindItems([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Tasks,$View)
 }
 
 # This block is used to provide optional one-time post-processing for the function.
 end 
 {
     # No need to dispose the $ExchangeService - In EWS Managed API FindItems will do the object dispose job. You can add some code if required. 
 }
}