[Powershell Script]Creating a task using EWS Managed API

This script is going to use the EWS managed API, please met the below requirements.
  • Admin account should have application impersonation rights, you can follow this MSDN post to setup the permissions.
  • Install EWS managed API on system where you plan to run the script from, if API is not installed, download the same from here
To create a Task we will be using "Microsoft.Exchange.WebServices.Data.task" class.

$task=New-Object Microsoft.Exchange.WebServices.Data.task -ArgumentList $service

After that we can check the property for the task using the Get-Member.


 Now we know what property we can assign, for this example we will just use the basic properties like, Subject, Body, and DueDate and then we will save the task by calling $task.Save() method.
$task.subject="This is test task"
$task.body="This Test task was created By EWS Service"
$duedate=$(get-date).adddays(2)
$task.duedate=$duedate
$task.save()

that will add the Task to user mailbox, below is the full script Code use in this example.

Import the admin credentials into PowerShell session in $cred array this script will fetch the same itself to avoid the hard code of the password in the script.

#Web Service Path
param(
$userName=$cred.UserName,
$password=$cred.GetNetworkCredential().password,
$impdUser="sunil.chauhan@mydomain.com",
$EWSServicePath = "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll",
$EWSurl = "https://outlook.office365.com/EWS/Exchange.asmx",
$duedate=$(get-date).adddays(2)
)

#Importing WebService DLL
Import-Module $EWSServicePath

#Creating Service Object
$Service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService
$ExchVer = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1
$Service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($exchver)
$service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList $userName, $password
$Service.URL = $EWSurl
$ArgumentList = ([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SMTPAddress),$impdUser
$ImpUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId -ArgumentList $ArgumentList
$service.ImpersonatedUserId = $ImpUserId

# Setting Task EWS Class and Crating a Test Task
$task=New-Object Microsoft.Exchange.WebServices.Data.task -ArgumentList $service
$task.subject="This is test task"
$task.body="This Test task was created By EWS Service"
$duedate=$(get-date).adddays(2)
$task.duedate=$duedate
$task.save()
"Adding Task to Mailbox"
"Done!"

Comments