|
| 1 | +function Get-CIPPAlertNewRiskyUsers { |
| 2 | + <# |
| 3 | + .FUNCTIONALITY |
| 4 | + Entrypoint |
| 5 | + #> |
| 6 | + [CmdletBinding()] |
| 7 | + Param ( |
| 8 | + [Parameter(Mandatory = $false)] |
| 9 | + [Alias('input')] |
| 10 | + $TenantFilter |
| 11 | + ) |
| 12 | + $Deltatable = Get-CIPPTable -Table DeltaCompare |
| 13 | + try { |
| 14 | + # Check if tenant has P2 capabilities |
| 15 | + $Capabilities = Get-CIPPTenantCapabilities -TenantFilter $TenantFilter |
| 16 | + if (-not $Capabilities.AADPremiumService) { |
| 17 | + Write-AlertMessage -tenant $($TenantFilter) -message 'Tenant does not have Azure AD Premium P2 licensing required for risky users detection' |
| 18 | + return |
| 19 | + } |
| 20 | + |
| 21 | + $Filter = "PartitionKey eq 'RiskyUsersDelta' and RowKey eq '{0}'" -f $TenantFilter |
| 22 | + $RiskyUsersDelta = (Get-CIPPAzDataTableEntity @Deltatable -Filter $Filter).delta | ConvertFrom-Json -ErrorAction SilentlyContinue |
| 23 | + |
| 24 | + # Get current risky users with more detailed information |
| 25 | + $NewDelta = (New-GraphGetRequest -uri 'https://graph.microsoft.com/v1.0/identityProtection/riskyUsers' -tenantid $TenantFilter) | Select-Object userPrincipalName, riskLevel, riskState, riskDetail, riskLastUpdatedDateTime, isProcessing, history |
| 26 | + |
| 27 | + $NewDeltatoSave = $NewDelta | ConvertTo-Json -Depth 10 -Compress -ErrorAction SilentlyContinue | Out-String |
| 28 | + $DeltaEntity = @{ |
| 29 | + PartitionKey = 'RiskyUsersDelta' |
| 30 | + RowKey = [string]$TenantFilter |
| 31 | + delta = "$NewDeltatoSave" |
| 32 | + } |
| 33 | + Add-CIPPAzDataTableEntity @DeltaTable -Entity $DeltaEntity -Force |
| 34 | + |
| 35 | + if ($RiskyUsersDelta) { |
| 36 | + $AlertData = $NewDelta | Where-Object { |
| 37 | + $_.userPrincipalName -notin $RiskyUsersDelta.userPrincipalName |
| 38 | + } | ForEach-Object { |
| 39 | + $riskHistory = if ($_.history) { |
| 40 | + $latestHistory = $_.history | Sort-Object -Property riskLastUpdatedDateTime -Descending | Select-Object -First 1 |
| 41 | + "Previous Risk Level: $($latestHistory.riskLevel), Last Updated: $($latestHistory.riskLastUpdatedDateTime)" |
| 42 | + } |
| 43 | + else { |
| 44 | + 'No previous risk history' |
| 45 | + } |
| 46 | + |
| 47 | + # Map risk level to severity |
| 48 | + $severity = switch ($_.riskLevel) { |
| 49 | + 'high' { 'Critical' } |
| 50 | + 'medium' { 'Warning' } |
| 51 | + 'low' { 'Info' } |
| 52 | + default { 'Info' } |
| 53 | + } |
| 54 | + |
| 55 | + @{ |
| 56 | + Message = "New risky user detected: $($_.userPrincipalName)" |
| 57 | + Details = @{ |
| 58 | + RiskLevel = $_.riskLevel |
| 59 | + RiskState = $_.riskState |
| 60 | + RiskDetail = $_.riskDetail |
| 61 | + LastUpdated = $_.riskLastUpdatedDateTime |
| 62 | + IsProcessing = $_.isProcessing |
| 63 | + RiskHistory = $riskHistory |
| 64 | + Severity = $severity |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + if ($AlertData) { |
| 70 | + Write-AlertTrace -cmdletName $MyInvocation.MyCommand -tenantFilter $TenantFilter -data $AlertData |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + catch { |
| 75 | + Write-AlertMessage -tenant $($TenantFilter) -message "Could not get risky users for $($TenantFilter): $(Get-NormalizedError -message $_.Exception.message)" |
| 76 | + } |
| 77 | +} |
0 commit comments