forked from Gerenios/AADInternals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MSGraphAPI.ps1
1137 lines (956 loc) · 38.4 KB
/
MSGraphAPI.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
1000
# This script contains functions for MSGraph API at https://graph.microsoft.com
# Returns the 50 latest signin entries or the given entry
# Jun 9th 2020
function Get-AzureSignInLog
{
<#
.SYNOPSIS
Returns the 50 latest entries from Azure AD sign-in log or single entry by id
.DESCRIPTION
Returns the 50 latest entries from Azure AD sign-in log or single entry by id
.Example
Get-AADIntAccessTokenForMSGraph
PS C:\>Get-AADIntAzureSignInLog
createdDateTime id ipAddress userPrincipalName appDisplayName
--------------- -- --------- ----------------- --------------
2020-05-25T05:54:28.5131075Z b223590e-8ba1-4d54-be54-03071659f900 199.11.103.31 [email protected] Azure Portal
2020-05-29T07:56:50.2565658Z f6151a97-98cc-444e-a79f-a80b54490b00 139.93.35.110 [email protected] Azure Portal
2020-05-29T08:02:24.8788565Z ad2cfeff-52f2-442a-b8fc-1e951b480b00 11.146.246.254 [email protected] Microsoft Docs
2020-05-29T08:56:48.7857468Z e0f8e629-863f-43f5-a956-a4046a100d00 1.239.249.24 [email protected] Azure Active Directory PowerShell
.Example
Get-AADIntAccessTokenForMSGraph
PS C:\>Get-AADIntAzureSignInLog
createdDateTime id ipAddress userPrincipalName appDisplayName
--------------- -- --------- ----------------- --------------
2020-05-25T05:54:28.5131075Z b223590e-8ba1-4d54-be54-03071659f900 199.11.103.31 [email protected] Azure Portal
2020-05-29T07:56:50.2565658Z f6151a97-98cc-444e-a79f-a80b54490b00 139.93.35.110 [email protected] Azure Portal
2020-05-29T08:02:24.8788565Z ad2cfeff-52f2-442a-b8fc-1e951b480b00 11.146.246.254 [email protected] Microsoft Docs
2020-05-29T08:56:48.7857468Z e0f8e629-863f-43f5-a956-a4046a100d00 1.239.249.24 [email protected] Azure Active Directory PowerShell
PS C:\>Get-AADIntAzureSignInLog -EntryId b223590e-8ba1-4d54-be54-03071659f900
id : b223590e-8ba1-4d54-be54-03071659f900
createdDateTime : 2020-05-25T05:54:28.5131075Z
userDisplayName : admin company
userPrincipalName : [email protected]
userId : 289fcdf8-af4e-40eb-a363-0430bc98d4d1
appId : c44b4083-3bb0-49c1-b47d-974e53cbdf3c
appDisplayName : Azure Portal
ipAddress : 199.11.103.31
clientAppUsed : Browser
userAgent : Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36
...
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory=$False)]
[String]$AccessToken,
[Parameter(Mandatory=$False)]
[String]$EntryId,
[switch]$Export
)
Process
{
# Get from cache if not provided
$AccessToken = Get-AccessTokenFromCache -AccessToken $AccessToken -Resource "https://graph.microsoft.com" -ClientId "1b730954-1685-4b74-9bfd-dac224a7b894"
# Select one entry if provided
if($EntryId)
{
$queryString = "`$filter=id eq '$EntryId'"
}
else
{
$queryString = "`$top=50&`$orderby=createdDateTime"
}
$results=Call-MSGraphAPI -AccessToken $AccessToken -API "auditLogs/signIns" -QueryString $queryString
# Return full results
if($Export)
{
return $results
}
elseif($EntryId) # The single entry
{
return $results
}
else # Print out only some info - the API always returns all info as $Select is not supported :(
{
$results | select createdDateTime,id,ipAddress,userPrincipalName,appDisplayName | ft
}
}
}
# Returns the 50 latest signin entries or the given entry
# Jun 9th 2020
function Get-AzureAuditLog
{
<#
.SYNOPSIS
Returns the 50 latest entries from Azure AD sign-in log or single entry by id
.DESCRIPTION
Returns the 50 latest entries from Azure AD sign-in log or single entry by id
.Example
Get-AADIntAccessTokenForMSGraph
PS C:\>Get-AADIntAzureAuditLog
id activityDateTime activityDisplayName operationType result initiatedBy
-- ---------------- ------------------- ------------- ------ -----------
Directory_9af6aff3-dc09-4ac1-a1d3-143e80977b3e_EZPWC_41985545 2020-05-29T07:57:51.4037921Z Add service principal Add success @{user=; app=}
Directory_f830a9d4-e746-48dc-944c-eb093364c011_1ZJAE_22273050 2020-05-29T07:57:51.6245497Z Add service principal Add failure @{user=; app=}
Directory_a813bc02-5d7a-4a40-9d37-7d4081d42b42_RKRRS_12877155 2020-06-02T12:49:38.5177891Z Add user Add success @{app=; user=}
.Example
Get-AADIntAccessTokenForMSGraph
PS C:\>Get-AADIntAzureAuditLog
id activityDateTime activityDisplayName operationType result initiatedBy
-- ---------------- ------------------- ------------- ------ -----------
Directory_9af6aff3-dc09-4ac1-a1d3-143e80977b3e_EZPWC_41985545 2020-05-29T07:57:51.4037921Z Add service principal Add success @{user=; app=}
Directory_f830a9d4-e746-48dc-944c-eb093364c011_1ZJAE_22273050 2020-05-29T07:57:51.6245497Z Add service principal Add failure @{user=; app=}
Directory_a813bc02-5d7a-4a40-9d37-7d4081d42b42_RKRRS_12877155 2020-06-02T12:49:38.5177891Z Add user Add success @{app=; user=}
PS C:\>Get-AADIntAzureAuditLog -EntryId Directory_9af6aff3-dc09-4ac1-a1d3-143e80977b3e_EZPWC_41985545
id : Directory_9af6aff3-dc09-4ac1-a1d3-143e80977b3e_EZPWC_41985545
category : ApplicationManagement
correlationId : 9af6aff3-dc09-4ac1-a1d3-143e80977b3e
result : success
resultReason :
activityDisplayName : Add service principal
activityDateTime : 2020-05-29T07:57:51.4037921Z
loggedByService : Core Directory
operationType : Add
initiatedBy : @{user=; app=}
targetResources : {@{id=66ce0b00-92ee-4851-8495-7c144b77601f; displayName=Azure Credential Configuration Endpoint Service; type=ServicePrincipal; userPrincipalName=;
groupType=; modifiedProperties=System.Object[]}}
additionalDetails : {}
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory=$False)]
[String]$AccessToken,
[Parameter(Mandatory=$False)]
[String]$EntryId,
[switch]$Export
)
Process
{
# Get from cache if not provided
$AccessToken = Get-AccessTokenFromCache -AccessToken $AccessToken -Resource "https://graph.microsoft.com" -ClientId "1b730954-1685-4b74-9bfd-dac224a7b894"
# Select one entry if provided
if($EntryId)
{
$queryString = "`$filter=id eq '$EntryId'"
}
else
{
$queryString = "`$top=50&`$orderby=activityDateTime"
}
$results=Call-MSGraphAPI -AccessToken $AccessToken -API "auditLogs/directoryAudits" -QueryString $queryString
# Return full results
if($Export)
{
return $results
}
elseif($EntryId) # The single entry
{
return $results
}
else # Print out only some info - the API always returns all info as $Select is not supported :(
{
$results | select id,activityDateTime,activityDisplayName,operationType,result,initiatedBy | ft
}
}
}
function Get-AADUsers
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$False)]
[String]$AccessToken,
[Parameter(Mandatory=$False)]
[String]$SearchString,
[Parameter(Mandatory=$False)]
[String]$UserPrincipalName
)
Process
{
if(![string]::IsNullOrEmpty($SearchString))
{
$queryString="`$filter=(startswith(displayName,'$SearchString') or startswith(userPrincipalName,'$SearchString'))"
}
elseif(![string]::IsNullOrEmpty($UserPrincipalName))
{
$queryString="`$filter=userPrincipalName eq '$UserPrincipalName'"
}
$results=Call-MSGraphAPI -AccessToken $AccessToken -API users -QueryString $queryString
return $results
}
}
# Gets the user's data
# Jun 16th 2020
function Get-MSGraphUser
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
[String]$AccessToken,
[Parameter(Mandatory=$True)]
[String]$UserPrincipalName
)
Process
{
# Url encode for external users, replace # with %23
$UserPrincipalName = $UserPrincipalName.Replace("#","%23")
$results=Call-MSGraphAPI -AccessToken $AccessToken -API "users/$UserPrincipalName" -ApiVersion "v1.0" -QueryString "`$select=businessPhones,displayName,givenName,id,jobTitle,mail,mobilePhone,officeLocation,preferredLanguage,surname,userPrincipalName,onPremisesDistinguishedName,onPremisesExtensionAttributes,onPremisesImmutableId,onPremisesLastSyncDateTime,onPremisesSamAccountName,onPremisesSecurityIdentifier,refreshTokensValidFromDateTime,signInSessionsValidFromDateTime,usageLocation,provisionedPlans,proxyAddresses"
return $results
}
}
# Gets the user's application role assignments
# Jun 16th 2020
function Get-MSGraphUserAppRoleAssignments
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
[String]$AccessToken,
[Parameter(Mandatory=$False)]
[String]$UserPrincipalName
)
Process
{
# Url encode for external users, replace # with %23
$UserPrincipalName = $UserPrincipalName.Replace("#","%23")
$results=Call-MSGraphAPI -AccessToken $AccessToken -API "users/$UserPrincipalName/appRoleAssignments" -ApiVersion v1.0
return $results
}
}
# Gets the user's owned devices
# Jun 16th 2020
function Get-MSGraphUserOwnedDevices
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
[String]$AccessToken,
[Parameter(Mandatory=$False)]
[String]$UserPrincipalName
)
Process
{
# Url encode for external users, replace # with %23
$UserPrincipalName = $UserPrincipalName.Replace("#","%23")
$results=Call-MSGraphAPI -AccessToken $AccessToken -API "users/$UserPrincipalName/ownedDevices" -ApiVersion v1.0
return $results
}
}
# Gets the user's registered devices
# Jun 16th 2020
function Get-MSGraphUserRegisteredDevices
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
[String]$AccessToken,
[Parameter(Mandatory=$False)]
[String]$UserPrincipalName
)
Process
{
# Url encode for external users, replace # with %23
$UserPrincipalName = $UserPrincipalName.Replace("#","%23")
$results=Call-MSGraphAPI -AccessToken $AccessToken -API "users/$UserPrincipalName/registeredDevices" -ApiVersion v1.0
return $results
}
}
# Gets the user's licenses
# Jun 16th 2020
function Get-MSGraphUserLicenseDetails
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
[String]$AccessToken,
[Parameter(Mandatory=$False)]
[String]$UserPrincipalName
)
Process
{
# Url encode for external users, replace # with %23
$UserPrincipalName = $UserPrincipalName.Replace("#","%23")
$results=Call-MSGraphAPI -AccessToken $AccessToken -API "users/$UserPrincipalName/licenseDetails" -ApiVersion v1.0
return $results
}
}
# Gets the user's groups
# Jun 16th 2020
function Get-MSGraphUserMemberOf
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
[String]$AccessToken,
[Parameter(Mandatory=$False)]
[String]$UserPrincipalName
)
Process
{
# Url encode for external users, replace # with %23
$UserPrincipalName = $UserPrincipalName.Replace("#","%23")
$results=Call-MSGraphAPI -AccessToken $AccessToken -API "users/$UserPrincipalName/memberOf" -ApiVersion v1.0
return $results
}
}
# Gets the user's direct reports
# Jun 16th 2020
function Get-MSGraphUserDirectReports
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
[String]$AccessToken,
[Parameter(Mandatory=$False)]
[String]$UserPrincipalName
)
Process
{
# Url encode for external users, replace # with %23
$UserPrincipalName = $UserPrincipalName.Replace("#","%23")
$results=Call-MSGraphAPI -AccessToken $AccessToken -API "users/$UserPrincipalName/directReports" -ApiVersion v1.0 -QueryString "`$select=businessPhones,displayName,givenName,id,jobTitle,mail,mobilePhone,officeLocation,preferredLanguage,surname,userPrincipalName,onPremisesDistinguishedName,onPremisesExtensionAttributes,onPremisesImmutableId,onPremisesLastSyncDateTime,onPremisesSamAccountName,onPremisesSecurityIdentifier,refreshTokensValidFromDateTime,signInSessionsValidFromDateTime,usageLocation,provisionedPlans,proxyAddresses"
return $results
}
}
# Gets the user's manager
# Jun 16th 2020
function Get-MSGraphUserManager
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
[String]$AccessToken,
[Parameter(Mandatory=$False)]
[String]$UserPrincipalName
)
Process
{
# Url encode for external users, replace # with %23
$UserPrincipalName = $UserPrincipalName.Replace("#","%23")
$results=Call-MSGraphAPI -AccessToken $AccessToken -API "users/$UserPrincipalName/manager" -ApiVersion v1.0 -QueryString "`$select=businessPhones,displayName,givenName,id,jobTitle,mail,mobilePhone,officeLocation,preferredLanguage,surname,userPrincipalName,onPremisesDistinguishedName,onPremisesExtensionAttributes,onPremisesImmutableId,onPremisesLastSyncDateTime,onPremisesSamAccountName,onPremisesSecurityIdentifier,refreshTokensValidFromDateTime,signInSessionsValidFromDateTime,usageLocation,provisionedPlans,proxyAddresses"
return $results
}
}
# Gets the group's owners
# Jun 16th 2020
function Get-MSGraphGroupOwners
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
[String]$AccessToken,
[Parameter(Mandatory=$False)]
[String]$GroupId
)
Process
{
$results=Call-MSGraphAPI -AccessToken $AccessToken -API "groups/$GroupId/owners" -ApiVersion v1.0 -QueryString "`$select=businessPhones,displayName,givenName,id,jobTitle,mail,mobilePhone,officeLocation,preferredLanguage,surname,userPrincipalName,onPremisesDistinguishedName,onPremisesExtensionAttributes,onPremisesImmutableId,onPremisesLastSyncDateTime,onPremisesSamAccountName,onPremisesSecurityIdentifier,refreshTokensValidFromDateTime,signInSessionsValidFromDateTime,usageLocation,provisionedPlans,proxyAddresses"
return $results
}
}
# Gets the group's members
# Jun 16th 2020
function Get-MSGraphGroupMembers
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
[String]$AccessToken,
[Parameter(Mandatory=$False)]
[String]$GroupId
)
Process
{
$results=Call-MSGraphAPI -AccessToken $AccessToken -API "groups/$GroupId/members" -ApiVersion v1.0 -QueryString "`$top=500&`$select=businessPhones,displayName,givenName,id,jobTitle,mail,mobilePhone,officeLocation,preferredLanguage,surname,userPrincipalName,onPremisesDistinguishedName,onPremisesExtensionAttributes,onPremisesImmutableId,onPremisesLastSyncDateTime,onPremisesSamAccountName,onPremisesSecurityIdentifier,refreshTokensValidFromDateTime,signInSessionsValidFromDateTime,usageLocation,provisionedPlans,proxyAddresses"
return $results
}
}
# Gets the group's members
# Jun 17th 2020
function Get-MSGraphRoleMembers
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
[String]$AccessToken,
[Parameter(Mandatory=$False)]
[String]$RoleId
)
Process
{
$results=Call-MSGraphAPI -AccessToken $AccessToken -API "directoryRoles/$RoleId/members" -ApiVersion v1.0 -QueryString "`$select=businessPhones,displayName,givenName,id,jobTitle,mail,mobilePhone,officeLocation,preferredLanguage,surname,userPrincipalName,onPremisesDistinguishedName,onPremisesExtensionAttributes,onPremisesImmutableId,onPremisesLastSyncDateTime,onPremisesSamAccountName,onPremisesSecurityIdentifier,refreshTokensValidFromDateTime,signInSessionsValidFromDateTime,usageLocation,provisionedPlans,proxyAddresses"
return $results
}
}
# Gets the tenant domains (all of them)
# Jun 16th 2020
function Get-MSGraphDomains
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
[String]$AccessToken
)
Process
{
$results=Call-MSGraphAPI -AccessToken $AccessToken -API "domains" -ApiVersion beta
return $results
}
}
# Gets team information
# Jun 17th 2020
function Get-MSGraphTeams
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
[String]$AccessToken,
[Parameter(Mandatory=$True)]
[String]$GroupId
)
Process
{
$results=Call-MSGraphAPI -AccessToken $AccessToken -API "teams/$GroupId" -ApiVersion v1.0
return $results
}
}
# Gets team's app information
# Jun 17th 2020
function Get-MSGraphTeamsApps
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
[String]$AccessToken,
[Parameter(Mandatory=$True)]
[String]$GroupId
)
Process
{
$results=Call-MSGraphAPI -AccessToken $AccessToken -API "teams/$GroupId/installedApps?`$expand=teamsAppDefinition" -ApiVersion v1.0
return $results
}
}
# Gets the authorizationPolicy
# Sep 18th 2020
function Get-TenantAuthPolicy
{
<#
.SYNOPSIS
Gets tenant's authorization policy.
.DESCRIPTION
Gets tenant's authorization policy, including user and guest settings.
.PARAMETER AccessToken
Access token used to retrieve the authorization policy.
.Example
Get-AADIntAccessTokenForMSGraph
PS C:\>Get-AADIntTenantAuthPolicy
id : authorizationPolicy
allowInvitesFrom : everyone
allowedToSignUpEmailBasedSubscriptions : True
allowedToUseSSPR : True
allowEmailVerifiedUsersToJoinOrganization : False
blockMsolPowerShell : False
displayName : Authorization Policy
description : Used to manage authorization related settings across the company.
enabledPreviewFeatures : {}
guestUserRoleId : 10dae51f-b6af-4016-8d66-8c2a99b929b3
permissionGrantPolicyIdsAssignedToDefaultUserRole : {microsoft-user-default-legacy}
defaultUserRolePermissions : @{allowedToCreateApps=True; allowedToCreateSecurityGroups=True; allowedToReadOtherUsers=True}
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory=$False)]
[String]$AccessToken
)
Process
{
# Get from cache if not provided
$AccessToken = Get-AccessTokenFromCache -AccessToken $AccessToken -Resource "https://graph.microsoft.com" -ClientId "1b730954-1685-4b74-9bfd-dac224a7b894"
$results = Call-MSGraphAPI -AccessToken $AccessToken -API "policies/authorizationPolicy"
return $results
}
}
# Gets the guest account restrictions
# Sep 18th 2020
function Get-TenantGuestAccess
{
<#
.SYNOPSIS
Gets the guest access level of the user's tenant.
.DESCRIPTION
Gets the guest access level of the user's tenant.
Inclusive: Guest users have the same access as members
Normal: Guest users have limited access to properties and memberships of directory objects
Restricted: Guest user access is restricted to properties and memberships of their own directory objects (most restrictive)
.PARAMETER AccessToken
Access token used to retrieve the access level.
.Example
Get-AADIntAccessTokenForMSGraph -SaveToCache
PS C:\>Get-AADIntTenantGuestAccess
Access Description RoleId
------ ----------- ------
Normal Guest users have limited access to properties and memberships of directory objects 10dae51f-b6af-4016-8d66-8c2a99b929b3
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory=$False)]
[String]$AccessToken
)
Process
{
# Get from cache if not provided
$AccessToken = Get-AccessTokenFromCache -AccessToken $AccessToken -Resource "https://graph.microsoft.com" -ClientId "1b730954-1685-4b74-9bfd-dac224a7b894"
$policy = Get-TenantAuthPolicy -AccessToken $AccessToken
$roleId = $policy.guestUserRoleId
switch($roleId)
{
"a0b1b346-4d3e-4e8b-98f8-753987be4970" {
$attributes=[ordered]@{
"Access" = "Full"
"Description" = "Guest users have the same access as members"
}
break
}
"10dae51f-b6af-4016-8d66-8c2a99b929b3" {
$attributes=[ordered]@{
"Access" = "Normal"
"Description" = "Guest users have limited access to properties and memberships of directory objects"
}
break
}
"2af84b1e-32c8-42b7-82bc-daa82404023b" {
$attributes=[ordered]@{
"Access" = "Restricted"
"Description" = "Guest user access is restricted to properties and memberships of their own directory objects (most restrictive)"
}
break
}
}
$attributes["RoleId"] = $roleId
return New-Object psobject -Property $attributes
}
}
# Sets the guest account restrictions
# Sep 18th 2020
function Set-TenantGuestAccess
{
<#
.SYNOPSIS
Sets the guest access level for the user's tenant.
.DESCRIPTION
Sets the guest access level for the user's tenant.
Inclusive: Guest users have the same access as members
Normal: Guest users have limited access to properties and memberships of directory objects
Restricted: Guest user access is restricted to properties and memberships of their own directory objects (most restrictive)
.PARAMETER AccessToken
Access token used to retrieve the access level.
.PARAMETER Level
Guest access level. One of Inclusive, Normal, or Restricted.
.Example
Get-AADIntAccessTokenForMSGraph
PS C:\>Set-AADIntTenantGuestAccess -Level Normal
Access Description RoleId
------ ----------- ------
Normal Guest users have limited access to properties and memberships of directory objects 10dae51f-b6af-4016-8d66-8c2a99b929b3
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory=$False)]
[String]$AccessToken,
[Parameter(Mandatory=$True)]
[ValidateSet('Full','Normal','Restricted')]
[String]$Level
)
Process
{
# Get from cache if not provided
$AccessToken = Get-AccessTokenFromCache -AccessToken $AccessToken -Resource "https://graph.microsoft.com" -ClientId "1b730954-1685-4b74-9bfd-dac224a7b894"
switch($Level)
{
"Full" {$roleId = "a0b1b346-4d3e-4e8b-98f8-753987be4970"; break}
"Normal" {$roleId = "10dae51f-b6af-4016-8d66-8c2a99b929b3"; break}
"Restricted" {$roleId = "2af84b1e-32c8-42b7-82bc-daa82404023b"; break}
}
$body = "{""guestUserRoleId"":""$roleId""}"
Call-MSGraphAPI -AccessToken $AccessToken -API "policies/authorizationPolicy/authorizationPolicy" -Method "PATCH" -Body $body
Get-TenantGuestAccess -AccessToken $AccessToken
}
}
# Enables Msol PowerShell access
# Sep 18th 2020
function Enable-TenantMsolAccess
{
<#
.SYNOPSIS
Enables Msol PowerShell module access for the user's tenant.
.DESCRIPTION
Enables Msol PowerShell module access for the user's tenant.
.PARAMETER AccessToken
Access token used to enable the Msol PowerShell access.
.Example
Get-AADIntAccessTokenForMSGraph
PS C:\>Enable-AADIntTenantMsolAccess
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory=$False)]
[String]$AccessToken
)
Process
{
# Get from cache if not provided
$AccessToken = Get-AccessTokenFromCache -AccessToken $AccessToken -Resource "https://graph.microsoft.com" -ClientId "1b730954-1685-4b74-9bfd-dac224a7b894"
$body = '{"blockMsolPowerShell":"false"}'
Call-MSGraphAPI -AccessToken $AccessToken -API "policies/authorizationPolicy/authorizationPolicy" -Method "PATCH" -Body $body
}
}
# Disables Msol PowerShell access
# Sep 18th 2020
function Disable-TenantMsolAccess
{
<#
.SYNOPSIS
Disables Msol PowerShell module access for the user's tenant.
.DESCRIPTION
Disables Msol PowerShell module access for the user's tenant.
.PARAMETER AccessToken
Access token used to disable the Msol PowerShell access.
.Example
Get-AADIntAccessTokenForMSGraph
PS C:\>Disable-AADIntTenantMsolAccess
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory=$False)]
[String]$AccessToken
)
Process
{
# Get from cache if not provided
$AccessToken = Get-AccessTokenFromCache -AccessToken $AccessToken -Resource "https://graph.microsoft.com" -ClientId "1b730954-1685-4b74-9bfd-dac224a7b894"
$body = '{"blockMsolPowerShell":"true"}'
Call-MSGraphAPI -AccessToken $AccessToken -API "policies/authorizationPolicy/authorizationPolicy" -Method "PATCH" -Body $body
}
}
# Get rollout policies
# Jan 7th 2021
function Get-RolloutPolicies
{
<#
.SYNOPSIS
Gets the tenant's rollout policies.
.DESCRIPTION
Gets the tenant's rollout policies.
.PARAMETER AccessToken
Access token used to get tenant's rollout policies.
.Example
Get-AADIntAccessTokenForMSGraph -SaveToCache
PS C:\>Get-AADIntRolloutPolicies
id : cdcb37e1-9c4a-4de9-a7f5-65fdf9f6241d
displayName : passthroughAuthentication rollout policy
description :
feature : passthroughAuthentication
isEnabled : True
isAppliedToOrganization : False
id : 3c89cd34-275c-4cba-8d8e-80338db7df91
displayName : seamlessSso rollout policy
description :
feature : seamlessSso
isEnabled : True
isAppliedToOrganization : False
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory=$False)]
[String]$AccessToken
)
Process
{
# Get from cache if not provided
$AccessToken = Get-AccessTokenFromCache -AccessToken $AccessToken -Resource "https://graph.microsoft.com" -ClientId "1b730954-1685-4b74-9bfd-dac224a7b894"
Call-MSGraphAPI -AccessToken $AccessToken -API "directory/featureRolloutPolicies" -ApiVersion beta
}
}
# Get rollout policy groups
# Jan 7th 2021
function Get-RolloutPolicyGroups
{
<#
.SYNOPSIS
Gets groups of the given rollout policy.
.DESCRIPTION
Gets groups of the given rollout policy.
.PARAMETER AccessToken
Access token used to get rollout policy groups.
.PARAMETER PolicyId
Guid of the rollout policy.
.Example
Get-AADIntAccessTokenForMSGraph -SaveToCache
PS C:\>Get-AADIntRolloutPolicyGroups -PolicyId cdcb37e1-9c4a-4de9-a7f5-65fdf9f6241d | Select displayName,id
displayName id
----------- --
PTA SSO Sales b9faf3ba-db5f-4ed2-b9c8-0fd5916de1f3
PTA SSO Markering f35d712f-dcdb-4040-a93d-ffd04aff3f75
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory=$False)]
[String]$AccessToken,
[Parameter(Mandatory=$True)]
[GUID]$PolicyId
)
Process
{
# Get from cache if not provided
$AccessToken = Get-AccessTokenFromCache -AccessToken $AccessToken -Resource "https://graph.microsoft.com" -ClientId "1b730954-1685-4b74-9bfd-dac224a7b894"
$response=Call-MSGraphAPI -AccessToken $AccessToken -API "directory/featureRolloutPolicies/$($PolicyId.ToString())" -QueryString "`$expand=appliesTo" -ApiVersion beta
$response.appliesTo
}
}
# Add groups to rollout policy
# Jan 7th 2021
function Add-RolloutPolicyGroups
{
<#
.SYNOPSIS
Adds given groups to the given rollout policy.
.DESCRIPTION
Adds given groups to the given rollout policy.
Status meaning:
204 The group successfully added
400 Invalid group id
404 Invalid policy id
.PARAMETER AccessToken
Access token used to add rollout policy groups.
.PARAMETER PolicyId
Guid of the rollout policy.
.PARAMETER GroupIds
List of group guids.
.Example
Get-AADIntAccessTokenForMSGraph -SaveToCache
PS C:\>Add-AADIntRolloutPolicyGroups -PolicyId cdcb37e1-9c4a-4de9-a7f5-65fdf9f6241d -GroupIds b9faf3ba-db5f-4ed2-b9c8-0fd5916de1f3,f35d712f-dcdb-4040-a93d-ffd04aff3f75
id status
-- ------
b9faf3ba-db5f-4ed2-b9c8-0fd5916de1f3 204
f35d712f-dcdb-4040-a93d-ffd04aff3f75 204
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory=$False)]
[String]$AccessToken,
[Parameter(Mandatory=$True)]
[GUID]$PolicyId,
[Parameter(Mandatory=$True)]
[GUID[]]$GroupIds
)
Process
{
# Get from cache if not provided
$AccessToken = Get-AccessTokenFromCache -AccessToken $AccessToken -Resource "https://graph.microsoft.com" -ClientId "1b730954-1685-4b74-9bfd-dac224a7b894"
# Build the body
$requests = @()
foreach($GroupId in $GroupIds)
{
$id = $GroupId.toString()
$request = @{
"id" = $id
"method" = "POST"
"url" = "directory/featureRolloutPolicies/$($PolicyId.toString())/appliesTo/`$ref"
"body" = @{ "@odata.id" = "https://graph.microsoft.com/beta/directoryObjects/$id" }
"headers" = @{ "Content-Type" = "application/json" }
}
$requests += $request
}
$body = @{ "requests" = $requests } | ConvertTo-Json -Depth 5
$response = Call-MSGraphAPI -AccessToken $AccessToken -API "`$batch" -ApiVersion beta -Method "POST" -Body $body
if($response.responses[0].body.error.message)
{
Write-Error $response.responses[0].body.error.message
}
else
{
$response.responses | select id,status
}
}
}
# Removes groups from the rollout policy
# Jan 7th 2021
function Remove-RolloutPolicyGroups
{
<#
.SYNOPSIS
Removes given groups from the given rollout policy.
.DESCRIPTION
Removes given groups from the given rollout policy.
Status meaning:
204 The group successfully added
400 Invalid group id
404 Invalid policy id
.PARAMETER AccessToken
Access token used to remove rollout policy groups.
.PARAMETER PolicyId
Guid of the rollout policy.
.PARAMETER GroupIds
List of group guids.
.Example
Get-AADIntAccessTokenForMSGraph -SaveToCache
PS C:\>Remove-AADIntRolloutPolicyGroups -PolicyId cdcb37e1-9c4a-4de9-a7f5-65fdf9f6241d -GroupIds b9faf3ba-db5f-4ed2-b9c8-0fd5916de1f3,f35d712f-dcdb-4040-a93d-ffd04aff3f75
id status
-- ------
b9faf3ba-db5f-4ed2-b9c8-0fd5916de1f3 204
f35d712f-dcdb-4040-a93d-ffd04aff3f75 204
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory=$False)]
[String]$AccessToken,
[Parameter(Mandatory=$True)]
[GUID]$PolicyId,
[Parameter(Mandatory=$True)]
[GUID[]]$GroupIds
)
Process
{
# Get from cache if not provided
$AccessToken = Get-AccessTokenFromCache -AccessToken $AccessToken -Resource "https://graph.microsoft.com" -ClientId "1b730954-1685-4b74-9bfd-dac224a7b894"
# Build the body
$requests = @()
foreach($GroupId in $GroupIds)
{
$id = $GroupId.toString()
$request = @{
"id" = $id
"method" = "DELETE"
"url" = "directory/featureRolloutPolicies/$($PolicyId.toString())/appliesTo/$id/`$ref"
}
$requests += $request
}
$body = @{ "requests" = $requests } | ConvertTo-Json -Depth 5
$response = Call-MSGraphAPI -AccessToken $AccessToken -API "`$batch" -ApiVersion beta -Method "POST" -Body $body
if($response.responses[0].body.error.message)
{
Write-Error $response.responses[0].body.error.message
}
else
{
$response.responses | select id,status
}
}
}
# Set rollout policy
# Jan 7th 2021
function Remove-RolloutPolicy