Monitoring Power BI API Calls using PowerShell

Loading

Monitoring Power BI API calls is essential for tracking service usage, performance, and security. With PowerShell, you can retrieve API call logs, analyze activity, and ensure compliance.

This guide will show you step-by-step how to monitor Power BI API calls using PowerShell.


Step 1: Prerequisites

1. Install Power BI PowerShell Module

Ensure the Microsoft Power BI Management module is installed:

Install-Module MicrosoftPowerBIMgmt -Scope CurrentUser -Force

Power BI PowerShell module installed!

2. Connect to Power BI Service

Authenticate with Power BI:

Connect-PowerBIServiceAccount

Authenticated successfully!


Step 2: Retrieve API Activity Logs

Power BI stores API logs in activity logs. To fetch logs for the last 7 days:

Get-PowerBIActivityEvent -StartDateTime (Get-Date).AddDays(-7) -EndDateTime (Get-Date) | Export-Csv "C:\PowerBI_API_Log.csv" -NoTypeInformation

API activity logs exported!


Step 3: Filter API Calls by User

To check API calls made by a specific user:

$userEmail = "user@yourdomain.com"

Get-PowerBIActivityEvent -StartDateTime (Get-Date).AddDays(-7) -EndDateTime (Get-Date) |
Where-Object { $_.UserId -eq $userEmail } |
Select-Object UserId, OperationName, ActivityDateTime |
Format-Table -AutoSize

Filtered API calls by user!


Step 4: Monitor API Calls for a Specific Workspace

To check API calls related to a specific Power BI workspace:

$workspaceId = "<Workspace_ID>"

Get-PowerBIActivityEvent -StartDateTime (Get-Date).AddDays(-7) -EndDateTime (Get-Date) |
Where-Object { $_.WorkSpaceId -eq $workspaceId } |
Select-Object UserId, OperationName, ActivityDateTime |
Format-Table -AutoSize

API calls filtered by workspace!


Step 5: Identify High API Usage

To find users making excessive API calls:

Get-PowerBIActivityEvent -StartDateTime (Get-Date).AddDays(-7) -EndDateTime (Get-Date) |
Group-Object -Property UserId |
Sort-Object Count -Descending |
Select-Object Name, Count |
Format-Table -AutoSize

High API usage users identified!


Step 6: Automate API Monitoring with Alerts

To automate API monitoring and send alerts:

1. Create a PowerShell Script (Monitor_API_Usage.ps1)

# Connect to Power BI
Connect-PowerBIServiceAccount

# Set threshold for high API usage
$apiThreshold = 1000

# Retrieve API events for the last 24 hours
$apiLogs = Get-PowerBIActivityEvent -StartDateTime (Get-Date).AddDays(-1) -EndDateTime (Get-Date)

# Identify users exceeding the API threshold
$highUsageUsers = $apiLogs | Group-Object -Property UserId | Where-Object { $_.Count -gt $apiThreshold }

# Send alert if threshold exceeded
if ($highUsageUsers) {
$highUsageUsers | Format-Table -AutoSize | Out-File "C:\PowerBI_API_Alert.txt"
Send-MailMessage -To "admin@yourdomain.com" -From "monitor@yourdomain.com" -Subject "Power BI High API Usage Alert" -Body "Users exceeding API threshold detected!" -Attachments "C:\PowerBI_API_Alert.txt" -SmtpServer "smtp.yourdomain.com"
Write-Host " Alert sent!"
} else {
Write-Host " No high API usage detected."
}

Automated API monitoring enabled!

2. Schedule Script in Task Scheduler

  1. Open Task Scheduler.
  2. Click Create Basic Task.
  3. Set trigger: Daily at Midnight.
  4. Set action: Run PowerShell script:
powershell.exe -ExecutionPolicy Bypass -File "C:\Monitor_API_Usage.ps1"

Automated daily API monitoring set up!

Leave a Reply

Your email address will not be published. Required fields are marked *