diff --git a/powershell/internal/checks/maester/entra/Test-MtPimAlertsExists.ps1 b/powershell/internal/checks/maester/entra/Test-MtPimAlertsExists.ps1 index c053a4ffc9..b119c70d02 100644 --- a/powershell/internal/checks/maester/entra/Test-MtPimAlertsExists.ps1 +++ b/powershell/internal/checks/maester/entra/Test-MtPimAlertsExists.ps1 @@ -49,6 +49,25 @@ # Keep per-call compatibility properties and filtered counts out of the Graph cache. $Alert = $Alert.PSObject.Copy() $AlertDefinition = $Alert.alertDefinition + + # PIM reports an alert it has never scanned as inactive, which would read as a pass. + $lastScanned = $Alert.lastScannedDateTime + $lastScanUtc = $null + if ($lastScanned -is [datetimeoffset]) { + $lastScanUtc = $lastScanned.UtcDateTime + } elseif ($lastScanned -is [datetime]) { + $lastScanUtc = $lastScanned.ToUniversalTime() + } elseif (-not [string]::IsNullOrWhiteSpace("$lastScanned")) { + $parsedScan = [datetimeoffset]::MinValue + if ([datetimeoffset]::TryParse("$lastScanned", [cultureinfo]::InvariantCulture, [System.Globalization.DateTimeStyles]::AssumeUniversal, [ref]$parsedScan)) { + $lastScanUtc = $parsedScan.UtcDateTime + } + } + if (-not $Alert.isActive -and $null -ne $lastScanUtc -and $lastScanUtc.Year -le 1) { + Add-MtTestResultDetail -SkippedBecause Custom -SkippedCustomReason 'PIM has not scanned this alert in the tenant yet, so there is no result to evaluate. Refresh the PIM alerts for Microsoft Entra roles (POST /beta/identityGovernance/roleManagementAlerts/alerts/refresh), then run Maester again.' + return $null + } + $AffectedRoleAssignments = if ($Alert.isActive) { @($Alert.alertIncidents) } else { @() } # Preserve the PIM v2 result properties consumed by the Maester tests and cmdlet callers. @@ -120,7 +139,8 @@ $($AffectedRoleAssignmentSummary) Get more details from the PIM alert [$(Get-MtSafeMarkdown $Alert.alertName)](https://portal.azure.com/#view/Microsoft_Azure_PIMCommon/AlertDetail/providerId/aadroles/alertId/$($AlertId)/resourceId/$($tenantId)) in the Azure Portal. " } else { - $testResult = 'All privileged role assignments are managed by PIM. Well done!' + $lastScanText = if ($null -ne $lastScanUtc) { " (last scan: $($lastScanUtc.ToString('yyyy-MM-dd HH:mm', [cultureinfo]::InvariantCulture)) UTC)" } else { '' } + $testResult = "Well done. No role assignment in scope is affected by the PIM alert **$(Get-MtSafeMarkdown $Alert.alertName)**$lastScanText." } Add-MtTestResultDetail -Description $testDescription -Result $testResult diff --git a/powershell/tests/functions/Test-MtPimAlertsExists.Tests.ps1 b/powershell/tests/functions/Test-MtPimAlertsExists.Tests.ps1 index a93845c2fd..47b62cc828 100644 --- a/powershell/tests/functions/Test-MtPimAlertsExists.Tests.ps1 +++ b/powershell/tests/functions/Test-MtPimAlertsExists.Tests.ps1 @@ -8,25 +8,27 @@ param( [string] $AlertId = 'RedundantAssignmentAlert', [bool] $IsActive = $true, - [object[]] $AlertIncidents = @() + [object[]] $AlertIncidents = @(), + [object] $LastScannedDateTime = '2026-11-13T01:20:52Z' ) return [PSCustomObject]@{ - id = "DirectoryRole_tenant-id_$AlertId" - alertDefinitionId = "DirectoryRole_tenant-id_$AlertId" - isActive = $IsActive - incidentCount = $AlertIncidents.Count - alertDefinition = [PSCustomObject]@{ + id = "DirectoryRole_tenant-id_$AlertId" + alertDefinitionId = "DirectoryRole_tenant-id_$AlertId" + isActive = $IsActive + incidentCount = $AlertIncidents.Count + lastScannedDateTime = $LastScannedDateTime + alertDefinition = [PSCustomObject]@{ displayName = 'PIM alert name' description = 'PIM alert description' securityImpact = 'PIM security impact' mitigationSteps = 'PIM mitigation steps' howToPrevent = 'PIM prevention guidance' } - alertConfiguration = [PSCustomObject]@{ + alertConfiguration = [PSCustomObject]@{ isEnabled = $true } - alertIncidents = $AlertIncidents + alertIncidents = $AlertIncidents } } @@ -55,16 +57,18 @@ $script:testDescription = $null $script:testResult = $null $script:skippedBecause = $null + $script:skippedCustomReason = $null Mock -ModuleName Maester Get-MgContext { return [PSCustomObject]@{ TenantId = 'tenant-id' } } Mock -ModuleName Maester Add-MtTestResultDetail { - param($Description, $Result, $SkippedBecause) + param($Description, $Result, $SkippedBecause, $SkippedCustomReason) $script:testDescription = $Description $script:testResult = $Result $script:skippedBecause = $SkippedBecause + $script:skippedCustomReason = $SkippedCustomReason } Mock -ModuleName Maester Invoke-WebRequest } @@ -101,7 +105,55 @@ $result = Test-MtPimAlertsExists -AlertId RedundantAssignmentAlert -FilteredBreakGlass @() $result.numberOfAffectedItems | Should -Be 0 - $script:testResult | Should -Be 'All privileged role assignments are managed by PIM. Well done!' + $script:testResult | Should -Be 'Well done. No role assignment in scope is affected by the PIM alert **PIM alert name** (last scan: 2026-11-13 01:20 UTC).' + } + + It 'skips an inactive alert that PIM has never scanned' { + Mock -ModuleName Maester Invoke-MtGraphRequest { + return New-PimAlert -IsActive $false -LastScannedDateTime '0001-01-01T00:00:00Z' + } + Mock -ModuleName Maester Get-MtEamClassification { @{ 'control-plane-role' = 'ControlPlane' } } + + $result = Test-MtPimAlertsExists -AlertId RedundantAssignmentAlert -FilteredAccessLevel ControlPlane -FilteredBreakGlass @() + + $result | Should -BeNullOrEmpty + $script:skippedBecause | Should -Be 'Custom' + $script:skippedCustomReason | Should -Match 'PIM has not scanned this alert' + $script:testResult | Should -BeNullOrEmpty + Should -Invoke Get-MtEamClassification -ModuleName Maester -Times 0 -Exactly + } + + It 'skips an inactive alert whose scan date is the minimum DateTime value' { + Mock -ModuleName Maester Invoke-MtGraphRequest { + return New-PimAlert -IsActive $false -LastScannedDateTime ([datetime]::SpecifyKind([datetime]::MinValue, 'Utc')) + } + + Test-MtPimAlertsExists -AlertId StaleSignInAlert -FilteredBreakGlass @() | Should -BeNullOrEmpty + $script:skippedBecause | Should -Be 'Custom' + } + + It 'still evaluates an active alert whatever its scan date' { + $incident = New-PimAlertIncident -AssigneeId 'user-1' -AssigneeDisplayName 'User One' -AssigneeUserPrincipalName 'user1@contoso.com' + Mock -ModuleName Maester Invoke-MtGraphRequest { + return New-PimAlert -AlertIncidents @($incident) -LastScannedDateTime '0001-01-01T00:00:00Z' + } + + $result = Test-MtPimAlertsExists -AlertId RedundantAssignmentAlert -FilteredBreakGlass @() + + $result.numberOfAffectedItems | Should -Be 1 + $script:skippedBecause | Should -BeNullOrEmpty + } + + It 'evaluates an inactive alert without a scan date as before' { + Mock -ModuleName Maester Invoke-MtGraphRequest { + return New-PimAlert -IsActive $false -LastScannedDateTime $null + } + + $result = Test-MtPimAlertsExists -AlertId RedundantAssignmentAlert -FilteredBreakGlass @() + + $result.numberOfAffectedItems | Should -Be 0 + $script:skippedBecause | Should -BeNullOrEmpty + $script:testResult | Should -Be 'Well done. No role assignment in scope is affected by the PIM alert **PIM alert name**.' } It 'filters incidents by Enterprise Access Model role classification' { diff --git a/tests/maester/entra/Test.MT.1029.md b/tests/maester/entra/Test.MT.1029.md index d51437065b..3af610da4a 100644 --- a/tests/maester/entra/Test.MT.1029.md +++ b/tests/maester/entra/Test.MT.1029.md @@ -1,6 +1,6 @@ Stale accounts should not be assigned to privileged roles. -This test reads the Privileged Identity Management security alert **Accounts in a privileged role have not signed in during the past n days** (`StaleSignInAlert`) from Microsoft Graph and passes when the alert is not active or has no affected role assignments. Emergency access (break glass) accounts are excluded from the affected items. +This test reads the Privileged Identity Management security alert **Accounts in a privileged role have not signed in during the past n days** (`StaleSignInAlert`) from Microsoft Graph and passes when the alert is not active or has no affected role assignments. If PIM has never scanned the alert in the tenant, the test is skipped until the alerts are refreshed. Emergency access (break glass) accounts are excluded from the affected items. #### Remediation action diff --git a/tests/maester/entra/Test.MT.1030.md b/tests/maester/entra/Test.MT.1030.md index d0a103114c..bb393c46b7 100644 --- a/tests/maester/entra/Test.MT.1030.md +++ b/tests/maester/entra/Test.MT.1030.md @@ -1,6 +1,6 @@ Eligible role assignments on the Control Plane should be in use by administrators. -This test reads the Privileged Identity Management security alert **Administrators aren't using their privileged roles** (`RedundantAssignmentAlert`) from Microsoft Graph and passes when the alert is not active or has no affected role assignments. Emergency access (break glass) accounts are excluded from the affected items. The alert is filtered to Control Plane roles (Enterprise Access Model tiering). +This test reads the Privileged Identity Management security alert **Administrators aren't using their privileged roles** (`RedundantAssignmentAlert`) from Microsoft Graph and passes when the alert is not active or has no affected role assignments. If PIM has never scanned the alert in the tenant, the test is skipped until the alerts are refreshed. Emergency access (break glass) accounts are excluded from the affected items. The alert is filtered to Control Plane roles (Enterprise Access Model tiering). #### Remediation action diff --git a/tests/maester/entra/Test.MT.1031.md b/tests/maester/entra/Test.MT.1031.md index d5b7b9a6ad..daa1f88a75 100644 --- a/tests/maester/entra/Test.MT.1031.md +++ b/tests/maester/entra/Test.MT.1031.md @@ -1,6 +1,6 @@ Privileged roles on the Control Plane should be managed by Privileged Identity Management (PIM) only. -This test reads the Privileged Identity Management security alert **Roles are being assigned outside of Privileged Identity Management** (`RolesAssignedOutsidePimAlert`) from Microsoft Graph and passes when the alert is not active or has no affected role assignments. Emergency access (break glass) accounts are excluded from the affected items. The alert is filtered to Control Plane roles (Enterprise Access Model tiering). +This test reads the Privileged Identity Management security alert **Roles are being assigned outside of Privileged Identity Management** (`RolesAssignedOutsidePimAlert`) from Microsoft Graph and passes when the alert is not active or has no affected role assignments. If PIM has never scanned the alert in the tenant, the test is skipped until the alerts are refreshed. Emergency access (break glass) accounts are excluded from the affected items. The alert is filtered to Control Plane roles (Enterprise Access Model tiering). #### Remediation action diff --git a/tests/maester/entra/Test.MT.1032.md b/tests/maester/entra/Test.MT.1032.md index 3251e29259..2d3db80391 100644 --- a/tests/maester/entra/Test.MT.1032.md +++ b/tests/maester/entra/Test.MT.1032.md @@ -1,6 +1,6 @@ A limited number of Global Administrators should be assigned. -This test reads the Privileged Identity Management security alert **There are too many global administrators** (`TooManyGlobalAdminsAssignedToTenantAlert`) from Microsoft Graph and passes when the alert is not active or has no affected role assignments. Emergency access (break glass) accounts are excluded from the affected items. +This test reads the Privileged Identity Management security alert **There are too many global administrators** (`TooManyGlobalAdminsAssignedToTenantAlert`) from Microsoft Graph and passes when the alert is not active or has no affected role assignments. If PIM has never scanned the alert in the tenant, the test is skipped until the alerts are refreshed. Emergency access (break glass) accounts are excluded from the affected items. #### Remediation action