Export Conditional Access Policies

While converting object Ids to name, error thrown for deleted objects-fixed
This commit is contained in:
AdminDroid 2024-08-09 14:49:39 +05:30
parent 71c8c409e3
commit e81c613096

View File

@ -2,7 +2,7 @@
============================================================================================= =============================================================================================
Name: Export Conditional Access Policies to Excel using PowerShell Name: Export Conditional Access Policies to Excel using PowerShell
Description: The script exports all Conditional Access policies to an Excel file. Description: The script exports all Conditional Access policies to an Excel file.
Version: 2.0 Version: 2.2
Website: o365reports.com Website: o365reports.com
Script Highlights: Script Highlights:
@ -98,10 +98,22 @@ Function ConvertTo-Name {
} }
# Retrieve the display name for the directory object with the given ID # Retrieve the display name for the directory object with the given ID
else{ else{
$Name = (Get-MgBetaDirectoryObject -DirectoryObjectId $Id).AdditionalProperties["displayName"] try
{
$Name = ((Get-MgBetaDirectoryObject -DirectoryObjectId $Id ).AdditionalProperties["displayName"] )
if($Name -ne $null)
{
$DirectoryObjsHash[$Id]=$Name $DirectoryObjsHash[$Id]=$Name
}
$ConvertedNames += $Name $ConvertedNames += $Name
}
}
catch
{
Write-Host "Deleted object configured in the CA policy $CAName" -ForegroundColor Red
Write-Host "Processing CA policies..."
}
}
} }
return $ConvertedNames return $ConvertedNames
} }
@ -235,7 +247,7 @@ Get-MgBetaIdentityConditionalAccessPolicy -All | Foreach {
} }
$IncludeUsers=$IncludeUsers -join "," $IncludeUsers=$IncludeUsers -join ","
if($ExcludeUsers.Count -ne 0)# -and ($ExcludeUsers -ne 'GuestsOrExternalUsers' )) if(($ExcludeUsers.Count -ne 0) -and ($ExcludeUsers -ne 'GuestsOrExternalUsers' ))
{ {
$ExcludeUsers=ConvertTo-Name -InputIds $ExcludeUsers $ExcludeUsers=ConvertTo-Name -InputIds $ExcludeUsers
} }