[Powershell script EWS] Working with Calendar Items using EWS managed API

In this post I will describe how we can fetch meeting details using EWS managed API.
As we are going to use the EWS make sure you met all the prerequisites before you plan to run this script.
  • 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
Note: Please install the latest version of EWS API, version 2.2 is currently being used in my script.

to find the calendar items we will bind the Service to the Calender folder and then will find the items in it.

$Folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId `
([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Calendar,$MailboxToImpersonate)
$cal=[Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$Folderid)

and then we will get the items inside the Calendar Folder, calendar view required a date range max up to 2 years can be given, below is an example on how do I get meeting items for next 7 days. 

$StartDate =(Get-Date)
$EndDate =(get-date).AddDays(7)
$CalendarView = New-Object Microsoft.Exchange.WebServices.Data.CalendarView `
($StartDate,$EndDate,$itemsView)
$findCalItems = $service.FindAppointments($Cal.Id,$CalendarView)

Next we can report the Calendar Items, we can export them to CSV or display on Console, below is my sample script.

param(
$mailbox="Sunil.Chauhan@xyz.com",
$itemsView=1000,
$userName=$cred.UserName,
$password=$cred.GetNetworkCredential().password,
$StartDate =(Get-Date),
$EndDate =(get-date).AddDays(7) # find meeting upto next 7 days.
)
#set EWS URL and Web Service DLL file location path below.
$uri=[system.URI] "https://outlook.office365.com/ews/exchange.asmx"
$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
Import-Module $dllpath

#Set Exchange Version
$ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
$service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList $userName, $password
$service.url = $uri

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

$Folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId `
([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Calendar,$mailbox)
$cal=[Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$Folderid)

#Define the calendar view
$CalendarView = New-Object Microsoft.Exchange.WebServices.Data.CalendarView($StartDate,$EndDate,$itemsView)
$findCalItems = $service.FindAppointments($Cal.Id,$CalendarView)

$report = $findCalItems | Select Start,End,Duration,AppointmentType,Subject,Location,
Organizer,DisplayTo,DisplayCC,HasAttachments,IsReminderSet,ReminderDueBy

$report | Export-Csv $($mailbox + "-Meetings.csv") -NoTypeInformation


Comments