Skip to content

Monitoring Risky User Status with Microsoft Graph API: PowerShell Script

Monitoring Risky User Status with Microsoft Graph API

Section titled “Monitoring Risky User Status with Microsoft Graph API”

Understanding and managing risky user activities is crucial for maintaining the security of your Azure AD environment. Identifying potential threats early allows for prompt action to protect your organization’s resources. In this article, we will explore a PowerShell script that automates the retrieval and display of risky user status using Microsoft Graph API.


The script is designed to connect to Microsoft Graph and retrieve information about users deemed “risky” by Azure AD. The script organizes this information into a structured format, making it easier for administrators to review and act upon.

FeatureCapabilityBusiness Value
Risk DetectionIdentifies users flagged as risky by Azure ADProactive security monitoring
Structured OutputFormats data in JSON for easy processingIntegration with security workflows
Comprehensive DataIncludes risk levels, states, and timestampsComplete risk assessment context
Automated RetrievalBatch processing of all risky usersEfficient security operations

Terminal window
<#
.SYNOPSIS
Get-RiskyUserStatus.ps1
.DESCRIPTION
Retrieve and display the risky user status from Microsoft Graph.
#>
# Import necessary modules
Import-Module -Name 'Microsoft.Graph'
Import-Module -Name 'Microsoft.Graph.Beta.Identity.SignIns'
# Define constants
$TenantId = "" # Azure AD Tenant ID
$ClientId = "" # Application (client) ID
$ClientSecret = "" # Client secret
# Convert Client Secret to Secure String
$SecureClientSecret = ConvertTo-SecureString $ClientSecret -AsPlainText -Force
# Create credential object
$Credential = New-Object System.Management.Automation.PSCredential($ClientId, $SecureClientSecret)
# Acquire a token
$Token = Get-MsalToken -ClientId $ClientId -TenantId $TenantId -ClientSecret $SecureClientSecret -Scopes https://graph.microsoft.com/.default
# Convert token to secure string
$SecureToken = ConvertTo-SecureString $Token.AccessToken -AsPlainText -Force
# Connect to Microsoft Graph
Connect-MgGraph -AccessToken $SecureToken
# Retrieve the risky users
$riskyUsers = Get-MgRiskyUser
# Create result object
$result = $riskyUsers | ForEach-Object {
[PSCustomObject]@{
Id = $_.Id
IsDeleted = $_.IsDeleted
IsProcessing = $_.IsProcessing
RiskLevel = $_.RiskLevel
RiskState = $_.RiskState
RiskDetail = $_.RiskDetail
RiskLastUpdatedDateTime = $_.RiskLastUpdatedDateTime
UserDisplayName = $_.UserDisplayName
UserPrincipalName = $_.UserPrincipalName
}
}
# Convert result to JSON format
echo "</report>"
$result | ConvertTo-Json -Compress
echo "</report>"

ComponentRequirementPurpose
Microsoft Graph ModulesMicrosoft.Graph, Microsoft.Graph.Beta.Identity.SignInsRisk detection API access
Azure AD App RegistrationApplication with risk detection permissionsSecure API access
Tenant CredentialsTenant ID, Client ID, Client SecretAuthentication parameters

Ideal for: Security administrators monitoring user risk levels

Required Permissions:

  • IdentityRiskyUser.Read.All - Read risky user information
  • IdentityRiskEvent.Read.All - Access risk event data

  1. Configure Authentication Parameters:

    Terminal window
    $TenantId = "your-tenant-id"
    $ClientId = "your-app-client-id"
    $ClientSecret = "your-client-secret"
  2. Execute the Script:

    • Run with appropriate PowerShell permissions
    • Review JSON output for risk analysis
    • Integrate with security monitoring workflows

FieldDescriptionSecurity Value
RiskLevelCurrent risk assessment levelImmediate threat indicator
RiskStateCurrent state of risk evaluationProcessing status tracking
RiskDetailSpecific risk factors identifiedDetailed threat analysis
RiskLastUpdatedDateTimeLast risk assessment timestampRecency of threat detection

Key Takeaway: This script provides essential visibility into user risk status, enabling security teams to proactively identify and respond to potential security threats before they impact the organization.