-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInvoke-EXOLicensing.ps1
377 lines (358 loc) · 14.5 KB
/
Invoke-EXOLicensing.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<#
.NOTES
Name: Invoke-EXOLicensing.ps1
Author: Dana Garcia
Requires: Azure Automation Account, Azure Automation Runbook, Azure Active Directory,
Azure Active Directory Registered Application.
Version History:
1.0 - 12/4/2019 - Initial release.
###############################################################################
The sample scripts are not supported under any Microsoft standard support
program or service. The sample scripts are provided AS IS without warranty
of any kind. Microsoft further disclaims all implied warranties including,
without limitation, any implied warranties of merchantability or of fitness
for a particular purpose. The entire risk arising out of the use or
performance of the sample scripts and documentation remains with you. In no
event shall Microsoft, its authors, or anyone else involved in the creation,
production, or delivery of the scripts be liable for any damages whatsoever
(including, without limitation, damages for loss of business profits,
business interruption, loss of business information, or other pecuniary
loss) arising out of the use of or inability to use the sample scripts or
documentation, even if Microsoft has been advised of the possibility of such
damages.
###############################################################################
.SYNOPSIS
This script finds all unlicensed user mailboxes in the O365 tenant, and
adds them to the group used for EXO group based licensing, and notes any
work performed and/or issues found in a Power BI dataset.
.DESCRIPTION
This script check the users who are members of three different groups.
1. Licensed Users (All plans excluding Exchange)
2. Licensed Exchange Users (Only Exchange plans)
3. Licensed Disabled Users (Only Exchange and Sharepoint plans)
It then compares users who are in group 1 (licensed users) with users in
group 2 (licensed Exchange users). This allows us to identify which users
are licensed and active who don't have an Exchange license. It then checks
to see if it can pull those users mailbox settings via Graph API. If it can't
we can safely assume that they don't have a valid mailbox. If we can pull
the settings then we now know they have a mailbox without a license. We then
add them to group 2 for group based licensing. The script also compares users
who are in group 3 (licensed disabled users) with users in group 2. If there
are any users from group 2 present in group 3 they are removed. This is to
ensure proper license hygiene.
.LINK
https://www.github.com/danagarcia/EXO-Licensing
#>
param(
[Parameter(Mandatory=$True,
Position=0)][String] $TenantDomain,
[Parameter(Mandatory=$True,
Position=1)][String] $ClientID,
[Parameter(Mandatory=$True,
Position=2)][String] $ClientSecret,
[Parameter(Mandatory=$True,
Position=3)][String] $PowerBiEndPoint,
[Parameter(Mandatory=$True,
Position=4)][String] $LicensedExchangeUsersGroupID,
[Parameter(Mandatory=$True,
Position=5)][String] $DisabledExchangeUsersGroupID,
[Parameter(Mandatory=$True,
Position=6)][String] $LicensedUsersGroupID,
[Parameter(Mandatory=$True,
Position=7)][String] $DisabledLicensedUsersGroupID,
[Parameter(Mandatory=$False,
Position=8)][String] $ScopeToSingleUser
)
#Declare static variables
$loginURL = 'https://login.microsoft.com'
$resource = 'https://graph.microsoft.com'
Function Get-OAuthToken
{
param([Parameter(Mandatory=$True)][String]$TenantDomain,
[Parameter(Mandatory=$True)][String]$ClientID,
[Parameter(Mandatory=$True)][String]$ClientSecret)
#Build the OAuth request
$body = @{grant_type='client_credentials';resource=$resource;client_id=$ClientID;client_secret=$ClientSecret}
#Request OAuth token
try
{
$response = Invoke-RestMethod -Method Post -Uri $loginURL/$TenantDomain/oauth2/token?api-version=1.0 -Body $body
}
catch
{
Write-Error "Unable to obtain OAuth token, Error message: $($_.Exception.Message)"
exit
}
#Return OAuth token
return $response.access_token
}
Function Get-GroupMemberIDs
{
param([Parameter(Mandatory=$True)][String]$groupID,
[Parameter(Mandatory=$True)][String]$token)
#Declare and populate variables
$requestHeader = @{Authorization="Bearer $token"}
$requestUri = "https://graph.microsoft.com/v1.0/groups/$groupID/members?`$select=id&`$top=999"
$hasNext = $true
$result = @()
#Retrieve all member IDs from group paging through results (100 at a time)
while($hasNext)
{
try
{
$response = Invoke-WebRequest -UseBasicParsing -Headers $requestHeader -Uri $requestUri
switch($response.StatusCode)
{
200 {}
429 {
#Graph throttling, sleep for 5 seconds.
Start-Sleep -Seconds 5
$response = Invoke-WebRequest -UseBasicParsing -Headers $requestHeader -Uri $requestUri
}
default {throw}
}
if ($groupMembersResultSet.StatusCode -ne 200)
{
#Error occurred throw error
throw
}
$parsedJson = $response.Content | ConvertFrom-Json
if($parsedJson.'@odata.nextLink')
{
$requestUri = $parsedJson.'@odata.nextLink'
}
else
{
$hasNext = $false
}
$parsedJson.Value | ForEach-Object {$result += $_.id}
}
catch
{
#Write error notification and exit
Write-Host "An error occurred while retreiving group members from Graph API."
Write-Host $_.Exception.Message
Write-Host "Exiting"
exit
}
}
#All members retrieved return user id array
return $result
}
Function Add-GroupMember
{
param([Parameter(Mandatory=$True)][String]$User,
[Parameter(Mandatory=$True)][String]$Group,
[Parameter(Mandatory=$True)][String]$Token)
#Create web request values
$requestHeader = @{
"Authorization" = "Bearer $Token"
"Content-type" = "application/json"
}
$requestUri = "https://graph.microsoft.com/v1.0/groups/$Group/members/`$ref"
$requestBody = @{'@odata.id'="https://graph.microsoft.com/v1.0/directoryObjects/$User"} | ConvertTo-Json
#Add user to group
try
{
$response = Invoke-WebRequest -Method Post -UseBasicParsing -Headers $requestHeader -Uri $requestUri -Body $requestBody
switch ($response.StatusCode)
{
#Successful status code
204 {
#Return success
return @{
Status = 1
Message = "Success"
}
}
#Unsuccessful status code
default {throw}
}
}
catch
{
#Return failure
return @{
Status = 0
Message = "Error: $($_.Exception.Message)"
}
}
}
Function Remove-GroupMember
{
param([Parameter(Mandatory=$True)][String]$User,
[Parameter(Mandatory=$True)][String]$Group,
[Parameter(Mandatory=$True)][String]$Token)
#Create web request values
$requestHeader = @{Authorization = "Bearer $Token"}
$requestUri = "https://graph.microsoft.com/v1.0/groups/$Group/members/$User/`$ref"
#Remove user from group
try
{
$response = Invoke-WebRequest -Method Delete -UseBasicParsing -Headers $requestHeader -Uri $requestUri
switch ($response.StatusCode)
{
#Successful status code
204 {
#Return success
return @{
Status = 1
Message = "Success"
}
}
#Unsuccessful status code
default {throw}
}
}
catch {
#Return failure
return @{
Status = 0
Message = $_.Exception.Message
}
}
}
Function Write-LogEntry
{
param([Parameter(Mandatory=$True)][String]$User,
[Parameter(Mandatory=$True)][String]$Status,
[Parameter(Mandatory=$True)][String]$ErrorDetails,
[Parameter(Mandatory=$True)][String]$Token)
try
{
#Create web request values
$requestHeader = @{Authorization = "Bearer $Token"}
$requestUri = "https://graph.microsoft.com/v1.0/users/$User`?`$select=userPrincipalName"
#Request user information from Graph
$response = Invoke-WebRequest -UseBasicParsing -Headers $requestHeader -Uri $requestUri
#Ensure user was found
If($response.StatusCode -ne 200)
{
throw
}
#Pull User Principal Name from Graph response
$userPrincipalName = ($response.Content | ConvertFrom-Json).userPrincipalName
#Configure web request values to push data into Power BI streaming dataset
#Get endpoint from parameter
$endpoint = $PowerBiEndPoint
$payload = @{
"Time Stamp" = (Get-Date -Format "yyyy-MM-ddTHH:mm:ss.000Z").ToString()
"User Principal" = $userPrincipalName
"Status" = $Status
"Error Details" = $ErrorDetails
}
#Push data into Power BI streaming dataset
Invoke-RestMethod -Method Post -Uri $endpoint -Body (ConvertTo-Json @($payload))
}
catch
{
Write-Host $_.Exception.Message
}
}
#Get OAuth token
$token = Get-OAuthToken -TenantDomain $TenantDomain -ClientID $ClientID -ClientSecret $ClientSecret
#Request data via web request to Microsoft Graph
#Get all licensed users
$licensedUserIDs = Get-GroupMemberIDs -groupID $LicensedUsersGroupID -token $token
#Refresh token
$token = Get-OAuthToken -TenantDomain $TenantDomain -ClientID $ClientID -ClientSecret $ClientSecret
#Get all licensed Exchange users
$licensedExchangeUserIDs = Get-GroupMemberIDs -groupID $LicensedExchangeUsersGroupID -token $token
#Refresh token
$token = Get-OAuthToken -TenantDomain $TenantDomain -ClientID $ClientID -ClientSecret $ClientSecret
#Get all disabled Exchange users
$disabledExchangeUserIDs = Get-GroupMemberIDs -groupID $DisabledExchangeUsersGroupID -token $token
#Get all disabled users
$disabledLicensedUserIDs = Get-GroupMemberIDs -groupID $DisabledLicensedUsersGroupID -token $token
#Refresh token
$token = Get-OAuthToken -TenantDomain $TenantDomain -ClientID $ClientID -ClientSecret $ClientSecret
#Get licensed users that aren't licensed for Exchange
$licensedUserDif = $licensedUserIDs | Where-Object {$licensedExchangeUserIDs -notcontains $_}
#Dispose of licensed user IDs array
$licensedUserIDs = $null
#Start garbage collection
[GC]::Collect()
#Get licensed users that aren't licensed for Exchange but have been before
$reenabledUserDif = $licensedUserDif | Where-Object {$disabledExchangeUserIDs -contains $_}
#Dispose of disabled Exchange user IDs
$disabledExchangeUserIDs = $null
#Start garbage collection
[GC]::Collect()
#Filter re-enabled users from licensed users that aren't licensed for Exchange
$licensedUserDif = $licensedUserDif | Where-Object {$reenabledUserDif -notcontains $_}
#Get disabled users that are still licensed for Exchange
$disabledUserDif = $disabledLicensedUserIDs | Where-Object {$licensedExchangeUserIDs -contains $_}
#Dispose of licensed Exchange user IDs
$licensedExchangeUserIDs = $null
#Start garbage collection
[GC]::Collect()
If($ScopeToSingleUser)
{
$disabledUserDif = $disabledUserDif | Where-Object {$_ -like $ScopeToSingleUser}
$licensedUserDif = $licensedUserDif | Where-Object {$_ -like $ScopeToSingleUser}
$reenabledUserDif = $reenabledUserDif | Where-Object {$_ -like $ScopeToSingleUser}
}
#Configure URL for mailbox settings
$mailboxSettingsURL = "https://graph.microsoft.com/v1.0/users/{0}/mailboxSettings"
#Add re-enabled users to Exchange licensing group, remove them from Exchange disabled group
ForEach($user in $reenabledUserDif)
{
#Add user to Exchange licensing group
$result = Add-GroupMember -User $user -Group $LicensedExchangeUsersGroupID -Token $token
Switch ($result.Status)
{
0 {Write-LogEntry -User $user -Token $token -Status "Failed" -ErrorDetails $result.Message}
1 {
#Remove user from disabled Exchange group
$result = Remove-GroupMember -User $user -Group $DisabledExchangeUsersGroupID -Token $token
Switch ($result.Status)
{
0 {Write-LogEntry -User $user -Token $token -Status "Failed" -ErrorDetails $result.Message}
1 {Write-LogEntry -User $user -Token $token -Status "Success" -ErrorDetails "N/A"}
}
}
}
}
ForEach($user in $licensedUserDif)
{
#Get mailbox settings of each user to determine if any of the mailboxes are active but unlicensed
try
{
$mailboxSettingResponse = Invoke-WebRequest -UseBasicParsing -Headers $headerParams -Uri ($mailboxSettingsURL -f $user)
switch ($mailboxSettingResponse.StatusCode)
{
200 {
#Add user to Exchange licensing group
$result = Add-GroupMember -User $user -Group $LicensedExchangeUsersGroupID -Token $token
Switch ($result.Status)
{
0 {Write-LogEntry -User $user -Token $token -Status "Failed" -ErrorDetails $result.Message}
1 {Write-LogEntry -User $user -Token $token -Status "Success" -ErrorDetails "N/A"}
}
}
default {throw}
}
}
catch
{
#No need to handle this exception as it means the user doesn't have a mailbox we can see.
}
}
ForEach($user in $disabledUserDif)
{
#Remove user to Exchange licensing group
$result = Remove-GroupMember -User $user -Group $LicensedExchangeUsersGroupID -Token $token
Switch ($result.Status)
{
0 {Write-LogEntry -User $user -Token $token -Status "Failed - Disabled" -ErrorDetails $result.Message}
1 {
#Add user from disabled Exchange group
$result = Add-GroupMember -User $user -Group $DisabledExchangeUsersGroupID -Token $token
Switch ($result.Status)
{
0 {Write-LogEntry -User $user -Token $token -Status "Failed - Disabled" -ErrorDetails $result.Message}
1 {Write-LogEntry -User $user -Token $token -Status "Removed - Disabled" -ErrorDetails "N/A"}
}
}
}
}