-
Notifications
You must be signed in to change notification settings - Fork 6
/
Get-O365UserEvents.ps1
155 lines (138 loc) · 4.32 KB
/
Get-O365UserEvents.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<#
Microsoft provides programming examples for illustration only, without warranty either expressed or
implied, including, but not limited to, the implied warranties of merchantability and/or fitness
for a particular purpose. We grant You a nonexclusive, royalty-free right to use and modify the
Sample Code and to reproduce and distribute the object code form of the Sample Code, provided that
You agree: (i) to not use Our name, logo, or trademarks to market Your software product in which the
Sample Code is embedded; (ii) to include a valid copyright notice on Your software product in which
the Sample Code is embedded; and (iii) to indemnify, hold harmless, and defend Us and Our suppliers
from and against any claims or lawsuits, including attorneys' fees, that arise or result from the
use or distribution of the Sample Code.
#>
function Get-Token {
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)][string]$Tenant,
[Parameter(Mandatory = $true)][System.Guid]$ClientID,
[Parameter(Mandatory = $true)][string]$ClientSecret,
[Parameter(Mandatory = $true)][string]$Resource
)
begin {
}
process {
$ReqBody = @{
Grant_Type = "client_credentials"
Scope = "$Resource.default"
client_Id = $ClientID
Client_Secret = $ClientSecret
}
$RequestToken = @{
ContentType = 'application/x-www-form-urlencoded'
Method = 'POST'
Body = $ReqBody
Uri = "https://login.microsoftonline.com/$Tenant/oauth2/v2.0/token"
}
try {
$response = Invoke-RestMethod @RequestToken
}
catch {
Write-Error $_.Exception
}
}
end {
$response
}
}
function Get-AuthenticationHeaders {
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)][string]$AccessToken
)
begin {
}
process {
@{
'Content-Type' = 'application/json'
'Authorization' = "Bearer $($AccessToken)"
}
}
end {
}
}
function Get-Users {
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)][string]$AccessToken
)
begin {
$headers = Get-AuthenticationHeaders -AccessToken $AccessToken
$uri = "https://graph.microsoft.com/v1.0/users?`$filter=userType eq 'Member'"
$users = @()
}
process {
try {
$response = Invoke-RestMethod -Uri $uri -Headers $headers -Body $Payload -Method Get
$users = $response.value
}
catch {
Write-Error "Error sending batch request. Error: $($_.Exception)"
}
}
end {
$users
}
}
function Get-Events {
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)][string]$UserPrincipalName,
[Parameter(Mandatory = $true)][string]$AccessToken
)
begin {
$headers = Get-AuthenticationHeaders -AccessToken $AccessToken
$uri = "https://graph.microsoft.com/v1.0/users/$UserPrincipalName/events?`$top=3"
}
process {
try {
$response = Invoke-RestMethod -Uri $uri -Headers $headers -Body $Payload -Method Get
return @($response.value)
}
catch {
if ($_.Exception.Message.Contains("(403) Forbidden")) {
Write-Host " [ACCESS DENIED TO EVENTS]" -ForegroundColor Yellow
}
else {
Write-Error "Request Error: $($_.Exception)"
}
throw $_
}
}
}
# script requries Users.Read.All, Calendars.ReadWrite
$tenant = "contoso.onmicrosoft.com"
$clientId = "3617fd1b-xxxx-xxxx-xxxx-057668e52db0" # aka "Application ID" in Azure Portal > Azure Active Directory > App Registrations
$clientSecret = "abc123secret" # aka "Keys" in Azure Portal > Azure Active Directory > App Registrations
$token = Get-Token -Tenant $tenant -ClientID $clientId -ClientSecret $clientSecret -Resource "https://graph.microsoft.com/"
if ($token.access_token) {
$users = Get-Users -AccessToken $token.access_token
foreach ($user in $users | ? { $_.mail -ne $null }) {
Write-Host "[USER] $($user.userPrincipalName)"
try {
$events = Get-Events -AccessToken $token.access_token -UserPrincipalName $user.userPrincipalName
if ($null -ne $events -and $events.Count -gt 0) {
foreach ($event in $events) {
Write-Host " [EVENT] $($event.subject)" -ForegroundColor Green
}
}
else {
Write-Host " [NO EVENTS FOUND]" -ForegroundColor Blue
}
}
catch {}
Write-Host
}
}