Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
72 changes: 62 additions & 10 deletions powershell/tests/functions/Test-MtPimAlertsExists.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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' {
Expand Down
2 changes: 1 addition & 1 deletion tests/maester/entra/Test.MT.1029.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/maester/entra/Test.MT.1030.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/maester/entra/Test.MT.1031.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/maester/entra/Test.MT.1032.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Loading