-
Notifications
You must be signed in to change notification settings - Fork 217
/
OutlookAPI_utils.ps1
63 lines (52 loc) · 1.57 KB
/
OutlookAPI_utils.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Utilities for Outlook Rest API
# Escapes string to Json
function Escape-StringToJson
{
Param(
[Parameter(Mandatory=$True)]
[String]$String
)
Process
{
# ConvertTo-Json escapes strings automatically
$String | ConvertTo-Json
}
}
# Calls Outlook Rest API
function Call-OutlookAPI
{
Param(
[Parameter(Mandatory=$True)]
[String]$AccessToken,
[Parameter(Mandatory=$False)]
$Request,
[Parameter(Mandatory=$True)]
[String]$Command,
[Parameter(Mandatory=$False)]
[ValidateSet('Get','Post','Patch','Delete')]
[String]$Method="Get",
[Parameter(Mandatory=$False)]
[ValidateSet('v1.0','v2.0','beta')]
[String]$Api="v2.0"
)
Process
{
$headers = @{
"Authorization" = "Bearer $AccessToken"
"Accept" = "text/*, multipart/mixed, application/xml, application/json; odata.metadata=none"
"Content-Type" = "application/json; charset=utf-8"
"X-AnchorMailbox" = (Read-Accesstoken $AccessToken).upn
"Prefer" = 'exchange.behavior="ActivityAccess"'
}
$url="https://outlook.office.com/api/$Api/$Command"
if($Method -ne "Post" -and $Method -ne "Patch")
{
$response=Invoke-RestMethod -UseBasicParsing -Uri $Url -Method $Method -Headers $headers
}
else
{
$response=Invoke-RestMethod -UseBasicParsing -Uri $Url -Method $Method -Headers $headers -Body $Request
}
$response.value
}
}