@@ -48,15 +48,16 @@ function Test-CIPPAccessTenant {
4848 $ExchangeStatus = $false
4949
5050 $Results = [PSCustomObject ]@ {
51- TenantName = $Tenant.defaultDomainName
52- GraphStatus = $false
53- GraphTest = ' '
54- ExchangeStatus = $false
55- ExchangeTest = ' '
56- GDAPRoles = ' '
57- MissingRoles = ' '
58- OrgManagementRoles = @ ()
59- LastRun = (Get-Date ).ToUniversalTime()
51+ TenantName = $Tenant.defaultDomainName
52+ GraphStatus = $false
53+ GraphTest = ' '
54+ ExchangeStatus = $false
55+ ExchangeTest = ' '
56+ GDAPRoles = ' '
57+ MissingRoles = ' '
58+ OrgManagementRoles = @ ()
59+ OrgManagementRolesMissing = @ ()
60+ LastRun = (Get-Date ).ToUniversalTime()
6061 }
6162
6263 $AddedText = ' '
@@ -104,39 +105,32 @@ function Test-CIPPAccessTenant {
104105
105106 try {
106107 $null = New-ExoRequest - tenantid $Tenant.customerId - cmdlet ' Get-OrganizationConfig' - ErrorAction Stop
107- $ExchangeStatus = $true
108- $ExchangeTest = ' Successfully connected to Exchange'
109-
110- # Get the Exchange role definitions and assignments for the Organization Management role group
111- $Requests = @ (
112- @ {
113- id = ' roleDefinitions'
114- method = ' GET'
115- url = ' roleManagement/exchange/roleDefinitions?$top=999'
116- }
117- @ {
118- id = ' roleAssignments'
119- method = ' GET'
120- url = " roleManagement/exchange/roleAssignments?`$ filter=principalId eq '/RoleGroups/Organization Management'&`$ top=999"
121- }
122- )
123-
124- $ExchangeRoles = New-GraphBulkRequest - tenantid $Tenant.customerId - Requests $Requests
125-
126- # Get results and expand assigments with role definitions
127- $RoleDefinitions = ($ExchangeRoles | Where-Object - Property id -EQ ' roleDefinitions' ).body.value | Select-Object - Property id, displayName, description, isBuiltIn, isEnabled
128- $RoleAssignments = ($ExchangeRoles | Where-Object - Property id -EQ ' roleAssignments' ).body.value
129- $OrgManagementAssignments = $RoleAssignments | Where-Object - Property principalId -EQ ' /RoleGroups/Organization Management' | Sort-Object - Property roleDefinitionId - Unique
130- $OrgManagementRoles = $OrgManagementAssignments | ForEach-Object {
131- $RoleDefinitions | Where-Object - Property id -EQ $_.roleDefinitionId
132- } | Sort-Object - Property displayName
133108
134- Write-Warning " Found $ ( $OrgManagementRoles.Count ) Organization Management role assignments in Exchange"
109+ $OrgManagementRoles = New-ExoRequest - tenantid $Tenant.customerId - cmdlet ' Get-ManagementRoleAssignment' - cmdParams @ { RoleAssignee = ' Organization Management' ; Delegating = $false } | Select-Object - Property Role, Guid
110+ Write-Information " Found $ ( $OrgManagementRoles.Count ) Organization Management roles in Exchange"
135111 $Results.OrgManagementRoles = $OrgManagementRoles
136112
137- # TODO: Get list of known good roles and compare against the found roles
138-
139-
113+ $RoleDefinitions = New-GraphGetRequest - tenantid $Tenant.customerId - uri ' https://graph.microsoft.com/beta/roleManagement/exchange/roleDefinitions'
114+ Write-Information " Found $ ( $RoleDefinitions.Count ) Exchange role definitions"
115+
116+ $BasePath = Get-Module - Name ' CIPPCore' | Select-Object - ExpandProperty ModuleBase
117+ $AllOrgManagementRoles = Get-Content - Path " $BasePath \Public\OrganizationManagementRoles.json" - ErrorAction Stop | ConvertFrom-Json
118+ Write-Information " Loaded all Organization Management roles from $BasePath \Public\OrganizationManagementRoles.json"
119+
120+ $AvailableRoles = $RoleDefinitions | Where-Object - Property displayName -In $AllOrgManagementRoles | Select-Object - Property displayName, id, description
121+ Write-Information " Found $ ( $AvailableRoles.Count ) available Organization Management roles in Exchange"
122+ $MissingOrgMgmtRoles = $AvailableRoles | Where-Object { $OrgManagementRoles.Role -notcontains $_.displayName }
123+ if (($MissingOrgMgmtRoles | Measure-Object ).Count -gt 0 ) {
124+ $Results.OrgManagementRolesMissing = $MissingOrgMgmtRoles
125+ Write-Warning " Found $ ( $MissingRoles.Count ) missing Organization Management roles in Exchange"
126+ $ExchangeStatus = $false
127+ $ExchangeTest = ' Connected to Exchange but missing permissions in Organization Management. This may impact the ability to manage Exchange features'
128+ Write-LogMessage - headers $Headers - API $APINAME - tenant $tenant.defaultDomainName - message ' Tenant access check for Exchange failed: Missing Organization Management roles' - Sev ' Warning' - LogData $MissingOrgMgmtRoles
129+ } else {
130+ Write-Warning ' All available Organization Management roles are present in Exchange'
131+ $ExchangeStatus = $true
132+ $ExchangeTest = ' Successfully connected to Exchange'
133+ }
140134 } catch {
141135 $ErrorMessage = Get-CippException - Exception $_
142136 $ReportedError = ($_.ErrorDetails | ConvertFrom-Json - ErrorAction SilentlyContinue)
@@ -145,6 +139,7 @@ function Test-CIPPAccessTenant {
145139
146140 $ExchangeTest = " Failed to connect to Exchange: $ ( $ErrorMessage.NormalizedError ) "
147141 Write-LogMessage - headers $Headers - API $APINAME - tenant $tenant.defaultDomainName - message " Tenant access check for Exchange failed: $ ( $ErrorMessage.NormalizedError ) " - Sev ' Error' - LogData $ErrorMessage
142+ Write-Warning " Failed to connect to Exchange: $ ( $_.Exception.Message ) "
148143 }
149144
150145 if ($GraphStatus -and $ExchangeStatus ) {
0 commit comments