-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path92-Create-ConditionalAccessPolicies-P1.ps1
1215 lines (1170 loc) · 46.8 KB
/
92-Create-ConditionalAccessPolicies-P1.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
# Bitpusher
# \`._,'/
# (_- -_)
# \o/
# The Digital
# Fox
# https://theTechRelay.com
# https://github.com/bitpusher2k
#
# Create-ConditionalAccessPolicies.ps1 - By Bitpusher/The Digital Fox
# v2.9 last updated 2025-01-29
# Script to backup current Named Locations/Conditional Access Policies and
# to set up basic set of Named Locations and Conditional Access Policies in report-only mode.
#
# CAP info from Microsoft:
# https://learn.microsoft.com/en-us/entra/identity/conditional-access/howto-conditional-access-policy-all-users-mfa
# https://learn.microsoft.com/en-us/entra/identity/conditional-access/plan-conditional-access
# https://learn.microsoft.com/en-us/entra/identity/conditional-access/howto-conditional-access-insights-reporting
# https://learn.microsoft.com/en-us/entra/identity/conditional-access/concept-continuous-access-evaluation
#
#
# Prompts for creation of:
# *'Allowed Sign-in Countries' Named Location
# *'Blocked High Risk Countries' Named Location
# *'Blocked High Risk IP Addresses' Named Location
# *'Allow Sign-in from Specific Countries Only' Conditional Access Policy
# *'Block Sign-in from High Risk Countries' Conditional Access Policy
# *'Block Sign-in from High Risk IPs' Conditional Access Policy
# *'Require MFA for Device Registration' Conditional Access Policy
# *'Block Legacy Authentication All Apps' Conditional Access Policy
# *'Block sign-in from unused operating systems' ('Windows Phone', 'MacOS', and 'Linux') Conditional Access Policy
# *'Require Multifactor Authentication for Admin Roles' conditional access policy (1 hour)
# *'Require Multifactor Authentication for Azure management' conditional access policy (1 hour)
# *'Require Multifactor Authentication for All Users' conditional access policy (30 days)
# *'Require Hybrid Azure AD joined device (Windows devices need to be on domain and Entra ID)'
# *'Require phishing-resistant MFA for administrators' conditional access policy (new style recommended by Microsoft)
# *'Require MFA authentication strength for all users' conditional access policy (new style recommended by Microsoft)
# *'Require MFA authentication strength for guests' conditional access policy (new style recommended by Microsoft)
# *'Secure security info registration' conditional access policy (new style recommended by Microsoft)
# *'Require authentication strength for device registration' conditional access policy (new style recommended by Microsoft)
# *'Require device compliance' conditional access policy
# *'Restrict device code flow and authentication transfer' conditional access policy
# *'Require MFA for risky sign-in (P2)' conditional access policy (new style recommended by Microsoft)
# *'Require password change for risky users (P2)' conditional access policy (new style recommended by Microsoft)
# *'Block High-Risk Sign-ins (P2)' conditional access policy (only works with Entra ID P2 subscription)
# *'Block High-Risk Users (P2)' conditional access policy (only works with Entra ID P2 subscription)
#
# Enforcing these four policies recreates the protection provided by Microsoft "Security Defaults" through Conditional Access Policies:
# 'Block Legacy Authentication All Apps'
# 'Require Multifactor Authentication for Admin Roles'
# 'Require Multifactor Authentication for Azure management'
# 'Require Multifactor Authentication for All Users'
#
# The ten policies recommended by Microsoft are:
# 'Block legacy authentication'
# 'Require phishing-resistant MFA for administrators'
# 'Require MFA authentication strength for all users'
# 'Require MFA authentication strength for guests'
# 'Secure security info registration'
# 'Require MFA for risky sign-in'
# 'Require password change for risky users'
# 'Require authentication strength for device registration'
# 'Require device compliance'
# 'Restrict device code flow and authentication transfer'
#
#
# Note that conditional access policies are not applied in any particular order. All matching policies apply and the resulting access controls required by the policies are merged. If both "grant" and "block" policies match, block will always win. If multiple policies match and they have different Access Controls like Require MFA, Require Compliant Device or Require Azure AD Joined, the requirements will all be merged and all the access controls from all matching policies have to be met.
#
#
# Usage:
# powershell -executionpolicy bypass -f .\Create-ConditionalAccessPolicies.ps1 -OutputPath "Default"
#
# Run with already existing connection to M365 tenant through
# PowerShell modules.
#
# Uses (ExchangePowerShell), Microsoft Graph commands.
# If not connected:
# Connect-MgGraph -Scopes "Policy.Read.All","Policy.ReadWrite.ConditionalAccess","Application.Read.All"
# To check needed permissions for a command:
# (Find-MgGraphCommand -Command New-MgIdentityConditionalAccessNamedLocation)[0].Permissions.name
#
#comp #m365 #security #bec #script #irscript #powershell #conditional #access #policies #CAP #named #location #MFA
#Requires -Version 5.1
param(
[string]$OutputPath,
[string]$Encoding = "utf8bom" # "ascii","ansi","bigendianunicode","unicode","utf8","utf8","utf8NoBOM","utf32"
)
if ($PSVersionTable.PSVersion.Major -eq 5 -and ($Encoding -eq "utf8bom" -or $Encoding -eq "utf8nobom")) { $Encoding = "utf8" }
$date = Get-Date -Format "yyyyMMddHHmmss"
## If OutputPath variable is not defined, prompt for it
if (!$OutputPath) {
Write-Output ""
$OutputPath = Read-Host "Enter the output base path, e.g. $($env:userprofile)\Desktop\Investigation (default)"
if ($OutputPath -eq '') { $OutputPath = "$($env:userprofile)\Desktop\Investigation" }
Write-Output "Output base path will be in $OutputPath"
} elseif ($OutputPath -eq 'Default') {
Write-Output ""
$OutputPath = "$($env:userprofile)\Desktop\Investigation"
Write-Output "Output base path will be in $OutputPath"
}
## If OutputPath does not exist, create it
$CheckOutputPath = Get-Item $OutputPath -ErrorAction SilentlyContinue
if (!$CheckOutputPath) {
Write-Output ""
Write-Output "Output path does not exist. Directory will be created."
mkdir $OutputPath
}
## Get Primary Domain Name for output subfolder
# $PrimaryDomain = Get-AcceptedDomain | Where-Object Default -eq $true
# $DomainName = $PrimaryDomain.DomainName
$PrimaryDomain = Get-MgDomain | Where-Object { $_.isdefault -eq $True } | Select-Object -Property ID
$DomainName = $PrimaryDomain.ID
$CheckSubDir = Get-Item $OutputPath\$DomainName -ErrorAction SilentlyContinue
if (!$CheckSubDir) {
Write-Output ""
Write-Output "Domain sub-directory does not exist. Sub-directory will be created."
mkdir $OutputPath\$DomainName
}
Write-Output ""
Write-Output "Current Graph context:"
$Context = Get-MgContext
$Context
Write-Output "Current Graph user:"
$User = Get-MgUser -UserId $Context.Account
$User.DisplayName
$User.UserPrincipalName
$User.ID
$UserID = $User.ID
Write-Output ""
Write-Output "Tenant Entra ID licenses (Conditional Access Policies require at least 'P1' license)."
$LicenseStatus = (Get-MgSubscribedSku).ServicePlans | Where-Object { $_.ServicePlanName -like 'AAD_PREMIUM*' }
if ($LicenseStatus) {
Write-Output "Connected tenant appears to be license for at least Entra ID P1:"
$LicenseStatus
} else {
Write-Output "Tenant does not appear to be licensed to use Conditional Access Policies."
}
Write-Output ""
Write-Output "Script will attempt to backup any currently configured named locations and conditional access policies, then create a basic set of same."
Write-Output "All created policies will initially be in report-only mode, and the current user will be excluded from all created policies."
Write-Output ""
# Write-Output "Number of named location currently configured in tenant:"
# Get-MgIdentityConditionalAccessNamedLocationCount
# (Get-MgIdentityConditionalAccessNamedLocation).AdditionalProperties
# (Get-MgIdentityConditionalAccessPolicy).Conditions.Users
[array]$ConfiguredNamedLocations = Get-MgIdentityConditionalAccessNamedLocation | Sort-Object DisplayName
if ($ConfiguredNamedLocations) {
Write-Output "Configured Named Locations in tenant:"
$ConfiguredNamedLocations.DisplayName
Write-Output ""
$Continue = Read-Host "Enter 'Y' to backup current Named Locations to JSON"
if ($Continue -eq "Y") {
Write-Output "Backing up current Named Locations to JSON..."
$ConfiguredNamedLocations | ConvertTo-Json -Depth 100 | Out-File "$OutputPath\$DomainName\NamedLocationsExport_$($date).json" -Encoding $Encoding
}
}
Write-Output ""
[array]$ConfiguredPolicies = Get-MgIdentityConditionalAccessPolicy | Sort-Object DisplayName
if ($ConfiguredPolicies) {
Write-Output "Configured Conditional Access Policies in tenant:"
Get-MgIdentityConditionalAccessPolicy | Select-Object DisplayName, ID, CreatedDateTime, State
Write-Output ""
$Continue = Read-Host "Enter 'Y' to backup current Conditional Access Policies to JSON"
if ($Continue -eq "Y") {
Write-Output "Backing up current Conditional Access Policies to JSON..."
$ConfiguredPolicies | ConvertTo-Json -Depth 100 | Out-File "$OutputPath\$DomainName\ConditionalAccessPoliciesExport_$($date).json" -Encoding $Encoding
}
}
# https://andrewstaylor.com/2022/09/13/securing-azure-ad-quickly-and-programatically/
# ## Create Azure AD Breakglass user
# $PasswordProfile = New-Object -TypeName Microsoft.Open.AzureAD.Model.PasswordProfile
# $bgpassword = Get-RandomPassword -Length 20
# $PasswordProfile.Password = $bgpassword
# $breakglass = New-MgUser -DisplayName "Azure BreakGlass Account" -PasswordProfile $PasswordProfile -UserPrincipalName "breakglass@$suffix" -AccountEnabled -MailNickName "BreakGlass" -PasswordPolicies "DisablePasswordExpiration"
## Create allowed countries named location
Write-Output ""
$Continue = Read-Host "Enter 'Y' to create 'Allowed Sign-in Countries' Named Location (US only)"
if ($Continue -eq "Y") {
$params = @{
"@odata.type" = "#microsoft.graph.countryNamedLocation"
DisplayName = "Allowed Sign-in Countries"
CountriesAndRegions = @(
"US"
)
IncludeUnknownCountriesAndRegions = $false
}
New-MgIdentityConditionalAccessNamedLocation -BodyParameter $params
Write-Output "Named location created."
Write-Output ""
}
## Create blocked countries named location
Write-Output ""
Write-Output "High-risk countries - Russia, Nigeria, South Africa, UAE, The Netherlands"
$Continue = Read-Host "Enter 'Y' to create 'Blocked High Risk Countries' Named Location"
if ($Continue -eq "Y") {
$params = @{
"@odata.type" = "#microsoft.graph.countryNamedLocation"
DisplayName = "Blocked High Risk Countries"
CountriesAndRegions = @(# 2022 BEC sources: https://static.fortra.com/agari/pdfs/guide/ag-acid-geography-of-bec-gd.pdf - Percentages by location:
"RU" # Russia - less than 1%, but it's high signal and low noise
"NG" # Nigeria - 50% (!)
"ZA" # South Africa - 9%
"AE" # United Arab Emirates - 2%
"NL" # The Netherlands - 4% of Europe's 5%
# United States - 25%, UK - 3%, Canada 3%
)
IncludeUnknownCountriesAndRegions = $false
}
New-MgIdentityConditionalAccessNamedLocation -BodyParameter $params
Write-Output "Named Location created."
Write-Output ""
}
## Create blocked IP address named location
Write-Output ""
$Continue = Read-Host "Enter 'Y' to create 'Blocked High Risk IP Addresses' Named Location"
if ($Continue -eq "Y") {
$params = @{
"@odata.type" = "#microsoft.graph.ipNamedLocation"
DisplayName = "Blocked High Risk IP Addresses"
IsTrusted = $false
IpRanges = @()
}
# [array]$Location4 = Read-Host "Enter slash-formatted IPv4 ranges to add to the block list, comma separated (e.g.: '97.98.134.100/32','98.114.200.24/32','98.47.98.66/32','99.115.38.155/32')" # Need to split input into array
Write-Output "Enter slash-formatted IPv4 range to add to the block list (if any) (e.g.: '97.98.134.100/32')"
$Location4 = do {
$IPv4 = Read-Host "Enter IP range, or leave blank to finish"
$IPv4
} while ($IPv4 -ne '')
if (!$Location4.count) { $Location4 = @('97.98.134.100/32') }
foreach ($IP in $Location4) {
if ($IP.Length -gt 7 -and $IP -match '^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/\d{1,2}$') {
# -match '^[\d\.\/]{9,18}$'
$IpRanges = @{}
$IpRanges.Add("@odata.type", "#microsoft.graph.iPv4CidrRange")
$IpRanges.Add("CidrAddress", $IP)
$params.IpRanges += $IpRanges
# Write-output "IP: $IP"
}
}
# [array]$Location6 = Read-Host "Enter slash-formatted IPv6 ranges to add to the block list (if any), comma separated (e.g.: '2603:8001:bf40:f00:855a:4064:fd77:abcd/128')" # Need to split input into array
Write-Output "Enter slash-formatted IPv6 range to add to the block list (if any) (e.g.: '2603:8001:bf40:f00:855a:4064:fd77:abcd/128')"
$Location6 = do {
$IPv6 = Read-Host "Enter IP range, or leave blank to finish"
$IPv6
} while ($IPv6 -ne '')
if ($Location6.count) {
foreach ($IP in $Location6) {
if ($IP.Length -gt 9 -and $IP -match '^[a-f\d\.\:\/]{10,49}$') {
$IpRanges = @{}
$IpRanges.Add("@odata.type", "#microsoft.graph.iPv6CidrRange")
$IpRanges.Add("CidrAddress", $IP)
$params.IpRanges += $IpRanges
}
}
}
# $params.IpRanges
New-MgIdentityConditionalAccessNamedLocation -BodyParameter $params
Write-Output "Named Location created."
Write-Output ""
}
## Create allowed countries conditional access policy
Write-Output ""
$Continue = Read-Host "Enter 'Y' to create 'Allow Sign-in from Specific Countries Only' Conditional Access Policy"
if ($Continue -eq "Y") {
$location = Get-MgIdentityConditionalAccessNamedLocation | Where-Object DisplayName -EQ "Allowed Sign-in Countries"
$locationid = $location.ID
$conditions = @{
Applications = @{
includeApplications = 'All'
};
Users = @{
includeUsers = @(
"All"
)
excludeUsers = @(
"$UserID"
)
};
ClientAppTypes = @(
'All'
);
Locations = @{
includeLocations = @(
"All"
);
excludeLocations = @(
"$locationid"
)
};
}
$grantcontrols = @{
BuiltInControls = @('Block');
Operator = 'OR'
}
$name = "Allow Sign-in from Specific Countries Only"
$state = "enabledForReportingButNotEnforced"
New-MgIdentityConditionalAccessPolicy -DisplayName $name -State $state -Conditions $conditions -GrantControls $grantcontrols
Write-Output "Policy created."
Write-Output ""
}
## Create blocked countries conditional access policy
Write-Output ""
$Continue = Read-Host "Enter 'Y' to create 'Block Sign-in from High Risk Countries' Conditional Access Policy"
if ($Continue -eq "Y") {
$location = Get-MgIdentityConditionalAccessNamedLocation | Where-Object DisplayName -EQ "Blocked High Risk Countries"
$locationid = $location.ID
$conditions = @{
Applications = @{
includeApplications = 'All'
};
Users = @{
includeUsers = @(
"All"
)
excludeUsers = @(
"$UserID"
)
};
ClientAppTypes = @(
'All'
);
Locations = @{
includeLocations = @(
"$locationid"
)
};
}
$grantcontrols = @{
BuiltInControls = @('Block');
Operator = 'OR'
}
$name = "Block Sign-in from High Risk Countries"
$state = "enabledForReportingButNotEnforced"
New-MgIdentityConditionalAccessPolicy -DisplayName $name -State $state -Conditions $conditions -GrantControls $grantcontrols
Write-Output "Policy created."
Write-Output ""
}
## Create blocked IPs conditional access policy
Write-Output ""
$Continue = Read-Host "Enter 'Y' to create 'Block Sign-in from High Risk IPs' Conditional Access Policy"
if ($Continue -eq "Y") {
$location = Get-MgIdentityConditionalAccessNamedLocation | Where-Object DisplayName -EQ "Blocked High Risk IP Addresses"
$locationid = $location.ID
$conditions = @{
Applications = @{
includeApplications = 'All'
};
Users = @{
includeUsers = @(
"All"
)
excludeUsers = @(
"$UserID"
)
};
ClientAppTypes = @(
'All'
);
Locations = @{
includeLocations = @(
"$locationid"
)
};
}
$grantcontrols = @{
BuiltInControls = @('Block');
Operator = 'OR'
}
$name = "Block Sign-in from High Risk IPs"
$state = "enabledForReportingButNotEnforced"
New-MgIdentityConditionalAccessPolicy -DisplayName $name -State $state -Conditions $conditions -GrantControls $grantcontrols
Write-Output "Policy created."
Write-Output ""
}
## Create conditional access policy to require MFA for device registration/enrollment
Write-Output ""
$Continue = Read-Host "Enter 'Y' to create 'Require MFA for Device Registration' Conditional Access Policy"
# https://learn.microsoft.com/en-us/mem/intune/enrollment/multi-factor-authentication
if ($Continue -eq "Y") {
$conditions = @{
Users = @{
includeUsers = @(
"All"
)
excludeUsers = @(
"$UserID"
)
};
Applications = @{
IncludeUserActions = @(
'urn:user:registerdevice'
);
}
}
$grantcontrols = @{
Operator = 'OR'
BuiltInControls = @(
"mfa"
)
}
$name = "Require MFA for Device Registration"
$state = "enabledForReportingButNotEnforced"
New-MgIdentityConditionalAccessPolicy -DisplayName $name -State $state -Conditions $conditions -GrantControls $grantcontrols
Write-Output "Policy created."
Write-Output ""
}
## Create conditional access policy to block legacy authentication
Write-Output ""
Write-Output "Blocked Legacy Protocols include POP, IMAP, SMTP, Older Office Clients and ActiveSync using Basic authentication."
$Continue = Read-Host "Enter 'Y' to create 'Block Legacy Authentication All Apps' Conditional Access Policy"
if ($Continue -eq "Y") {
$conditions = @{
Applications = @{
includeApplications = 'All'
};
Users = @{
includeUsers = @(
"All"
)
excludeUsers = @(
"$UserID"
)
};
ClientAppTypes = @(
'ExchangeActiveSync',
'Other'
);
}
$grantcontrols = @{
BuiltInControls = @('Block');
Operator = 'OR'
}
$name = "Block Legacy Authentication All Apps"
$state = "enabledForReportingButNotEnforced"
New-MgIdentityConditionalAccessPolicy -DisplayName $name -State $state -Conditions $conditions -GrantControls $grantcontrols
Write-Output "Policy created."
Write-Output ""
}
## Create conditional access policy to block unused operating system authentication
Write-Output ""
$Continue = Read-Host "Enter 'Y' to create 'Block sign-in from unused operating systems' Conditional Access Policy (includes 'Windows Phone', 'MacOS', and 'Linux')"
if ($Continue -eq "Y") {
$conditions = @{
Applications = @{
includeApplications = 'All'
};
Users = @{
includeUsers = @(
"All"
)
excludeUsers = @(
"$UserID"
)
};
ClientAppTypes = @(
'All'
);
Platforms = @{
IncludePlatforms = @(
"windowsPhone",
"macOS",
"linux"
)
};
}
$grantcontrols = @{
BuiltInControls = @('Block');
Operator = 'OR'
}
$name = "Block sign-in from unused operating systems"
$state = "enabledForReportingButNotEnforced"
New-MgIdentityConditionalAccessPolicy -DisplayName $name -State $state -Conditions $conditions -GrantControls $grantcontrols
Write-Output "Policy created."
Write-Output ""
}
## Create MFA enforcing policy for admins
Write-Output ""
$Continue = Read-Host "Enter 'Y' to create 'Require Multifactor Authentication for Admin Roles' conditional access policy"
if ($Continue -eq "Y") {
$PolicySettings = @{
DisplayName = "Require Multifactor Authentication for Admin Roles"
state = "enabledForReportingButNotEnforced"
conditions = @{
ClientAppTypes = @(
"all"
)
Applications = @{
includeApplications = @(
"All"
)
}
Users = @{
excludeUsers = @(
"$UserID"
)
includeRoles = @(
"62e90394-69f5-4237-9190-012177145e10" # Global Administrator role
"d2562ede-74db-457e-a7b6-544e236ebb61" # AI Administrator
"194ae4cb-b126-40b2-bd5b-6091b380977d" # Security Administrator role
"f28a1f50-f6e7-4571-818b-6a12f2af6b6c" # SharePoint Administrator role
"29232cdf-9323-42fd-ade2-1d097af3e4de" # Exchange Administrator role
"b1be1c3e-b65d-4f19-8427-f6fa0d97feb9" # Conditional Access Administrator role
"729827e3-9c14-49f7-bb1b-9608f156bbb8" # Helpdesk Administrator role
"b0f54661-2d74-4c50-afa3-1ec803f12efe" # Billing Administrator role
"fe930be7-5e62-47db-91af-98c3a49a38b1" # User Administrator role
"c4e39bd9-1100-46d3-8c65-fb160da0071f" # Authentication Administrator role
"9b895d92-2cd3-44c7-9d02-a6ac2d5ea5c3" # Application Administrator role
"158c047a-c907-4556-b7ef-446551a6b5f7" # Cloud Application Administrator role
"966707d0-3269-4727-9be2-8c3a10f19b9d" # Password Administrator role
"7be44c8a-adaf-4e2a-84d6-ab2649e08a13" # Privileged Authentication Administrator role
"e8611ab8-c189-46e8-94e1-60213ab1f814" # Privileged Role Administrator role
) # https://learn.microsoft.com/en-us/entra/identity/role-based-access-control/permissions-reference
}
}
grantControls = @{
Operator = "OR"
BuiltInControls = @(
"mfa"
)
}
sessionControls = @{
signInFrequency = @{
value = 1
type = "hours"
isEnabled = $true
}
}
}
New-MgIdentityConditionalAccessPolicy -BodyParameter $PolicySettings
Write-Output "Policy created."
Write-Output ""
}
## Create MFA enforcing policy for Azure management access
Write-Output ""
$Continue = Read-Host "Enter 'Y' to create 'Require Multifactor Authentication for Azure management' conditional access policy"
if ($Continue -eq "Y") {
$PolicySettings = @{
DisplayName = "Require Multifactor Authentication for Azure management"
state = "enabledForReportingButNotEnforced"
conditions = @{
Applications = @{
includeApplications = @(
"797f4846-ba00-4fd7-ba43-dac1f8f63013" # Windows Azure Service Management API (Azure portal, Azure PowerShell, Azure CLI)
)
}
Users = @{
includeUsers = @(
"All"
)
excludeUsers = @(
"$UserID"
)
}
ClientAppTypes = @(
"all"
)
}
grantControls = @{
Operator = "AND"
BuiltInControls = @(
"mfa"
)
}
sessionControls = @{
signInFrequency = @{
value = 1
type = "hours"
isEnabled = $true
}
}
}
New-MgIdentityConditionalAccessPolicy -BodyParameter $PolicySettings
Write-Output "Policy created."
Write-Output ""
}
## Create MFA enforcing policy for all users
Write-Output ""
$Continue = Read-Host "Enter 'Y' to create 'Require Multifactor Authentication for All Users' conditional access policy"
if ($Continue -eq "Y") {
$PolicySettings = @{
DisplayName = "Require Multifactor Authentication for All Users"
state = "enabledForReportingButNotEnforced"
conditions = @{
Applications = @{
includeApplications = @(
"All"
)
}
Users = @{
includeUsers = @(
"All"
)
excludeUsers = @(
"$UserID"
)
}
ClientAppTypes = @(
"all"
)
}
grantControls = @{
Operator = "AND"
BuiltInControls = @(
"mfa"
)
}
sessionControls = @{
signInFrequency = @{
value = 30
type = "days"
isEnabled = $true
}
}
}
New-MgIdentityConditionalAccessPolicy -BodyParameter $PolicySettings
Write-Output "Policy created."
Write-Output ""
}
## Create Require Hybrid Azure AD joined device policy
Write-Output ""
$Continue = Read-Host "Enter 'Y' to create 'Require Hybrid Azure AD joined device' conditional access policy"
if ($Continue -eq "Y") {
$PolicySettings = @{
DisplayName = "Require Hybrid Azure AD joined device"
state = "enabledForReportingButNotEnforced"
conditions = @{
Applications = @{
includeApplications = @(
"All"
)
}
Users = @{
includeUsers = @(
"All"
)
excludeUsers = @(
"$UserID"
)
}
ClientAppTypes = @(
"all"
)
}
grantControls = @{
Operator = "AND"
BuiltInControls = @(
"domainJoinedDevice"
)
}
}
New-MgIdentityConditionalAccessPolicy -BodyParameter $PolicySettings
Write-Output "Policy created."
Write-Output ""
}
## Create Require phishing-resistant MFA for administrators policy - If you use external authentication methods, these are currently incompatible with authentication strength and you should use the Require multifactor authentication grant control.
Write-Output ""
$Continue = Read-Host "Enter 'Y' to create 'Require phishing-resistant MFA for administrators' conditional access policy"
if ($Continue -eq "Y") {
$PolicySettings = @{
DisplayName = "Require phishing-resistant MFA for administrators"
state = "enabledForReportingButNotEnforced"
conditions = @{
ClientAppTypes = @(
"all"
)
Applications = @{
includeApplications = @(
"All"
)
}
Users = @{
excludeUsers = @(
"$UserID"
)
includeRoles = @(
"62e90394-69f5-4237-9190-012177145e10" # Global Administrator role
"d2562ede-74db-457e-a7b6-544e236ebb61" # AI Administrator
"194ae4cb-b126-40b2-bd5b-6091b380977d" # Security Administrator role
"f28a1f50-f6e7-4571-818b-6a12f2af6b6c" # SharePoint Administrator role
"29232cdf-9323-42fd-ade2-1d097af3e4de" # Exchange Administrator role
"b1be1c3e-b65d-4f19-8427-f6fa0d97feb9" # Conditional Access Administrator role
"729827e3-9c14-49f7-bb1b-9608f156bbb8" # Helpdesk Administrator role
"b0f54661-2d74-4c50-afa3-1ec803f12efe" # Billing Administrator role
"fe930be7-5e62-47db-91af-98c3a49a38b1" # User Administrator role
"c4e39bd9-1100-46d3-8c65-fb160da0071f" # Authentication Administrator role
"9b895d92-2cd3-44c7-9d02-a6ac2d5ea5c3" # Application Administrator role
"158c047a-c907-4556-b7ef-446551a6b5f7" # Cloud Application Administrator role
"966707d0-3269-4727-9be2-8c3a10f19b9d" # Password Administrator role
"7be44c8a-adaf-4e2a-84d6-ab2649e08a13" # Privileged Authentication Administrator role
"e8611ab8-c189-46e8-94e1-60213ab1f814" # Privileged Role Administrator role
) # https://learn.microsoft.com/en-us/entra/identity/role-based-access-control/permissions-reference
}
}
grantControls = @{
Operator = "OR"
AuthenticationStrength = @{
Id = "00000000-0000-0000-0000-000000000004"
}
}
}
New-MgIdentityConditionalAccessPolicy -BodyParameter $PolicySettings
Write-Output "Policy created."
Write-Output ""
}
## Create Require MFA authentication strength for all users policy - If you use external authentication methods, these are currently incompatable with authentication strength and you should use the Require multifactor authentication grant control.
Write-Output ""
$Continue = Read-Host "Enter 'Y' to create 'Require MFA authentication strength for all users' conditional access policy"
if ($Continue -eq "Y") {
$PolicySettings = @{
DisplayName = "Require MFA authentication strength for all users"
state = "enabledForReportingButNotEnforced"
conditions = @{
Applications = @{
includeApplications = @(
"All"
)
}
Users = @{
includeUsers = @(
"All"
)
excludeUsers = @(
"$UserID"
)
}
ClientAppTypes = @(
"all"
)
}
grantControls = @{
Operator = "OR"
AuthenticationStrength = @{
Id = "00000000-0000-0000-0000-000000000002"
}
}
}
New-MgIdentityConditionalAccessPolicy -BodyParameter $PolicySettings
Write-Output "Policy created."
Write-Output ""
}
## Create Require MFA authentication strength for guests policy - Currently, you can only apply authentication strength policies to external users who authenticate with Microsoft Entra ID. For email one-time passcode, SAML/WS-Fed, and Google federation users, use the MFA grant control to require MFA.
Write-Output ""
$Continue = Read-Host "Enter 'Y' to create 'Require MFA authentication strength for guests' conditional access policy"
if ($Continue -eq "Y") {
$PolicySettings = @{
DisplayName = "Require MFA authentication strength for guests"
state = "enabledForReportingButNotEnforced"
conditions = @{
Applications = @{
includeApplications = @(
MembershipKind = "All"
)
}
Users = @{
IncludeGuestsOrExternalUsers = @{
ExternalTenants = @(
"All"
)
GuestOrExternalUserTypes = "internalGuest,b2bCollaborationGuest,b2bCollaborationMember,b2bDirectConnectUser,otherExternalUser,serviceProvider"
}
excludeUsers = @(
"$UserID"
)
}
ClientAppTypes = @(
"all"
)
}
grantControls = @{
Operator = "OR"
AuthenticationStrength = @{
Id = "00000000-0000-0000-0000-000000000002"
}
}
}
New-MgIdentityConditionalAccessPolicy -BodyParameter $PolicySettings
Write-Output "Policy created."
Write-Output ""
}
## Create Secure security info registration policy - If you use external authentication methods, these are currently incompatable with authentication strength and you should use the Require multifactor authentication grant control.
Write-Output ""
$Continue = Read-Host "Enter 'Y' to create 'Secure security info registration policy' conditional access policy"
if ($Continue -eq "Y") {
$conditions = @{
Users = @{
includeUsers = @(
"All"
)
excludeUsers = @(
"$UserID"
)
"ExcludeGuestsOrExternalUsers" = @{
"ExternalTenants" = @{
MembershipKind = "all"
}
GuestOrExternalUserTypes = "internalGuest,b2bCollaborationGuest,b2bCollaborationMember,b2bDirectConnectUser,otherExternalUser,serviceProvider"
}
};
Applications = @{
IncludeUserActions = @(
'urn:user:registerdevice'
);
}
Locations = @{
includeLocations = @(
"All"
);
excludeLocations = @(
"AllTrusted"
)
}
}
$grantControls = @{
Operator = "OR"
AuthenticationStrength = @{
Id = "00000000-0000-0000-0000-000000000002"
}
}
$name = "Secure security info registration policy"
$state = "enabledForReportingButNotEnforced"
New-MgIdentityConditionalAccessPolicy -DisplayName $name -State $state -Conditions $conditions -GrantControls $grantcontrols
Write-Output "Policy created."
Write-Output ""
}
## Create Require authentication strength for device registration policy - If you use external authentication methods, these are currently incompatible with authentication strength and you should use the Require multifactor authentication grant control.
Write-Output ""
$Continue = Read-Host "Enter 'Y' to create 'Require authentication strength for device registration' conditional access policy"
if ($Continue -eq "Y") {
$conditions = @{
Users = @{
includeUsers = @(
"All"
)
excludeUsers = @(
"$UserID"
)
};
Applications = @{
IncludeUserActions = @(
'urn:user:registerdevice'
);
}
}
$grantControls = @{
Operator = "OR"
AuthenticationStrength = @{
Id = "00000000-0000-0000-0000-000000000002"
}
}
$name = "Require authentication strength for device registration"
$state = "enabledForReportingButNotEnforced"
New-MgIdentityConditionalAccessPolicy -DisplayName $name -State $state -Conditions $conditions -GrantControls $grantcontrols
Write-Output "Policy created."
Write-Output ""
}
## Create Require device compliance policy - Without a compliance policy created in Microsoft Intune this Conditional Access policy will not function as intended. Create a compliance policy first and ensure you have at least one compliant device before proceeding.
Write-Output ""
$Continue = Read-Host "Enter 'Y' to create 'Require device compliance' conditional access policy"
if ($Continue -eq "Y") {
$PolicySettings = @{
DisplayName = "Require device compliance"
state = "enabledForReportingButNotEnforced"
conditions = @{
Applications = @{
includeApplications = @(
"All"
)
}
Platforms = @{
IncludePlatforms = @(
"All"
)
ExcludePlatforms = @(
"android"
"iOS"
"macOS"
"linux"
)
}
Users = @{
includeUsers = @(
"All"
)
excludeUsers = @(
"$UserID"
)
}
ClientAppTypes = @(
"all"
)
}
grantControls = @{
Operator = "OR"
BuiltInControls = @(
"compliantDevice"
)
}
}
New-MgIdentityConditionalAccessPolicy -BodyParameter $PolicySettings
Write-Output "Policy created."
Write-Output ""
}
## Create Restrict device code flow and authentication transfer policy
Write-Output ""
$Continue = Read-Host "Enter 'Y' to create 'Restrict device code flow and authentication transfer' conditional access policy (manual completion of configuration required)"
if ($Continue -eq "Y") {
$PolicySettings = @{
DisplayName = "Restrict device code flow and authentication transfer"
state = "enabledForReportingButNotEnforced"
conditions = @{
Applications = @{
includeApplications = @(
"All"
)
}
Users = @{
includeUsers = @(
"All"
)
excludeUsers = @(
"$UserID"
)
}
ClientAppTypes = @(
"all"
)
}
grantControls = @{
Operator = "AND"
BuiltInControls = @(
"block"
)
}
}
New-MgIdentityConditionalAccessPolicy -BodyParameter $PolicySettings
Write-Output "Policy created."
Write-Output "NOTE: Authentication Flows is in preview and NOT FULLY CONFIGURABLE FROM POWERSHELL - Go to this policy > Conditions > Authentication Flows, set Configure to Yes, Select Device code flow and Authentication transfer, and Save to finish configuration."
Write-Output "Opening Edge browser window to finish policy configuration..."
Write-Output "https://portal.azure.com/#view/Microsoft_AAD_ConditionalAccess/ConditionalAccessBlade/~/Policies"
Start-Process msedge.exe -ArgumentList "https://portal.azure.com/#view/Microsoft_AAD_ConditionalAccess/ConditionalAccessBlade/~/Policies"
Write-Output ""
}
## Create Require MFA for risky sign-in (P2) policy
Write-Output ""
$Continue = Read-Host "Enter 'Y' to create 'Require MFA for risky sign-in (P2)' conditional access policy (only works with Entra ID P2 subscription)"
if ($Continue -eq "Y") {
$PolicySettings = @{
DisplayName = "Require MFA for risky sign-in (P2)"
state = "enabledForReportingButNotEnforced"
conditions = @{
Applications = @{
includeApplications = @(
"All"
)
}
Users = @{
includeUsers = @(
"All"
)
excludeUsers = @(
"$UserID"