forked from dougsbaker/CA-Export
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Export-CaPolicy.MSGraph.ps1
394 lines (357 loc) · 12.5 KB
/
Export-CaPolicy.MSGraph.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
###############################################################################
# Conditional Access Export Utility with Microsoft Graph PowerShell (MgGraph)
# Based on https://github.com/dougsbaker/CA-Export
# Developed my own "flavor" - Output is structured more like the AzureAD Portal
###############################################################################
<#
.SYNOPSIS
Conditional Access Export Utility with Microsoft Graph PowerShell (MgGraph)
.DESCRIPTION
Exports CA Policy to HTML Format for auditing/historical purposes.
.NOTES
Douglas Baker
@dougsbaker
Andres Bohren
@andresbohren
10.02.2023 Fixed:
- Directory Roles
- Users
- Applications
- DeviceFilter
Andres Bohren
@andresbohren
13.02.2023 Fixed:
- Test Module and Connect-MgGraph
- Addet Session Controls
- Output is now devided into Conditions, SessionControls, GrantControls
- Code Cleanup and changed from Spaces to Tabs
Andres Bohren
@andresbohren
15.02.2023 Fixed:
- Output is now devided into Users, Cloud Apps or Actions, Conditions, GrantControls, SessionControls (like in CA Portal)
- Minor rearrangement of Rows
Andres Bohren
@andresbohren
18.04.2023 Fixed:
- Fixed Script Parameter "PolicyID" -ConditionalAccessPolicyId
- Addet PolicyID to HTML Output
Andres Bohren
@andresbohren
03.05.2023 Fixed:
- Changed UPN to Displaynames for AD Objects because Groups don't have a UPN
Andres Bohren
@andresbohren
04.06.2024 Fixed:
- Remove Select-MgProfile (older Microsoft.Graph Module)
- Fixed GrantControls
#>
[CmdletBinding()]
param (
[Parameter()]
[String]
$TenantID,
# Parameter help description
[Parameter()]
[String]
$PolicyID
)
#ExportLocation
$ExportLocation = $PSScriptRoot
$FileName = "\CAPolicy.html"
$HTMLExport = $true
#Test Graph Module
$GraphModule = Get-Module "Microsoft.Graph" -ListAvailable
If ($Null -eq $GraphModule)
{
Write-Host "Microsoft.Graph Module not installed" -ForegroundColor Yellow
Write-Host "Use: Install-Module -Name Microsoft.Graph" -ForegroundColor Yellow
break
}
#Connect-MgGraph
$MgContext = Get-MgContext
If ($Null -eq $MgContext)
{
Write-host "Connect-MgGraph"
Connect-MgGraph -Scopes 'Policy.Read.All', 'Directory.Read.All','Application.Read.All' -NoWelcome
} else {
Write-host "Disconnect-MgGraph"
Disconnect-MgGraph -ErrorAction SilentlyContinue | Out-Null
Write-host "Connect-MgGraph"
Connect-MgGraph -Scopes 'Policy.Read.All', 'Directory.Read.All','Application.Read.All' -NoWelcome
}
#Collect CA Policy
Write-host "Exporting: CA Policy"
if($PolicyID)
{
$CAPolicy = Get-MgIdentityConditionalAccessPolicy -ConditionalAccessPolicyId $PolicyID
}
else
{
$CAPolicy = Get-MgIdentityConditionalAccessPolicy -all
}
#Tenant Informations
$TenantData = Get-MgOrganization
$TenantName = $TenantData.DisplayName
$date = Get-Date
$CAExport = [PSCustomObject]@()
$AdUsers = @()
$Apps = @()
#Extract Values
Write-host "Extracting: CA Policy Data"
foreach( $Policy in $CAPolicy)
{
### Conditions ###
$IncludeUG = $null
$IncludeUG = $Policy.Conditions.Users.IncludeUsers
$IncludeUG += $Policy.Conditions.Users.IncludeGroups
$IncludeUG += $Policy.Conditions.Users.IncludeRoles
$ExcludeUG = $null
$ExcludeUG = $Policy.Conditions.Users.ExcludeUsers
$ExcludeUG += $Policy.Conditions.Users.ExcludeGroups
$ExcludeUG += $Policy.Conditions.Users.ExcludeRoles
$Apps += $Policy.Conditions.Applications.IncludeApplications
$Apps += $Policy.Conditions.Applications.ExcludeApplications
$AdUsers += $ExcludeUG
$AdUsers += $IncludeUG
$InclLocation = $Null
$ExclLocation = $Null
$InclLocation = $Policy.Conditions.Locations.includelocations
$ExclLocation = $Policy.Conditions.Locations.Excludelocations
$InclPlat = $Null
$ExclPlat = $Null
$InclPlat = $Policy.Conditions.Platforms.IncludePlatforms
$ExclPlat = $Policy.Conditions.Platforms.ExcludePlatforms
$InclDev = $null
$ExclDev = $null
$InclDev = $Policy.Conditions.Devices.IncludeDevices
$ExclDev = $Policy.Conditions.Devices.ExcludeDevices
$devFilters = $null
$devFilters = $Policy.Conditions.Devices.DeviceFilter.Rule
$CAExport += New-Object PSObject -Property @{
### Users ###
Users = ""
Name = $Policy.DisplayName;
PolicyID = $Policy.ID
Status = $Policy.State;
UsersInclude = ($IncludeUG -join ", `r`n");
UsersExclude = ($ExcludeUG -join ", `r`n");
### Cloud apps or actions ###
'Cloud apps or actions' ="";
ApplicationsIncluded = ($Policy.Conditions.Applications.IncludeApplications -join ", `r`n");
ApplicationsExcluded = ($Policy.Conditions.Applications.ExcludeApplications -join ", `r`n");
userActions = ($Policy.Conditions.Applications.IncludeUserActions -join ", `r`n");
AuthContext = ($Policy.Conditions.Applications.IncludeAuthenticationContextClassReferences -join ", `r`n");
### Conditions ###
Conditions = "";
UserRisk = ($Policy.Conditions.UserRiskLevels -join ", `r`n");
SignInRisk = ($Policy.Conditions.SignInRiskLevels -join ", `r`n");
PlatformsInclude = ($InclPlat -join ", `r`n");
PlatformsExclude = ($ExclPlat -join ", `r`n");
LocationsIncluded = ($InclLocation -join ", `r`n");
LocationsExcluded = ($ExclLocation -join ", `r`n");
ClientApps = ($Policy.Conditions.ClientAppTypes -join ", `r`n");
DevicesIncluded = ($InclDev -join ", `r`n");
DevicesExcluded = ($ExclDev -join ", `r`n");
DeviceFilters =($devFilters -join ", `r`n");
### Grant Controls ###
GrantControls = "";
BuiltInControls = $($Policy.GrantControls.BuiltInControls -join " ")
TermsOfUse = $($Policy.GrantControls.TermsOfUse)
CustomControls = $($Policy.GrantControls.CustomAuthenticationFactors)
GrantOperator = $Policy.GrantControls.Operator
### Session Controls ###
SessionControls = ""
SessionControlsAdditionalProperties = $Policy.SessionControls.AdditionalProperties
ApplicationEnforcedRestrictionsIsEnabled = $Policy.SessionControls.ApplicationEnforcedRestrictions.IsEnabled
ApplicationEnforcedRestrictionsAdditionalProperties = $Policy.SessionControls.ApplicationEnforcedRestrictions.AdditionalProperties
CloudAppSecurityType = $Policy.SessionControls.CloudAppSecurity.CloudAppSecurityType
CloudAppSecurityIsEnabled = $Policy.SessionControls.CloudAppSecurity.IsEnabled
CloudAppSecurityAdditionalProperties = $Policy.SessionControls.CloudAppSecurity.AdditionalProperties
DisableResilienceDefaults = $Policy.SessionControls.DisableResilienceDefaults
PersistentBrowserIsEnabled = $Policy.SessionControls.PersistentBrowser.IsEnabled
PersistentBrowserMode = $Policy.SessionControls.PersistentBrowser.Mode
PersistentBrowserAdditionalProperties = $Policy.SessionControls.PersistentBrowser.AdditionalProperties
SignInFrequencyAuthenticationType = $Policy.SessionControls.SignInFrequency.AuthenticationType
SignInFrequencyInterval = $Policy.SessionControls.SignInFrequency.FrequencyInterval
SignInFrequencyIsEnabled = $Policy.SessionControls.SignInFrequency.IsEnabled
SignInFrequencyType = $Policy.SessionControls.SignInFrequency.Type
SignInFrequencyValue = $Policy.SessionControls.SignInFrequency.Value
SignInFrequencyAdditionalProperties = $Policy.SessionControls.SignInFrequency.AdditionalProperties
}
}
#Swith user/group Guid to display names
Write-host "Converting: EntraID Guids"
#Filter out Objects
$cajson = $CAExport | ConvertTo-Json -Depth 4
$ADsearch = $AdUsers | Where-Object {$_ -ne 'All' -and $_ -ne 'GuestsOrExternalUsers' -and $_ -ne 'None'}
$AdNames =@{}
Get-MgDirectoryObjectById -ids $ADsearch | ForEach-Object{
$obj = $_.Id
#$disp = $_.displayName
$disp = $_.AdditionalProperties.displayName
$AdNames.$obj = $disp
$cajson = $cajson -replace "$obj", "$disp"
}
$CAExport = $cajson |ConvertFrom-Json
#Switch Apps Guid with Display names
$allApps = Get-MgServicePrincipal -All
$allApps | Where-Object{ $_.AppId -in $Apps} | ForEach-Object{
$obj = $_.AppId
$disp =$_.DisplayName
$cajson = $cajson -replace "$obj", "$disp"
}
#switch named location Guid for Display Names
Get-MgIdentityConditionalAccessNamedLocation | ForEach-Object{
$obj = $_.Id
$disp =$_.DisplayName
$cajson = $cajson -replace "$obj", "$disp"
}
#Switch Roles Guid to Names
#Get-MgDirectoryRole | ForEach-Object{
Get-MgDirectoryRoleTemplate | ForEach-Object{
$obj = $_.Id
$disp =$_.DisplayName
$cajson = $cajson -replace "$obj", "$disp"
}
$CAExport = $cajson | ConvertFrom-Json
#Export Setup
Write-host "Pivoting: CA to Export Format"
$pivot = @()
$rowItem = New-Object PSObject
$rowitem | Add-Member -type NoteProperty -Name 'CA Item' -Value "row1"
$Pcount = 1
foreach($CA in $CAExport)
{
$rowitem | Add-Member -type NoteProperty -Name "Policy $pcount" -Value "row1"
#$ca.Name
$pcount += 1
}
$pivot += $rowItem
#Add Data to Report
$Rows = $CAExport | Get-Member | Where-Object {$_.MemberType -eq "NoteProperty"}
$Rows| ForEach-Object{
$rowItem = New-Object PSObject
$rowname = $_.Name
$rowitem | Add-Member -type NoteProperty -Name 'CA Item' -Value $_.Name
$Pcount = 1
foreach($CA in $CAExport)
{
$ca | Get-Member | Where-Object {$_.MemberType -eq "NoteProperty"} | ForEach-Object {
$a = $_.name
$b = $ca.$a
if ($a -eq $rowname) {
$rowitem | Add-Member -type NoteProperty -Name "Policy $pcount" -Value $b
}
}
$pcount += 1
}
$pivot += $rowItem
}
#Column Sorting Order
$sort = "Name","PolicyID","Status","Users","UsersInclude","UsersExclude","Cloud apps or actions","ApplicationsIncluded","ApplicationsExcluded",`
"userActions","AuthContext","Conditions","UserRisk","SignInRisk","PlatformsInclude","PlatformsExclude","LocationsIncluded",`
"LocationsExcluded","ClientApps","Devices","DevicesIncluded","DevicesExcluded","DeviceFilters",`
"GrantControls", "BuiltInControls", "TermsOfUse", "CustomControls", "GrantOperator",`
"SessionControls","SessionControlsAdditionalProperties","ApplicationEnforcedRestrictionsIsEnabled","ApplicationEnforcedRestrictionsAdditionalProperties",`
"CloudAppSecurityType","CloudAppSecurityIsEnabled","CloudAppSecurityAdditionalProperties","DisableResilienceDefaults","PersistentBrowserIsEnabled",`
"PersistentBrowserMode","PersistentBrowserAdditionalProperties","SignInFrequencyAuthenticationType","SignInFrequencyInterval","SignInFrequencyIsEnabled",`
"SignInFrequencyType","SignInFrequencyValue","SignInFrequencyAdditionalProperties"
#Debug
#$pivot | Sort-Object $sort | Out-GridView
#HTML Export
if ($HTMLExport) {
Write-host "Saving to File: HTML"
$jquery = '<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("tr").click(function(){
if(!$(this).hasClass("selected")){
$(this).addClass("selected");
} else {
$(this).removeClass("selected");
}
});
$("th").click(function(){
if(!$(this).hasClass("colselected")){
$(this).addClass("colselected");
} else {
$(this).removeClass("colselected");
}
});
});
</script>'
$html = "<html><head><base href='https://docs.microsoft.com/' target='_blank'>
$jquery<style>
.title{
display: block;
font-size: 2em;
margin-block-start: 0.67em;
margin-block-end: 0.67em;
margin-inline-start: 0px;
margin-inline-end: 0px;
font-weight: bold;
font-family: Segoe UI;
}
table{
border-collapse: collapse;
margin: 25px 0;
font-size: 0.9em;
font-family: Segoe UI;
min-width: 400px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.15) ;
text-align: center;
}
thead tr {
background-color: #009879;
color: #ffffff;
text-align: left;
}
th, td {
min-width: 250px;
padding: 12px 15px;
border: 1px solid lightgray;
vertical-align: top;
}
td {
vertical-align: top;
}
tbody tr {
border-bottom: 1px solid #dddddd;
}
tbody tr:nth-of-type(even) {
background-color: #f3f3f3;
}
tbody tr:nth-of-type(5), tbody tr:nth-of-type(8), body tr:nth-of-type(13), tbody tr:nth-of-type(24), tbody tr:nth-of-type(29){
background-color: #36c;
text-aling:left !important
}
tbody tr:last-of-type {
border-bottom: 2px solid #009879;
}
tr:hover{
background-color: #ffea76!important;
}
.selected:not(th){
background-color:#ffea76!important;
}
th{
background-color:white !important;
}
.colselected {
background-color: rgb(93, 236, 213)!important;
}
table tr th:first-child,table tr td:first-child {
position: sticky;
inset-inline-start: 0;
background-color: #36c!important;
Color: #fff;
font-weight: bolder;
text-align: center;
}
</style></head><body> <div class='Title'>CA Export: $Tenantname - $Date </div>"
Write-host "Launching: Web Browser"
$Launch = $ExportLocation+$FileName
$HTML += $pivot | Where-Object {$_."CA Item" -ne 'row1' } | Sort-object { $sort.IndexOf($_."CA Item") }| convertto-html -Fragment
$HTML | Out-File $Launch
start-process $Launch
}