Skip to content

"Monitoring Risky User Status with Microsoft Graph API: PowerShell Script"

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.

SCRIPT OVERVIEW

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.

Here is the script:

<# .SYNOPSIS Get-RiskyUserStatus.ps1

.DESCRIPTION Retrieve and display the risky user status from Microsoft Graph. #>

Import-Module -Name ‘Microsoft.Graph’ Import-Module -Name ‘Microsoft.Graph.Beta.Identity.SignIns’

$TenantId = "" # Azure AD Tenant ID $ClientId = "" # Application (client) ID $ClientSecret = "" # Client secret

$SecureClientSecret = ConvertTo-SecureString $ClientSecret -AsPlainText -Force

$Credential = New-Object System.Management.Automation.PSCredential($ClientId, $SecureClientSecret)

$Token = Get-MsalToken -ClientId $ClientId -TenantId $TenantId -ClientSecret $SecureClientSecret -Scopes https://graph.microsoft.com/.default

$SecureToken = ConvertTo-SecureString $Token.AccessToken -AsPlainText -Force

Connect-MgGraph -AccessToken $SecureToken

$riskyUsers = Get-MgRiskyUser

$result = $riskyUsers | ForEach-Object { [PSCustomObject]@{ Id = $.Id IsDeleted = $.IsDeleted IsProcessing = $.IsProcessing RiskLevel = $.RiskLevel RiskState = $.RiskState RiskDetail = $.RiskDetail RiskLastUpdatedDateTime = $.RiskLastUpdatedDateTime UserDisplayName = $.UserDisplayName UserPrincipalName = $_.UserPrincipalName } }

echo "" $result | ConvertTo-Json -Compress echo ""