6464 [string ]$OutputDirectory = " $PSScriptRoot \Output" ,
6565 [string ]$OutputFileName = " CA_Policies_Report_$ ( Get-Date - Format ' yyyyMMdd_HHmmss' ) .csv" ,
6666 [switch ]$IncludeEmptyColumns
67-
6867)
6968
7069# endregion
@@ -96,8 +95,6 @@ foreach ($sub in $RequiredSubmodules) {
9695 }
9796}
9897
99-
100-
10198# endregion
10299
103100# region Global Hash Caches
@@ -108,6 +105,7 @@ $global:NamedLocationHash = @{}
108105# endregion
109106
110107# region Graph Connection
108+ # Authenticates to Microsoft Graph using either certificate-based or interactive login
111109function Connect-MgGraphSession {
112110 if ($CreateSession.IsPresent ) {
113111 Disconnect-MgGraph - ErrorAction SilentlyContinue
@@ -127,6 +125,7 @@ Connect-MgGraphSession
127125# endregion
128126
129127# region Conversion Helpers
128+ # Functions to convert raw GUIDs into human-readable names (directory objects, SPNs, named locations)
130129function ConvertTo-DirectoryObjectName {
131130 param (
132131 [Parameter (Mandatory = $true )]
@@ -198,6 +197,7 @@ function Get-NamedLocationDisplayName {
198197# endregion
199198
200199# region Utility Functions
200+ # Miscellaneous helpers to support consistent formatting and data handling
201201function Join-Array {
202202 param ([array ]$Values )
203203 return ($Values -join ' ,' )
@@ -277,6 +277,7 @@ Write-Progress -Activity "Exporting" -Status "Retrieving CA policies..." -Percen
277277# endregion
278278
279279# region Policy Retrieval and Processing
280+ # Miscellaneous helpers to support consistent formatting and data handling
280281# Processing all CA policies
281282$AllPolicies = Get-MgBetaIdentityConditionalAccessPolicy - All
282283$total = $AllPolicies.Count
@@ -287,6 +288,8 @@ $AllPolicies | ForEach-Object {
287288 $CreatedDateTime = $_.CreatedDateTime
288289 $ModifiedDateTime = $_.ModifiedDateTime
289290 $State = $_.State
291+
292+ # Show progress bar for current policy being processed
290293 Write-Progress - Activity " Exporting Conditional Access Policies" - Status " Processing: $DisplayName " - PercentComplete (($ProcessedCount / $total ) * 100 )
291294
292295 # Filter CA policies based on their State
@@ -321,8 +324,8 @@ $AllPolicies | ForEach-Object {
321324 return
322325 }
323326
324-
325- # Assignments
327+ # --- Assignments Block ---
328+ # Evaluate and convert all user/group/role assignments from object IDs to display names
326329 $Conditions = $_.Conditions
327330 $IncludeUsers = $Conditions.Users.IncludeUsers
328331 $ExcludeUsers = $Conditions.Users.ExcludeUsers
@@ -363,9 +366,8 @@ $AllPolicies | ForEach-Object {
363366 $IncludeGuestsOrExtUsers = Join-Array $IncludeGuestsOrExtUsers
364367 $ExcludeGuestsOrExtUsers = Join-Array $ExcludeGuestsOrExtUsers
365368
366-
367-
368- # Target Resources
369+ # --- Target Resources Block ---
370+ # Evaluate application and user action conditions
369371 $IncludeApplications = $_.Conditions.Applications.IncludeApplications
370372 $ExcludeApplications = $_.Conditions.Applications.ExcludeApplications
371373 $UserAction = $_.Conditions.Applications.IncludeUserActions
@@ -381,9 +383,8 @@ $AllPolicies | ForEach-Object {
381383 }
382384 $ExcludeApplications = Join-Array $ExcludeApplications
383385
384-
385-
386- # Conditions
386+ # --- Conditions Block ---
387+ # Evaluate risk levels, client apps, platforms, and locations
387388 $UserRiskLevel = $_.Conditions.UserRiskLevelLevels
388389 $SigninRiskLevel = $_.Conditions.SigninRiskLevelLevels
389390 $ClientAppTypes = $_.Conditions.ClientAppTypes
@@ -409,14 +410,14 @@ $AllPolicies | ForEach-Object {
409410 }
410411 $ExcludeLocations = Join-Array $ExcludeLocations
411412
412-
413-
414- # Grant Control
413+ # --- Grant Controls Block ---
414+ # Evaluate grant control settings and operator
415415 $GrantControls = $_.GrantControls.BuiltInControls -join " ,"
416416 $GrantControlsOperator = $_.GrantControls.Operator
417417 $GrantControlsAuthStrength = $_.GrantControls.GrantControlsAuthStrength.DisplayName
418418
419- # Session Control
419+ # --- Session Controls Block ---
420+ # Evaluate session controls like app restrictions and sign-in frequency
420421 $AppEnforcedRestrictions = $_.SessionControls.ApplicationEnforcedRestrictions.IsEnabled
421422 $CloudAppSecurity = $_.SessionControls.CloudAppSecurity.IsEnabled
422423 $CAEMode = $_.SessionControls.ContinuousAccessEvaluation.Mode
@@ -435,8 +436,6 @@ $AllPolicies | ForEach-Object {
435436 $SignInFrequencyValue = " "
436437 }
437438
438-
439-
440439 $OutputCount ++
441440 $Result = @ {' DisplayName' = $DisplayName ;
442441 ' Description' = $Description ;
@@ -474,10 +473,10 @@ $AllPolicies | ForEach-Object {
474473 $Results += [pscustomobject ]$Result
475474}
476475
477-
478476# endregion
479477
480478# region Final Output and Export
479+ # Finalize and export the filtered policy data to CSV, optionally pruning empty columns
481480if ($Results.Count -eq 0 ) {
482481 Write-Host " No data found for the given criteria."
483482} else {
0 commit comments