forked from mardahl/PSBucket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoMapUnifiedGroupDrives.ps1
597 lines (433 loc) · 19.7 KB
/
AutoMapUnifiedGroupDrives.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
#Requires -Version 5.0
<#
.SYNOPSIS
This script will Automap/Sync Unified Group Drives (Office 365 Groups / Teams Files) with the Next Gen OneDrive Client
.DESCRIPTION
The script will iterate through all Unified Groups and map them with the OneDrive client, if permissions are OK for the currently logged on user.
.EXAMPLE
Just run this script without any parameters in the users context
.NOTES
NAME: AutoMapUnifiedGroupDrives.ps1
VERSION: 1b
You need to have registered an App in Azure AD with the required permissions to have this script work with the Microsoft Graph API.
For this script the following permissions must be assigned to the application Files.Read.All, Group.Read.All, Directory.Read.All and Sites.Read.All
.COPYRIGHT
@michael_mardahl / https://www.iphase.dk
Some parts of the authentication functions have been heavily modified from their original state, initially provided by Microsoft as samples of Accessing Intune.
Licensed under the MIT license.
Please credit me if you fint this script useful and do some cool things with it.
#>
####################################################
#
# CONFIG
#
####################################################
#Required credentials - Get the client_id and client_secret from the app when creating it i Azure AD
$client_id = "84567bce-ff05-4g6f-a98b-fff8a94d4dcf" #App ID
$client_secret = "NShaTlgyiugliuygCvY+4IAHhjxNBnMUVFCCaSl5Q=" #API Access Key Password
#tenant_id can be read from the azure portal of your tenant (check the properties blade on your azure active directory)
$tenant_id = "8688f3f8-gg73-gggg-886b-f9a659b02231" #Directory ID
#Set to $true to delete leftover folders from previous syncs (if false, nothing wil be synced if the destination folder already exists)
$CleanupLeftovers = $true
#Seconds to wait between each mount - not having a delay can cause OneDrive to barf when adding multiple sync folders at once. (default: 3 sec)
$waitSec = 3
#Special params for some advanced modification
$global:graphApiVersion = "v1.0" #should be "v1.0"
$documentsName = "Documents" #If you have a non English tenant, then you might need to rename this your own language (default: Documents)
####################################################
#
# FUNCTIONS
#
####################################################
Function Get-AuthToken {
<#
.SYNOPSIS
This function is used to get an auth_token for the Microsoft Graph API
.DESCRIPTION
The function authenticates with the Graph API Interface with client credentials to get an access_token for working with the REST API
.EXAMPLE
Get-AuthToken -TenantID "0000-0000-0000" -ClientID "0000-0000-0000" -ClientSecret "sw4t3ajHTwaregfasdgAWREGawrgfasdgAWREGw4t24r"
Authenticates you with the Graph API interface and creates the AuthHeader to use when invoking REST Requests
.NOTES
NAME: Get-AuthToken
#>
param
(
[Parameter(Mandatory=$true)]
$TenantID,
[Parameter(Mandatory=$true)]
$ClientID,
[Parameter(Mandatory=$true)]
$ClientSecret
)
try{
# Define parameters for Microsoft Graph access token retrieval
$resource = "https://graph.microsoft.com"
$authority = "https://login.microsoftonline.com/$TenantID"
$tokenEndpointUri = "$authority/oauth2/token"
# Get the access token using grant type client_credentials for Application Permissions
$content = "grant_type=client_credentials&client_id=$ClientID&client_secret=$ClientSecret&resource=$resource"
$response = Invoke-RestMethod -Uri $tokenEndpointUri -Body $content -Method Post -UseBasicParsing
Write-Host "Got new Access Token!" -ForegroundColor Green
Write-Host
# If the accesstoken is valid then create the authentication header
if($response.access_token){
# Creating header for Authorization token
$authHeader = @{
'Content-Type'='application/json'
'Authorization'="Bearer " + $response.access_token
'ExpiresOn'=$response.expires_on
}
return $authHeader
}
else{
Write-Error "Authorization Access Token is null, check that the client_id and client_secret is correct..."
break
}
}
catch{
FatalWebError -Exeption $_.Exception -Function "Get-AuthToken"
}
}
####################################################
Function Get-ValidToken {
<#
.SYNOPSIS
This function is used to identify a possible existing Auth Token, and renew it using Get-AuthToken, if it's expired
.DESCRIPTION
Retreives any existing Auth Token in the session, and checks for expiration. If Expired, it will run the Get-AuthToken Fucntion to retreive a new valid Auth Token.
.EXAMPLE
Get-ValidToken
Authenticates you with the Graph API interface by reusing a valid token if available - else a new one is requested using Get-AuthToken
.NOTES
NAME: Get-ValidToken
#>
#Fixing client_secret illegal char (+), which do't go well with web requests
$client_secret = $($client_secret).Replace("+","%2B")
# Checking if authToken exists before running authentication
if($global:authToken){
# Get current time in (UTC) UNIX format (and ditch the milliseconds)
$CurrentTimeUnix = $((get-date ([DateTime]::UtcNow) -UFormat +%s)).split((Get-Culture).NumberFormat.NumberDecimalSeparator)[0]
# If the authToken exists checking when it expires (converted to minutes for readability in output)
$TokenExpires = [MATH]::floor(([int]$authToken.ExpiresOn - [int]$CurrentTimeUnix) / 60)
if($TokenExpires -le 0){
Write-Host "Authentication Token expired" $TokenExpires "minutes ago! - Requesting new one..." -ForegroundColor Green
$global:authToken = Get-AuthToken -TenantID $tenant_id -ClientID $client_id -ClientSecret $client_secret
}
else{
Write-Host "Using valid Authentication Token that expires in" $TokenExpires "minutes..." -ForegroundColor Green
Write-Host
}
}
# Authentication doesn't exist, calling Get-AuthToken function
else {
# Getting the authorization token
$global:authToken = Get-AuthToken -TenantID $tenant_id -ClientID $client_id -ClientSecret $client_secret
}
}
####################################################
Function FatalWebError {
<#
.SYNOPSIS
This function will output mostly readable error information for web request related errors.
.DESCRIPTION
Unwraps most of the exeptions details and gets the response codes from the web request, afterwards it stops script execution.
.EXAMPLE
FatalWebError -Exception $_.Exception -Function "myFunctionName"
Shows the error message and the name of the function calling it.
.NOTES
NAME: FatalWebError
#>
param
(
[Parameter(Mandatory=$true)]
$Exeption, # Should be the execption trace, you might try $_.Exception
[Parameter(Mandatory=$true)]
$Function # Name of the function that calls this function (for readability)
)
#Handles errors for all my Try/Catch'es
$errorResponse = $Exeption.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($errorResponse)
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
$responseBody = $reader.ReadToEnd();
Write-Host "Failed to execute Function : $Function" -f Red
Write-Host "Response content:`n$responseBody" -f Red
Write-Host "Request to $Uri failed with HTTP Status $($Exeption.Response.StatusCode) $($Exeption.Response.StatusDescription)" -f Red
write-host
break
}
####################################################
Function Get-UnifiedGroups(){
<#
.SYNOPSIS
This function is used to get all Unified Groups for a user in Office 365 using the Graph API REST interface
.DESCRIPTION
The function connects to the Graph API Interface and gets all groups belonging to a UPN
.EXAMPLE
Get-UnifiedGroups -UPN user@domain.com
.NOTES
NAME: Get-UnifiedGroups
PREREQUISITES: Requires a global authToken (properly formattet hashtable header) to be set as $authToken
#>
param
(
[Parameter(Mandatory=$true)]
$UPN
)
#$Resource = "myorganization/groups"
$Resource = "/users/$UPN/memberOf"
$uri = "https://graph.microsoft.com/$graphApiVersion/$Resource"
try {
Return (Invoke-RestMethod -Uri $uri -Headers $authToken -Method Get).Value
}
catch {
FatalWebError -Exeption $_.Exception -Function "Get-UnifiedGroups"
}
}
####################################################
Function Get-UnifiedGroupDrive(){
<#
.SYNOPSIS
This function is used to access the files in a specific unified group in Office 365 using the Graph API REST interface
.DESCRIPTION
The function connects to the Graph API Interface and gets all files in a unified group
.EXAMPLE
Get-UnifiedGroupFiles -groupID "00000000-0000000-0000000"
.NOTES
NAME: Get-UnifiedGroupFiles
PREREQUISITES: Requires a global authToken (properly formattet hashtable header) to be set as $authToken
#>
param
(
[Parameter(Mandatory=$true)]
$GroupID
)
$Resource = "groups/$GroupID/sites/root"
$uri = "https://graph.microsoft.com/$graphApiVersion/$Resource"
try {
Return (Invoke-RestMethod -Uri $uri -Headers $authToken -Method Get)
}
catch {
FatalWebError -Exeption $_.Exception -Function "Get-UnifiedGroupFiles"
}
}
#####################################################
Function Get-UnifiedGroupDriveList(){
<#
.SYNOPSIS
This function is used to get the List information of a specific group in Office 365 using the Graph API REST interface
.DESCRIPTION
The function connects to the Graph API Interface and gets the List data of the groups site
.EXAMPLE
Get-UnifiedGroupDriveList -GroupID "00000000-0000-0000-0000-000000000000"
Gets the List data of the unified group with id "00000000-0000-0000-0000-000000000000"
.NOTES
NAME: Get-UnifiedGroupDriveList
PREREQUISITES: Requires a global authToken (properly formattet hashtable header) to be set as $authToken
#>
param
(
[Parameter(Mandatory=$true)]
$GroupID
)
$Resource = "groups/$GroupID/sites/root/lists"
try {
$uri = "https://graph.microsoft.com/$graphApiVersion/$($Resource)"
return (Invoke-RestMethod -Uri $uri -Headers $authToken -Method Get).value
}
catch {
FatalWebError -Exeption $_.Exception -Function "Get-GroupDriveListID"
}
}
#####################################################
Function Get-CurrentUserODInfo(){
<#
.SYNOPSIS
This function is used to find the OneDrive user email and folder of the currently logged in user, matching a specific Azure AD Tennant ID
.DESCRIPTION
The function parses the HKCU registry hive, matching certain propertied with the specified TenantID - Returning userEmail and UserFolder if a match is found
.EXAMPLE
Get-CurrentUserODInfo -TennantID "00000000-0000000-0000000"
.NOTES
NAME: Get-CurrentUserODInfo
#>
param
(
[Parameter(Mandatory=$true)]
$TenantID
)
#Get OneDrive Registry Settings
$ODFBregPath = "Registry::HKEY_CURRENT_USER\Software\Microsoft\OneDrive\Accounts"
$ODFBaccounts = Get-ChildItem -Path $ODFBregPath
#resetting ODUserEmail in case script is run multiple times in same session
$ODuserEmail -eq $null
#Find the correct OneDrive Account for this tennant, in case the user has multiple OD accounts.
foreach ($Account in $ODFBaccounts) {
if ($Account.Name -match "Business") {
$ODTenant = Get-ItemProperty -Path "Registry::$($Account.Name)" | Select-Object -ExpandProperty ConfiguredTenantId
if ($ODTenant -eq $TenantID) {
$ODuserEmail = Get-ItemProperty -Path "Registry::$($Account.Name)" | Select-Object -ExpandProperty UserEmail
$ODuserFolder = Get-ItemProperty -Path "Registry::$($Account.Name)" | Select-Object -ExpandProperty UserFolder
$ODuserTenantName = Get-ItemProperty -Path "Registry::$($Account.Name)" | Select-Object -ExpandProperty DisplayName
#Getting a list of Existing MountPoints that are synced with the OneDrive Client (key might not exist is no drives are syncing, so we silently continue on any error.
$MountPoints = Get-ItemProperty -Path "Registry::$($Account.Name)\Tenants\$ODuserTenantName" -ErrorAction SilentlyContinue
}
}
}
if ($ODuserEmail -eq $null) {
Write-Error "No configured OneDrive accounts found for the configured Tenant ID! Script will exit now."
break
}
else {
#Building hashtable with our aquired OneDrive info and returning it to the caller.
$ODinfo = @{
'Email'=$ODuserEmail
'Folder'=$ODuserFolder
'TenantName'=$ODuserTenantName
'MountPoints'=$MountPoints
}
return $ODinfo
}
}
######################################################
Function Get-GroupODSyncURL(){
<#
.SYNOPSIS
This function is used to find the OneDrive user email and folder of the currently logged in user, matching a specific Azure AD Tennant ID
.DESCRIPTION
The function parses the HKCU registry hive, matching certain propertied with the specified TenantID - Returning userEmail and UserFolder if a match is found
.EXAMPLE
Get-CurrentUserODInfo -TennantID "00000000-0000000-0000000"
.NOTES
NAME: Get-CurrentUserODInfo
#>
param
(
[Parameter(Mandatory=$true)]
$GroupID,
[Parameter(Mandatory=$true)]
$UPN
)
#Executing other functions in order to get the ID's we require to build the odopen:// URL.
$DriveInfo = Get-UnifiedGroupDrive -GroupID $GroupID
$ListInfo = Get-UnifiedGroupDriveList -GroupID $GroupID
#Building our odopen:// URL from the information we have gathered, and encoding it correctly so OneDrive wont barf when we feed it.
$siteid = [System.Web.HttpUtility]::UrlEncode("{$($DriveInfo.id.Split(',')[1])}")
$webid = [System.Web.HttpUtility]::UrlEncode("{$($DriveInfo.id.Split(',')[2])}")
$listid = [System.Web.HttpUtility]::UrlEncode($ListInfo.id.Split('+')[0])
$upn = [System.Web.HttpUtility]::UrlEncode($UPN)
$webURL = [System.Web.HttpUtility]::UrlEncode($DriveInfo.webUrl)
$webtitle = [System.Web.HttpUtility]::UrlEncode($DriveInfo.DisplayName).Replace("+","%20")
$listtitle = [System.Web.HttpUtility]::UrlEncode("$documentsName")
return "odopen://sync/?siteId=$siteid&webId=$webid&listId=$listid&userEmail=$upn&webUrl=$webURL&webtitle=$webtitle&listtitle=$listtitle"
}
######################################################
Function Get-DriveMembers(){
<#
.SYNOPSIS
This function is used to access the members list of a specific unified group in Office 365 using the Graph API REST interface
.DESCRIPTION
The function connects to the Graph API Interface and gets the members of a specified Group
.EXAMPLE
Get-DriveMembers -groupID "00000000-0000000-0000000"
.NOTES
NAME: Get-DriveMembers
PREREQUISITES: Requires a global authToken (properly formattet hashtable header) to be set as $authToken
#>
param
(
[Parameter(Mandatory=$true)]
$GroupID
)
$Resource = "/groups/$GroupID/members"
$uri = "https://graph.microsoft.com/$graphApiVersion/$Resource"
try {
Return (Invoke-RestMethod -Uri $uri -Headers $authToken -Method Get).Value
}
catch {
FatalWebError -Exeption $_.Exception -Function "Get-Drive"
}
}
######################################################
function WaitForOneDrive () {
<#
.SYNOPSIS
This function will check to see if OneDrive is Running on the local machine
.DESCRIPTION
The function poll's for the OneDrive process every second, and will resume script eecution, once it's running
.EXAMPLE
WaitForOneDrive
.NOTES
NAME: WaitforOneDrive
#>
$started = $false
$maxWaitSec = 120 #maximum number of seconds we are willing to wait for the OneDrive Process. (not an exact counter, might be a bit longer)
$wait = 0 #Initial Wait counter
Do {
$status = Get-Process OneDrive -ErrorAction SilentlyContinue #Looking for the OneDrive Process
If (!($status)) {
Write-Output 'Waiting for OneDrive to start...'
Start-Sleep -Seconds 1
} Else {
Write-Output 'OneDrive has started yo!'
$started = $true
}
$wait++ #increase wait counter
If ($wait -eq $maxWaitSec) {
Write-Output "Failed to find OneDrive Process. Exiting Script!"
Exit
}
}
Until ( $started )
}
#####################################################
#
# SCRIPT EXECUTION
#
######################################################
# Wait for OneDrive Process
WaitForOneDrive
# Getting OneDrive data for currently logged in user, and matching it with the configured Tenant ID
$OneDrive = Get-CurrentUserODInfo -TenantID $tenant_id
# Calling Microsoft to see if they will give us access with the parameters defined in the config section of this script.
Get-ValidToken
# Getting a list of all O365 Unified Groups
$allUnifiedGroups = $null
$allUnifiedGroups = Get-UnifiedGroups -UPN $($OneDrive.Email)
# Getting OneDrive Sync URL's for all Unified Groups
Write-Host "Mounting OneDrive all Unified Groups in Tenant ($($OneDrive.TenantName)) that is accessible by $($OneDrive.Email)" -ForegroundColor Yellow
Write-Host
foreach ($Group in $allUnifiedGroups) {
#Skip if group is not unified
if (!$($group.groupTypes -match "Unified")){Continue}
# Validate that the users is not already Syncing the Drive
if ($OneDrive.MountPoints -match "$($Group.displayName) - $documentsName"){
Write-Host "The drive ($($Group.displayName) - $documentsName) is already synced! Skipping..." -ForegroundColor Yellow
Write-Host
continue #skip this loop and go to the next group
}
Write-Host "Attempting to sync the drive ($($Group.displayName) - $documentsName)..." -ForegroundColor Yellow
# Getting the OneDrive Sync URL for the Group Drive
$ODsyncURL = Get-GroupODSyncURL -GroupID $Group.id -UPN $OneDrive.Email
Write-Verbose $Group.displayName
Write-Verbose $ODsyncURL
# Check for leftover folders, and start sync if none found, else cleanup and start sync.
$UserHomePath = join-Path $env:HOMEDRIVE $env:HOMEPATH
$BusinessPath = Join-Path $UserHomePath $($OneDrive.TenantName)
$localSyncPath = Join-Path $BusinessPath "$($Group.displayName) - $documentsName"
if(Test-Path $localSyncPath){
Write-Host "Leftover Folder Found for $localSyncPath" -ForegroundColor Red
if ($CleanupLeftovers -eq $true) {
Write-Host '$CleanupLeftovers is set to true - Deleting old folder and starting sync' -ForegroundColor Yellow
Remove-Item -Path $localSyncPath -Force -Recurse
Start $ODsyncURL
Sleep -Seconds $waitSec
}
} else {
Write-Host "The drive ($($Group.displayName) - $documentsName) is NOT synced! Adding to OneDrive client..." -ForegroundColor Yellow
Start $ODsyncURL
Sleep -Seconds $waitSec
}
Write-Host
}