Wednesday, February 25, 2015

Call Exchange Web servies API through PowerShell

I had to test whether a given user has a given meeting requests in a calendar in Exchange. Therefore, I wrote this little piece of PowerShell script, that’s using Microsoft Exchange Web Services Managed API, to find the Exchange server based on user's email address. Then make a call to the user's calendar, where I could check if there was create meeting request.

Step 1. Download and execute the the Microsoft Exchange Web Services Managed API 2.0 http://www.microsoft.com/en-us/download/details.aspx?id=35371

Step 2. Change the PowerShell script
Change this variable:
$exchangeversion
$pathToExchangeDll
$folderName
$usersmtpadress
$userlogin
$userdomain
$userpassword

Step 3. Run the PowerShell script

1 $exchangeversion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010
2 $pathToExchangeDll = "c:\ExchangeWebserviceApi\Microsoft.Exchange.WebServices.dll"
3 $folderName = [Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Calendar
4 $usersmtpadress = "user@domain.dk"
5 $userlogin = "userlogind"
6 $userdomain = "domain"
7 $userpassword = "P@ssword!"
8
9 #callback function
10 function AutodiscoverCallback
11 {
12 param (
13 [System.String]
14 $Url
15 )
16 return $true
17 }
18 $callback = ${function:AutodiscoverCallback}
19 $service = new-object "Microsoft.Exchange.WebServices.Data.ExchangeService" $exchangeversion
20 $principle = new-object "Microsoft.Exchange.WebServices.Data.Mailbox" $usersmtpadress
21 $folder = new-object "Microsoft.Exchange.WebServices.Data.FolderId" $folderName, $principle
22 $connectingIdType = [Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress
23 [system.reflection.assembly]::loadfile($pathToExchangeDll) | Out-Null #e.g. c:\ExchangeWebserviceApi\Microsoft.Exchange.WebServices.dll
24
25 #setup call for Exchange web services
26 $service.Credentials = new-object "System.Net.NetworkCredential" $userlogin, $userpassword, $userdomain
27 $impersonatedUserId = new-object "Microsoft.Exchange.WebServices.Data.ImpersonatedUserId" $connectingIdType, $usersmtpadress
28 $service.ImpersonatedUserId = $impersonatedUserId
29 $service.AutodiscoverUrl($usersmtpadress, $callback)
30
31 #calling Exchange web services
32 $principleCalendar = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service, $folder)
33 "User {0}'s calendar" -f $userlogin
34 $principleCalendar

1 comment: