-
Notifications
You must be signed in to change notification settings - Fork 0
/
Set-AADAUDynamic.ps1
999 lines (735 loc) · 45.7 KB
/
Set-AADAUDynamic.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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
<#
.SYNOPSIS
This script automates the population of Users and Administrators to Azure AD Administrative Units.
.DESCRIPTION
Automation is completed by targeting Dynamic or Static Azure AD Groups which will populate the
Administrative Unit with Users and Administrators for the selected scope.
## Set-AADAUDynamic.ps1 [-AADAdminUnit <array[ObjectID],[ObjectID]>] [-UserGroup <string[ObjectID]>]
[-AdminGroup <array[ObjectID]>] [-GroupFilter <string[description_field*]>] [-RoleIds <array[ObjectID]>]
[-RoleIds <array[ObjectID]>] [-AutomationPSCredential <string[Name]>]
[-AutomationPSCertificate <string[Name]>] [-AutomationPSConnection <string[Name]>]
.PARAMETER AADAdminUnit
The AADAdminUnit parameter specifies the ObjectId of the Administrative Unit. You can find this
value by running Get-AzureADAdministrativeUnit
.PARAMETER UserGroup
The UserGroup parameter details the ObjectId of the Administrative Unit.
Get-AzureADGroup -SearchString "All Users"
.PARAMETER AdminGroups
The AdminGroup parameter details the ObjectId of the Administrative Unit.
Get-AzureADGroup -SearchString "All Scoped Admins"
.PARAMETER GroupFilter
The GroupFilter parameter allows to identifiy Groups that can also be added to the Administrative
Unit. By default we target the Description field of the Group. This uses the default search function
so wild cards are not needed but we recommend choosing something unique so false positives are not
included in your group filter.
.PARAMETER DeviceGroups
The DeviceFilter parameter allows for Devices be added to the Administrative Unit. Add the Object
GUID for Device groups you would like to add and all Devices within these groups will be added to the AU.
.PARAMETER DifferentialScope
The DifferentialScope parameter defines how many objects can be added or removed from the Administrative
Units in a single operation of the script. The goal of this setting is throttle bulk changes to limit the
impact of misconfigurationby an administrator. What value you choose here will be dictated by your userbase
and your script schedule. The default value is set to 10 Objects.
.PARAMETER RoleIds
The RoleIds array includes all Object GUIDs for the roles which you would like to assign. Use the
Get-AzureADDirectoryRole CMDlet to identify the the roles you would like to target.
.PARAMETER AutomationPSCredential
The AutomationPSCredential parameter defines the automation account that should be used. Please note that
this requires basic authnetication and is not prefered. Please consider using AutomationPSCertificate &
AutomationPSConnection
.PARAMETER AutomationPSCertificate
The AutomationCertificate parameter defines which Azure Automation Certificate you would like to use which
grants access to Azure AD. Parameter must be used with -AutomationPSConnection.
.PARAMETER AutomationPSConnection
The AutomationPSConnection parameter defines the connection details such as AppID, Tenant ID. Parameter
must be used with -AutomationPSCertificate.
.EXAMPLE
.\Set-AADAUDynamic.ps1 -AADAdminUnit '7b7c4926-c6d7-4ca8-9bbf-5965751022c2' -UserGroup '0e55190c-73ee-e811-80e9-005056a31be6' -AdminGroup '0e55190c-73ee-e811-80e9-005056a3' -RoleIds ('947776ed-f72c-41d3-a3c4-d97378087558','556a69f3-7b0b-45a7-b4c0-f35f1eb06dab') -GroupFilter "BU2 Managed Group*" -DeviceGroups 'b1f0f94e-7525-4292-9cc4-3cb053b9599c' -RoleIds ('0a37a06a-5eef-46a1-a3bb-c9e51ff53e72','4e8ab09b-443c-458a-8df0-c9746f4e407c')
-- SET USERS AND HELPDESK ADMINISTRATORS FOR AU --
In this example we add Users and Administrators (with Helpdesk Role) to the Administrative Unit '7b7c4926-c6d7-4ca8-9bbf-5965751022c2'
.EXAMPLE
.\Set-AADAUDynamic.ps1 -AADAdminUnit '7b7c4926-c6d7-4ca8-9bbf-5965751022c2' -UserGroup '0e55190c-73ee-e811-80e9-005056a31be6' -AdminGroup '0e55190c-73ee-e811-80e9-005056a3' -RoleIds ('569bbd46-9742-4130-ad44-0ebc4fb18374')
-- SET USERS AND HELPDESK & USER ADMINISTRATORS FOR AU --
In this example we add Users and Administrators (with Helpdesk & User Account Role) to the Administrative Unit '7b7c4926-c6d7-4ca8-9bbf-5965751022c2'
.EXAMPLE
.\Set-AADAUDynamic.ps1 -AADAdminUnit "7b7c4926-c6d7-4ca8-9bbf-5965751022c2" -UserGroup "0e55190c-73ee-e811-80e9-005056a31be6" -AdminGroups ('0e55190c-73ee-e811-80e9-005056a3','3587ebfc-bb8f-41eb-a043-02bf66361af2') -GroupFilter "Contso Managed Group" -DeviceGroups '3db29ad3-5804-41ac-82d5-4937f71f10e9' -RoleIds ('569bbd46-9742-4130-ad44-0ebc4fb18374')
-- ADD USERS, ADMINISTRATORS, GROUPS AND DEVICES TO AU --
In this example we add Users and Administrators (with Helpdesk & User Account Role) to the Administrative Unit '7b7c4926-c6d7-4ca8-9bbf-5965751022c2'
.LINK
Azure AD Dynamic Group Membership - https://docs.microsoft.com/en-us/azure/active-directory/users-groups-roles/groups-dynamic-membership
Log Analytics Workspace - https://docs.microsoft.com/en-us/azure/azure-monitor/learn/quick-create-workspace
.NOTES
IMPORTANT! This function requires that you have already created your Adminstrative Unit, the Group containing user objects and the Group containing Admin objects. You will also need the ObjectID for roles Helpdesk Administrator or User Account Administrator which can be obtained by running Get-AzureADDirectoryRole
IMPORTANT! Hey! You must use user creds or an Azure Application which is granted the following GRAPH API permissions.
Directory.Read.All - Access Groups and User Information
AdministrativeUnit.ReadWrite.All - Add/remove users, groups and devices to administrative units
RoleManagement.ReadWrite.Directory - Add/remove Scoped administrators
Quickstart: https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-app
NOTE! Please be aware that the DifferentialScope applies to both users and Administrators meaning that large changes will also impact the provensioning of Scoped Administrators.
NOTE! Script Requires the MSAL.PS Module. Run - Install-Module -Name MSAL.PS - https://www.powershellgallery.com/packages/MSAL.PS
IMPORTANT! The use of client secret is not recommended in production. It is highly recommended that you use
Certifcate based authenication.
#This code-sample is provided "AS IT IS" without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.
#This sample is not supported under any standard support program or service.
#The entire risk arising out of the use or performance of the sample and documentation remains with you.
#In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the script 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 or documentation, even if Microsoft or others has been advised of the possibility of such damages.
[AUTHOR]
Joshua Bines, Consultant
Find me on:
* Web: https://theinformationstore.com.au
* LinkedIn: https://www.linkedin.com/in/joshua-bines-4451534
* Github: https://github.com/jbines
[VERSION HISTORY / UPDATES]
0.0.1 20190514 - JBINES - Created the bare bones
0.0.2 20190221 - JBines - [Feature] Added DifferentialScope Switch, Membership Feeds.
[BUGFIX] Fixed Compare-Object bug on null values.
0.0.2 20190222 - JBines - [Feature] Added suport for Scoped Administrators, Change script error string to VAR.
0.0.3 20190304 - JBines - [Feature] Added switch for helpdesk and User admin roles. Added Loop.
[BUGFIX] Lots of testing and bug fixes.
0.0.4 20190305 - JBines - [Feature] Add support for Azure Runbook Creds
1.0.0 20190314 - JBines - [BUGFIX] Begin Process End not supported in azure automation so it has been removed. Other than that it works like a dream...
1.0.1 20190401 - JBines - [BUGFIX] Found errors with if(-not()) statements with azure automation. Also added a -top for group membership over 100 members.
1.0.2 20190402 - JBines - [BUGFIX] Reset the counter DifferentialScope for administrators so the DifferentialScope is applied independently for users and Scoped Admins
[BUGFIX] Get-AzureADAdministrativeUnitMember is limited to 100 members moved to Get-MsolAdministrativeUnitMember pull all members.
1.0.3 20191001 - CG - Changed variable $AdminGroup from String type to $AdminGroups of Array type for maximum flexibility.
1.0.4 20191021 - JBines - [BUGFIX] Added Select-Object -Unique on the $AdminGroups Array and Cleaned Up Code as suggested by CG.
1.1.0 20210621 - JBines - [Feature] Added Support for Modern Authenication via Service Principals.
1.2.0 20210713 - JBines - [Feature] Added Roles Authenication Administrator & Groups Administrator.
1.2.1 20211011 - JBines - [BUGFIX] Fixed role name change from User Account Admin to User Administrator. Allow scoped roles to be updated without removing all objects out of the group.
1.3.0 20211021 - JBines - [Feature] Script updated to support GA Azure Ad module and options to use modern auth. Removed Requirement for Connect-MsolService with improved GA scope.
[Feature] - Add Groups to Admin Unit via GroupFilter Switch.
1.3.1 20211227 - JBines - [Feature] Added switches for the Get-AutomationConnection and removed extra variables which were needed.
1.4.0 20220405 - JBines - [Feature] Added Support for the Devices population of devices. Added RoleIds array and updated script to allow targeted mangement of Roles.
2.0.0 20220419 - JBines - [Feature] Converted to use Graph API! Major rewrite and Azure AD Module discontinued. Script name changed to Set-AADAUDynamic
2.0.1 20220602 - JBines - [Feature] Added support for PS Version 7
[TO DO LIST / PRIORITY]
Migrate to Graph API / MED
Azure Managed Idenities / MED
#>
Param
(
[Parameter(Mandatory = $True)]
[ValidateNotNullOrEmpty()]
[String]$AADAdminUnit,
[Parameter(Mandatory = $True)]
[ValidateNotNullOrEmpty()]
[String]$UserGroup,
[Parameter(Mandatory = $True)]
[ValidateNotNullOrEmpty()]
[Array]$AdminGroups,
[Parameter(Mandatory = $True)]
[ValidateNotNullOrEmpty()]
[Array]$RoleIds,
[ValidateNotNullOrEmpty()]
[string]$GroupFilter,
[ValidateNotNullOrEmpty()]
[Array]$DeviceGroups,
[Parameter(Mandatory = $False)]
[ValidateNotNullOrEmpty()]
[Int]$DifferentialScope = 30,
[Parameter(Mandatory = $False)]
[ValidateNotNullOrEmpty()]
[String]$AppID,
[Parameter(Mandatory = $False)]
[ValidateNotNullOrEmpty()]
[String]$TenantID,
[Parameter(Mandatory = $False)]
[ValidateNotNullOrEmpty()]
[String]$CertificatePath,
[Parameter(Mandatory = $False)]
[ValidateNotNullOrEmpty()]
[String]$ClientSecret,
[Parameter(Mandatory = $False)]
[ValidateNotNullOrEmpty()]
[String]$AutomationPSConnection,
[Parameter(Mandatory = $False)]
[ValidateNotNullOrEmpty()]
[String]$AutomationPSCertificate
)
#Set VAR
$counter = 0
#Powershell v7
$PSDefaultParameterValues['Invoke-RestMethod:SkipHeaderValidation'] = $true
$PSDefaultParameterValues['Invoke-WebRequest:SkipHeaderValidation'] = $true
# Success Strings
$sString0 = "OUT-GraphAPI:Remove-AzureADAdministrativeUnitMember;AADAU:$AADAdminUnit"
$sString1 = "IN-GraphAPI:Add-AzureADAdministrativeUnitMember;AADAU:$AADAdminUnit"
$sString2 = "OUT-GraphAPI:Remove-AzureADScopedRoleMembership;AADAU:$AADAdminUnit"
$sString3 = "IN-GraphAPI:Add-AzureADScopedRoleMembership;AADAU:$AADAdminUnit;RoleId:"
# Info Strings
$iString0 = "Starting AAD AU Scoped Administrator Role Members"
$iString1 = "Starting AAD AU User Scope Members"
# Warn Strings
$wString0 = "CMDlet:Compare-Object;ReferenceObject:Input memberships for Users, Groups or devices is equal to NULL"
$wString1 = "CMDlet:Compare-Object;DifferenceObject:AAD AU membership is equal to NULL"
$wString2 = "CMDlet:Compare-Object;ReferenceObject:Admin Group membership is equal to NULL"
$wString3 = "CMDlet:Compare-Object;DifferenceObject:AAD AU Scoped Role membership is equal to NULL"
$wString4 = "CMDlet:Compare-Object;DifferenceObject:AAD AU Scoped Role already a member of"
# Error Strings
$eString0 = "AzureADDirectoryRole not found or more than one exists"
$eString1 = '$_.Exception.Message'
$eString2 = "Hey! You made it to the default switch. That shouldn't happen might be a null or returned value."
$eString3 = "Hey! You hit the -DifferentialScope limit of $DifferentialScope. Let's break out of this loop"
$eString4 = "Hey! Help us out and put some users in the group."
$eString5 = "objRoleIdCheck Failed. Please check and confirm the Object GUID is correct: "
# Debug Strings
$dString0 = "CMDlet:Get-AzureADAdministrativeUnit;ObjectId:$AADAdminUnit;DisplayName:$AADAdminUnit.DisplayName"
$dString1 = "Starting Scoped Role Members"
$dString2 = "RoleID Check Appling Role: "
#Load Functions
function Write-Log([string[]]$Message, [string]$LogFile = $Script:LogFile, [switch]$ConsoleOutput, [ValidateSet("SUCCESS", "INFO", "WARN", "ERROR", "DEBUG")][string]$LogLevel)
{
$Message = $Message + $Input
If (!$LogLevel) { $LogLevel = "INFO" }
switch ($LogLevel)
{
SUCCESS { $Color = "Green" }
INFO { $Color = "White" }
WARN { $Color = "Yellow" }
ERROR { $Color = "Red" }
DEBUG { $Color = "Gray" }
}
if ($Message -ne $null -and $Message.Length -gt 0)
{
$TimeStamp = [System.DateTime]::Now.ToString("yyyy-MM-dd HH:mm:ss")
if ($LogFile -ne $null -and $LogFile -ne [System.String]::Empty)
{
Out-File -Append -FilePath $LogFile -InputObject "[$TimeStamp] [$LogLevel] $Message"
}
if ($ConsoleOutput -eq $true)
{
Write-Host "[$TimeStamp] [$LogLevel] :: $Message" -ForegroundColor $Color
if($AutomationPSCredential -or $AutomationPSCertificate -or $AutomationPSConnection)
{
Write-Output "[$TimeStamp] [$LogLevel] :: $Message"
}
}
if($LogLevel -eq "ERROR")
{
Write-Error "[$TimeStamp] [$LogLevel] :: $Message"
}
}
}
Function Test-CommandExists
{
Param ($command)
$oldPreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'
try {if(Get-Command $command){RETURN $true}}
Catch {Write-Host "$command does not exist"; RETURN $false}
Finally {$ErrorActionPreference=$oldPreference}
} #end function test-CommandExists
#Validate Input Values From Parameter
Try{
#Check cred Account has all the required permissions ,Get-MailUser,Set-MailUser
If(Test-CommandExists Get-Item,Get-MsalToken){
Write-Log -Message "Correct RBAC Access Confirmed" -LogLevel DEBUG -ConsoleOutput
}
Else {Write-Log -Message "Script requires a higher level of access! You are missing the MSAL.PS PowerShell Module" -LogLevel ERROR -ConsoleOutput; Break}
If($RemoveExpiredGuests -or $RemoveInactiveGuests -and (-not $InactiveTimeSpan)){
Write-Log -Message "InactiveTimeSpan parameter is required when using RemoveExpiredGuests or RemoveInactiveGuests" -LogLevel ERROR -ConsoleOutput;
Break
}
if($CertificatePath) {
If($AppID -and $TenantID){
##Import Certificate
$Certificate = Get-Item $certificatePath
if($Certificate){
$Token = Get-MsalToken -ClientId $AppId -TenantId $TenantId -ClientCertificate $Certificate
if($?){ Write-Log -Message "Authenication via Certificate - Completed!" -LogLevel SUCCESS -ConsoleOutput }
else {
Write-Log -Message "Authenication via Certificate - Failed!" -LogLevel ERROR -ConsoleOutput;
break
}
}
else {
Write-Log -Message "No Certificate could be found! Try Get-Item certificatePath. Session will require admin access to get the private key." -LogLevel ERROR -ConsoleOutput;
Break
}
##Request Token - Cert
}
else {
Write-Log -Message "Certificate Auth also requires Switches AppID & TenantID" -LogLevel ERROR -ConsoleOutput;
Break
}
}
if($AppID -and $TenantID -and $ClientSecret) {
Write-Log -Message "Using ClientSecret for testing ONLY. Folks - not recommended!" -LogLevel WARN -ConsoleOutput
##Request Token - Cert
$Token = Get-MsalToken -clientID $AppID -ClientSecret (ConvertTo-SecureString $ClientSecret -AsPlainText -Force) -tenantID $tenantID
if($?){ Write-Log -Message "Authenication via ClientSecret - Completed!" -LogLevel SUCCESS -ConsoleOutput }
else {
Write-Log -Message "Authenication via ClientSecret - Failed!" -LogLevel ERROR -ConsoleOutput;
break
}
}
if($AutomationPSConnection -and $AutomationPSCertificate) {
#Azure Automation - Certicate Auth
$Connection = Get-AutomationConnection -Name $AutomationPSConnection
$Certificate = Get-AutomationCertificate -Name $AutomationPSCertificate
$Token = Get-MsalToken -ClientId $Connection.ApplicationId -TenantId $Connection.TenantId -ClientCertificate $Certificate
if($?){ Write-Log -Message "Azure AUTOMATION - Authenication via Certificate - Completed!" -LogLevel SUCCESS -ConsoleOutput }
Else{break}
}
Else
{
Remove-Variable AutomationPSConnection
Remove-Variable AutomationPSCertificate
}
#Obtain Access Token from $token VAR
$AccessToken = $Token.AccessToken
#Form request headers with the acquired $AccessToken
#$headers = @{'Content-Type'="application\json";'Authorization'="Bearer $AccessToken"}
$headers = @{'Content-Type'="application\json";'ConsistencyLevel'="eventual";'Authorization'="Bearer $AccessToken"}
#Check Admin Unit
#$objAADAdminUnit = Get-AzureADMSAdministrativeUnit -Id $AADAdminUnit -ErrorAction Stop
$urlAADAdminUnit = "https://graph.microsoft.com/v1.0/directory/administrativeUnits/$AADAdminUnit"
$objAADAdminUnit = Invoke-WebRequest -Method 'GET' -Uri $urlAADAdminUnit -Headers $headers -ContentType "application\json" -UseBasicParsing -ErrorAction Stop | ConvertFrom-Json
#If($?){Write-Log -Message $dString0 -LogLevel DEBUG -ConsoleOutput}
#$objUserGroup = Get-AzureADGroup -ObjectId $UserGroup -ErrorAction Stop
$urlUserGroup = "https://graph.microsoft.com/v1.0/groups/$UserGroup"
$objUserGroup = Invoke-WebRequest -Method 'GET' -Uri $urlUserGroup -Headers $headers -ContentType "application\json" -UseBasicParsing -ErrorAction Stop | ConvertFrom-Json
#New Array and Count of Users from Azure Group
#$userGroupMembers = Get-AzureADGroupMember -ObjectId $UserGroup -All:$true
$urlUserGroupMembers = "https://graph.microsoft.com/v1.0/groups/$UserGroup/transitiveMembers/microsoft.graph.user?`$count=true&`$select=displayName,id&`$top=999"
$userGroupMembers = @()
while ($null -ne $urlUserGroupMembers) {
$userGroupMembersResponse = Invoke-WebRequest -Method 'GET' -Uri $urlUserGroupMembers -Headers $headers -UseBasicParsing | ConvertFrom-Json
If($userGroupMembersResponse."@odata.count"){
$userGroupMembersCount = $userGroupMembersResponse."@odata.count"
}
$userGroupMembers += $userGroupMembersResponse.value
$urlUserGroupMembers = $userGroupMembersResponse."@odata.nextLink"
}
#New Array and Count of Users from Administrative Unit
#$administrativeUnitMembers = Get-AzureADMSAdministrativeUnitMember -Id $AADAdminUnit -All:$true
$urlAdministrativeUnitMembers = "https://graph.microsoft.com/v1.0/directory/administrativeUnits/$AADAdminUnit/members?`$select=id&`$top=99"
$administrativeUnitMembers = @()
while ($urlAdministrativeUnitMembers -ne $null) {
$AdministrativeUnitMembersResponse = Invoke-WebRequest -Method 'GET' -Uri $urlAdministrativeUnitMembers -Headers $headers -UseBasicParsing | ConvertFrom-Json
If($AdministrativeUnitMembersResponse."@odata.count"){
$AdministrativeUnitMembersCount = $AdministrativeUnitMembersResponse."@odata.count"
}
$administrativeUnitMembers += $AdministrativeUnitMembersResponse.value
$urlAdministrativeUnitMembers = $AdministrativeUnitMembersResponse."@odata.nextLink"
}
#Check all role ID's are vaild
Foreach($objRoleIdCheck in $roleIds){
$urlRoleIdCheckValue = $null
$RoleIdCheckValueResponse = $null
$urlRoleIdCheckValue = "https://graph.microsoft.com/v1.0/directoryRoles/$objRoleIdCheck"
$RoleIdCheckValueResponse = Invoke-WebRequest -Method 'GET' -Uri $urlRoleIdCheckValue -Headers $headers -UseBasicParsing -ErrorAction Stop | ConvertFrom-Json
if($RoleIdCheckValueResponse.displayName){Write-Log -Message "$dString2 $($RoleIdCheckValueResponse.displayName)" -LogLevel DEBUG -ConsoleOutput}
}
#New Array of Administrators from Administrative Unit
$administrativeUnitScopedRole = @()
Foreach($objRoleId in $roleIds){
#$administrativeUnitScopedRole += Get-AzureADMSScopedRoleMembership -Id $AADAdminUnit | Where-Object{$_.RoleId -eq $objRoleId}
$urladministrativeUnitScopedRole = "https://graph.microsoft.com/v1.0/directory/administrativeUnits/$AADAdminUnit/scopedRoleMembers"
while ($null -ne $urladministrativeUnitScopedRole) {
$administrativeUnitScopedRoleResponse = Invoke-WebRequest -Method 'GET' -Uri $urladministrativeUnitScopedRole -Headers $headers -UseBasicParsing | ConvertFrom-Json
If($administrativeUnitScopedRoleResponse."@odata.count"){
$AdministrativeUnitMembersCount = $administrativeUnitScopedRoleResponse."@odata.count"
}
$administrativeUnitScopedRole += $administrativeUnitScopedRoleResponse.value | Where-Object{$_.roleId -eq $objRoleId}
$urladministrativeUnitScopedRole = $administrativeUnitScopedRoleResponse."@odata.nextLink"
}
}
$administrativeUnitScopedRoleMembers = $administrativeUnitScopedRole.RoleMemberInfo | Select-Object id -Unique
#Create single array of admin users
#$objUserGroup = @($AdminGroups | ForEach-Object {Get-AzureADGroupMember -ObjectId $_ -All:$true -ErrorAction Stop})
$objadminGroup = @()
Foreach($objAdminGroups in $AdminGroups){
$urlAdminGroupMembers = "https://graph.microsoft.com/v1.0/groups/$objAdminGroups/transitiveMembers/microsoft.graph.user?`$select=id&`$count=true&`$top=999"
while ($null -ne $urlAdminGroupMembers) {
$adminGroupMembersResponse = Invoke-WebRequest -Method 'GET' -Uri $urlAdminGroupMembers -Headers $headers -UseBasicParsing | ConvertFrom-Json
If($adminGroupMembersResponse."@odata.count"){
$adminGroupMembersCount = $adminGroupMembersResponse."@odata.count"
}
$objadminGroup += $adminGroupMembersResponse.value
$urlAdminGroupMembers = $adminGroupMembersResponse."@odata.nextLink"
}
}
$adminGroupMembers = $null
$adminGroupMembers = $objadminGroup | Select-Object Id -Unique
#$administrativeUnitScopedRoleMembersNull = $false
#Find groups by GroupFilter Switch
If($GroupFilter){
$Groups = @()
#$Groups = Get-AzureADGroup -All:$true | Where-Object{$_.Description -like $GroupFilter}
#Switched to searchfilter based on description field
$urlGroupFilter = "https://graph.microsoft.com/v1.0/groups?`$search=`"description:$($GroupFilter)`"&`$count=true&`$top=99"
while ($null -ne $urlGroupFilter) {
$userGroupFilterResponse = Invoke-WebRequest -Method 'GET' -Uri $urlGroupFilter -Headers $headers -UseBasicParsing | ConvertFrom-Json
If($userGroupFilterResponse."@odata.count"){
$urlGroupFilter = $adminGroupMembersResponse."@odata.count"
}
$Groups += $userGroupFilterResponse.value
$urlGroupFilter = $userGroupFilterResponse."@odata.nextLink"
}
}
#Join Groups and User Arrays together
$UnitMembers = $null
$UnitMembers = $userGroupMembers.Id
if ($Groups.count -gt 0) {
$UnitMembers += $Groups.Id
}
#Find Devices
$Devices = $null
If($DeviceGroups){
#$objDevices = @($DeviceGroups | ForEach-Object {Get-AzureADGroupMember -ObjectId $_ -All:$true -ErrorAction Stop})
$objDeviceGroupMembers = @()
Foreach($objDeviceGroup in $DeviceGroups){
$deviceGroupsMembersResponse = $null
$urlDeviceGroup = "https://graph.microsoft.com/v1.0/groups/$objDeviceGroup/transitiveMembers/microsoft.graph.device?`$select=id&`$count=true&`$top=999"
while ($null -ne $urlDeviceGroup) {
$deviceGroupsMembersResponse = Invoke-WebRequest -Method 'GET' -Uri $urlDeviceGroup -Headers $headers -UseBasicParsing | ConvertFrom-Json
$objDeviceGroupMembers += $deviceGroupsMembersResponse.value
If($deviceGroupsMembersResponse."@odata.count"){
$deviceGroupsMembersCount = $deviceGroupsMembersResponse."@odata.count"
}
$urlDeviceGroup = $deviceGroupsMembersResponse."@odata.nextLink"
}
}
$Devices = $objDeviceGroupMembers | Select-Object ID -Unique
}
#Join Unitmember and Groups Arrays together
if ($Devices.count -gt 0) {
$UnitMembers += $Devices.id
}
}
Catch{
if(-not $RoleIdCheckValueResponse){Write-Log -Message $eString5 -LogLevel ERROR -ConsoleOutput}
$ErrorMessage = $_.Exception.Message
Write-Output "Try - Catch - Data Collection Failure"
If($?){Write-Log -Message $ErrorMessage -LogLevel Error -ConsoleOutput}
Break
}
Write-Log -Message $iString1 -LogLevel INFO -ConsoleOutput
Try{
#Set VAR
#$userGroupMembersNull = $false
$UnitMembersNull = $false
$administrativeUnitMembersNull = $false
$UnitMembersCount = $null
$administrativeUnitMembersCount_2 = $null
$CompareUsers = @()
$assessUsers = @()
#Compare Lists and find missing users those who should be removed.
$CompareUsers = Compare-Object -ReferenceObject $UnitMembers -DifferenceObject $administrativeUnitMembers.ID | Where-Object {$_.SideIndicator -ne "="}
If(($CompareUsers | Measure-Object).count -gt 0){
Foreach($obj in $CompareUsers){
$resultId = $obj.InputObject
$urlFindDataType = "https://graph.microsoft.com/v1.0/directoryObjects/$resultId"
$assessUsers += [pscustomobject] @{
Id = $resultId
SideIndicator = $obj.SideIndicator
'@odata.type' = (Invoke-WebRequest -Method 'GET' -Uri $urlFindDataType -Headers $headers -ContentType 'application/json' -UseBasicParsing | ConvertFrom-Json).'@odata.type'
}
}
}
}
Catch {
#Check Error for Blank Arrays
$UnitMembersCount = ($UnitMembers | measure-object).Count
$administrativeUnitMembersCount_2 = ($administrativeUnitMembers | measure-object).Count
if ($administrativeUnitMembersCount_2 -eq 0) {
If($?){Write-Log -Message $wString1 -LogLevel WARN -ConsoleOutput}
$administrativeUnitMembersNull = $True
}
if ($UnitMembersCount -eq 0) {
If($?){Write-Log -Message $wString0 -LogLevel WARN -ConsoleOutput}
$UnitMembersNull = $True
}
}
# <= -eq Add Object
# = -eq Skip
# => -eq Remove Object
if(($administrativeUnitMembersNull -ne $true) -and ($UnitMembersNull -ne $true)) {
Foreach($objUser in $assessUsers){
if ($counter -lt $DifferentialScope) {
Switch ($objUser.SideIndicator) {
"=>" {
$objID = $objUser.Id
$urlRemoveAUMember = "https://graph.microsoft.com/v1.0/directory/administrativeUnits/$AADAdminUnit/members/$objID/`$ref"
$objRemoveAUMemberResponse = Invoke-WebRequest -Method 'DELETE' -Uri $urlRemoveAUMember -Headers $headers -ContentType 'application/json' -UseBasicParsing
#Write Sucess String
if($objRemoveAUMemberResponse.StatusCode -eq 204){Write-Log -Message "$sString0;DataType:$($objUser.'@odata.type');ObjectId:$objID" -LogLevel SUCCESS -ConsoleOutput}
#Increase the count post change
$counter++
$objID = $null
}
"<=" {
$objID = $objUser.Id
$body = $null
switch -Wildcard ($objUser.'@odata.type'){
"*.User" {
$body = @{
"@odata.id" = "https://graph.microsoft.com/v1.0/users/$objID"
}
}
"*.group" {
$body = @{
"@odata.id" = "https://graph.microsoft.com/v1.0/groups/$objID"
}
}
"*.Device" {
$body = @{
"@odata.id" = "https://graph.microsoft.com/v1.0/devices/$objID"
}
}
Default {Write-Log -Message "Unable to confirm Object DataType for ObjectId:$objID" -LogLevel ERROR -ConsoleOutput}
}
If($body){
$urlAddAUMember = "https://graph.microsoft.com/v1.0/directory/administrativeUnits/$AADAdminUnit/members/`$ref"
$bodyAddAUMember = $body | ConvertTo-Json
$objAddAUMemberResponse = Invoke-WebRequest -Method 'POST' -Uri $urlAddAUMember -Headers $headers -ContentType 'application/json' -UseBasicParsing -Body $bodyAddAUMember
#Write Sucess String
if($objAddAUMemberResponse.StatusCode -eq 204){Write-Log -Message "$sString1;DataType:$($objUser.'@odata.type');ObjectId:$objID" -LogLevel SUCCESS -ConsoleOutput}
}
#Increase the count post change
$counter++
$objID = $null
}
Default {Write-log -Message $eString2 -ConsoleOutput -LogLevel ERROR }
}
}
else {
#Exceeded couter limit
Write-log -Message $eString3 -ConsoleOutput -LogLevel ERROR
Break
}
}
}
else {
#Blank group remove members from AAD AU
if ($UnitMembersNull -and (-not($administrativeUnitMembersNull))) {
if (-not($administrativeUnitMembersNull)) {
foreach($objAADAdminUnitMember in $administrativeUnitMembers){
if($counter -lt $DifferentialScope){
$objID = $objAADAdminUnitMember.ID
$urlRemoveAUMember = "https://graph.microsoft.com/v1.0/directory/administrativeUnits/$AADAdminUnit/members/$objID/`$ref"
$objRemoveAUMemberResponse = Invoke-WebRequest -Method 'DELETE' -Uri $urlRemoveAUMember -Headers $headers -ContentType 'application/json' -UseBasicParsing
#Write Sucess String
if($objRemoveAUMemberResponse.StatusCode -eq 204){Write-Log -Message "$sString0;ObjectId:$objID" -LogLevel SUCCESS -ConsoleOutput}
#Increase the count post change
$counter++
$objID = $null
}
else {
#Exceeded counter limit
Write-log -Message $eString3 -ConsoleOutput -LogLevel ERROR
Break
}
}
}
}
#Blank AAD AU add members from the User Group.
if ($administrativeUnitMembersNull -and (-not($UnitMembersNull))) {
if (-not($UnitMembersNull)) {
foreach($objUnitMember in $UnitMembers){
if($counter -lt $DifferentialScope){
$objType = $null
$body = $null
$objID = $objUnitMember
$urlFindDataType = "https://graph.microsoft.com/v1.0/directoryObjects/$objID"
$objType = (Invoke-WebRequest -Method 'GET' -Uri $urlFindDataType -Headers $headers -ContentType 'application/json' -UseBasicParsing | ConvertFrom-Json).'@odata.type'
switch -Wildcard ($objType){
"*.User" {
$body = @{
"@odata.id" = "https://graph.microsoft.com/v1.0/users/$objID"
}
}
"*.group" {
$body = @{
"@odata.id" = "https://graph.microsoft.com/v1.0/groups/$objID"
}
}
"*.Device" {
$body = @{
"@odata.id" = "https://graph.microsoft.com/v1.0/devices/$objID"
}
}
Default {Write-Log -Message "Unable to confirm Object DataType for ObjectId:$objID" -LogLevel ERROR -ConsoleOutput}
}
If($body){
$urlAddAUMember = "https://graph.microsoft.com/v1.0/directory/administrativeUnits/$AADAdminUnit/members/`$ref"
$bodyAddAUMember = $body | ConvertTo-Json
$objAddAUMemberResponse = Invoke-WebRequest -Method 'POST' -Uri $urlAddAUMember -Headers $headers -ContentType 'application/json' -UseBasicParsing -Body $bodyAddAUMember
#Write Sucess String
if($objAddAUMemberResponse.StatusCode -eq 204){Write-Log -Message "$sString1;DataType:$objType;ObjectId:$objID" -LogLevel SUCCESS -ConsoleOutput}
}
#Increase the count post change
$counter++
$objID = $null
$objDisplayName = $null
}
else {
#Exceeded couter limit
Write-log -Message $eString3 -ConsoleOutput -LogLevel ERROR
Break
}
}
}
}
else {
#Both AAD AU and the User Group are blank
Write-log -Message $eString4 -ConsoleOutput -LogLevel ERROR
}
}#End Else
Write-Log -Message $iString0 -LogLevel INFO -ConsoleOutput
#Start ScopedRoleMembers
#Reset counter for the administrators
$counter = 0
Try{
#Compare Lists and find missing administrators those who should be removed.
$CompareAdministrators = @()
$assessAdministrators = @()
Foreach($objRoleId in $roleIds){
$administrativeUnitScopedRoleMembers = ($administrativeUnitScopedRole | Where-Object{$_.RoleId -eq $objRoleId}).RoleMemberInfo
If($administrativeUnitScopedRoleMembers -and $adminGroupMembers){
$CompareAdministrators = Compare-Object -ReferenceObject $adminGroupMembers.Id -DifferenceObject $administrativeUnitScopedRoleMembers.Id | Where-Object {$_.SideIndicator -ne "="}
}
If(($CompareAdministrators | Measure-Object).count -gt 0){
Foreach($result in $CompareAdministrators){
$resultId = $result.InputObject
$assessAdministrators += [pscustomobject] @{
Id = $resultId
RoleId = $objRoleId
SideIndicator = $result.SideIndicator
ScopedRoleMembershipId = ($administrativeUnitScopedRole | Where-Object{($_.RoleId -eq $objRoleId)-and($_.RoleMemberInfo.Id -eq $resultId)}).id
}
}
}
}
#Set VAR
$adminGroupMembersNull = $false
$administrativeUnitScopedRoleMembersNull = $false
$adminGroupMembersCount = $null
$administrativeUnitScopedRoleMembersCount = $null
#Check Error for Blank Array
$adminGroupMembersCount = $adminGroupMembers.Count
$administrativeUnitScopedRoleMembersCount = $administrativeUnitScopedRoleMembers.Count
if($adminGroupMembersCount -eq 0){
If($?){Write-Log -Message $wString2 -LogLevel WARN -ConsoleOutput}
$adminGroupMembersNull = $True
}
if ($administrativeUnitScopedRoleMembersCount -eq 0) {
If($?){Write-Log -Message $wString3 -LogLevel WARN -ConsoleOutput}
$administrativeUnitScopedRoleMembersNull = $True
}
}
Catch {
Write-Error -Message $_.Exception.Message
Break
}
# <= -eq Add Object
# == -eq Skip
# => -eq Remove Object
if (($adminGroupMembersNull -ne $true) -and ($administrativeUnitScopedRoleMembersNull -ne $true)) {
Foreach($objAdmin in $assessAdministrators){
if ($counter -lt $DifferentialScope) {
Switch ($objAdmin.SideIndicator) {
"=>" {
$objID = $objAdmin.Id
$objRoleId = $objAdmin.RoleId
$objScopedRoleMembershipId = $objAdmin.ScopedRoleMembershipId
$urlRemoveAUAdmin = "https://graph.microsoft.com/v1.0/directory/administrativeUnits/$AADAdminUnit/scopedRoleMembers/$objScopedRoleMembershipId"
$objRemoveAUAdmin = Invoke-WebRequest -Method 'DELETE' -Uri $urlRemoveAUAdmin -Headers $headers -ContentType 'application/json' -UseBasicParsing
if($?){Write-Log -Message "$sString2;ObjectId:$objID" -LogLevel SUCCESS -ConsoleOutput}
#Increase the count post change
$counter++
$objID = $null
$objRoleID = $null
$objScopedRoleMembershipId = $null
}
"<=" {
$objID = $objAdmin.Id
$body = $null
$objRoleId = $objAdmin.RoleId
$body = @{
"roleId" = $objRoleId
}
$data = @{"id" = $objID}
$body.Add("roleMemberInfo",$data)
$urlAddAUAdmin = "https://graph.microsoft.com/v1.0/directory/administrativeUnits/$AADAdminUnit/scopedRoleMembers"
$bodyAddAUAdmin = $body | ConvertTo-Json
$objAddAUAdmin = Invoke-WebRequest -Method 'POST' -Uri $urlAddAUAdmin -Headers $headers -ContentType 'application/json' -UseBasicParsing -Body $bodyAddAUAdmin
#Write Sucess String
if($?){Write-Log -Message "$sString3$$objRoleId;UserObjectID:$objID" -LogLevel SUCCESS -ConsoleOutput}
#Increase the count post change
$counter++
$objID = $null
$RoleMember = $null
}
Default {Write-log -Message $eString2 -ConsoleOutput -LogLevel ERROR }
}
}
else {
#Exceeded couter limit
Write-log -Message $eString3 -ConsoleOutput -LogLevel ERROR
Break
}
}
}
else {
#Blank Admin group remove Scoped Administrators from AAD AU
if ($adminGroupMembersNull -and (-not($administrativeUnitScopedRoleMembersNull))) {
foreach($objAADAdminUnitMember in $administrativeUnitScopedRole){
if($counter -lt $DifferentialScope){
$objID = $objAADAdminUnitMember.ID
If($objAADAdminUnitMember.roleId -match $RoleIds){
$objID = $objAADAdminUnitMember.roleMemberInfo.Id
$objRoleId = $objAADAdminUnitMember.RoleId
$objScopedRoleMembershipId = $objAADAdminUnitMember.Id
$urlRemoveAUAdmin = "https://graph.microsoft.com/v1.0/directory/administrativeUnits/$AADAdminUnit/scopedRoleMembers/$objScopedRoleMembershipId"
$objRemoveAUAdmin = Invoke-WebRequest -Method 'DELETE' -Uri $urlRemoveAUAdmin -Headers $headers -ContentType 'application/json' -UseBasicParsing
if($?){Write-Log -Message "$sString2;ObjectId:$objID" -LogLevel SUCCESS -ConsoleOutput}
}
#Increase the count post change
$counter++
$objID = $null
$objRole = $null
}
else {
#Exceeded counter limit
Write-log -Message $eString3 -ConsoleOutput -LogLevel ERROR
Break
}
}
}
#Blank AAD AU Scoped Add members from the User Group.
elseif ($administrativeUnitScopedRoleMembersNull -and (-not($adminGroupMembersNull))) {
foreach($objadminGroupMember in $adminGroupMembers){
if($counter -lt $DifferentialScope){
foreach($objRoleId in $RoleIds){
$body = $null
$objID = $objadminGroupMember.Id
$body = @{
"roleId" = $objRoleId
}
$data = @{"id" = $objID}
$body.Add("roleMemberInfo",$data)
$urlAddAUAdmin = "https://graph.microsoft.com/v1.0/directory/administrativeUnits/$AADAdminUnit/scopedRoleMembers"
$bodyAddAUAdmin = $body | ConvertTo-Json
$objAddAUAdmin = Invoke-WebRequest -Method 'POST' -Uri $urlAddAUAdmin -Headers $headers -ContentType 'application/json' -UseBasicParsing -Body $bodyAddAUAdmin
if($?){Write-Log -Message "$sString3$($objAdmin.RoleId);UserObjectID:$objID" -LogLevel SUCCESS -ConsoleOutput}
}
#Increase the count post change
$counter++
$objID = $null
$objDisplayName = $null
$RoleMember = $null
}
else {
#Exceeded couter limit
Write-log -Message $eString3 -ConsoleOutput -LogLevel ERROR
Break
}
}
}
else {
#Both AAD AU and the User Group are blank
Write-log -Message $eString4 -ConsoleOutput -LogLevel ERROR
}
}#End Else