-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathForward_Event.ps1
113 lines (86 loc) · 3.12 KB
/
Forward_Event.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#Get the required params
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[String]$Username,
[Parameter(Mandatory=$true)]
[String]$Password,
[Parameter(Mandatory=$true)]
[String]$tenantId,
[Parameter(Mandatory=$true)]
[String]$clientId,
[Parameter(Mandatory=$true)]
[String]$ClientSecret,
[Parameter(Mandatory=$true)]
[String]$ForwardSubject,
[Parameter(Mandatory=$true)]
[String]$ForwardUser,
[Parameter(Mandatory=$true)]
[String]$Note
)
# Set the web request protocol
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Azure AD OAuth Token for Graph API
# Body params
$granttype = 'password'
$scope = 'https://graph.microsoft.com/.default'
# Construct URI
$uri = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"
# Construct Body
$body = @{
client_id = $clientId
scope = $scope
client_secret = $clientSecret
grant_type = $granttype
username = $Username
password = $Password
}
# Get OAuth 2.0 Token
$tokenRequest = Invoke-WebRequest -Method Post -Uri $uri -ContentType "application/x-www-form-urlencoded" -Body $body -UseBasicParsing
# Access Token
$token = ($tokenRequest.Content | ConvertFrom-Json).access_token
# Get some information on the user who will receive the forwarded event
$ForwardDisplayURL = "https://graph.microsoft.com/beta/users/$ForwardUser"
$DisplayResponse = Invoke-WebRequest -Method "GET" -Uri $ForwardDisplayURL -Headers $Headers -UseBasicParsing | ConvertFrom-Json
$ForwardDisplay = $DisplayResponse.userPrincipalName
# Get the most updated event ID
$ContentType = "application/json"
$Headers = @{
Authorization = "Bearer $token"
"Content-Type" = $ContentType
}
$Filter = "?$" + "Select=Subject" | Out-String
$EventURL = "https://graph.microsoft.com/beta/users/$UserName/calendar/events$Filter"
$Request = Invoke-WebRequest -Method "GET" -Uri $EventURL -Headers $Headers -UseBasicParsing | ConvertFrom-Json
$EventIDs = $Request.value.id
# Check the Event IDs for matches to the desired event subject
$CalHeader = @{
Authorization = "Bearer $token"
}
ForEach ($Event in $EventIDs)
{
$CalURL = "https://graph.microsoft.com/beta/users/$CalUserName/events/$Event$Filter"
$EventRequest = Invoke-WebRequest -Method GET -Uri $CalURL -Headers $CalHeader -UseBasicParsing
$Result = $EventRequest.content | ConvertFrom-Json
$Subject = $Result.subject
# Do something with the event IDs that match the subject
If ($Subject -match $ForwardSubject)
{
$Body = @"
{
"ToRecipients":[
{
"emailAddress": {
"address": "$ForwardUser",
"name":"$ForwardDisplay"
}
}
],
"Comment": "$Note"
}
"@
# Forward the event
$ForwardURL = "https://graph.microsoft.com/beta/me/events/$Event/forward"
$SendResult = Invoke-WebRequest -Method POST -Uri $ForwardURL -Headers $Headers -Body $Body -UseBasicParsing
}
}