Monitoring Power Platform API Rate Limits using PowerShell

Loading

Power Platform has API rate limits that control the number of requests per user or per environment within a specific time frame. Monitoring API consumption ensures compliance with limits and prevents throttling.


Step 1: Install and Import Power Platform PowerShell Modules

Ensure you have the required PowerShell modules installed.

# Install Power Platform PowerShell modules
Install-Module -Name Microsoft.PowerApps.Administration.PowerShell -Scope CurrentUser -Force
Install-Module -Name Microsoft.PowerApps.PowerShell -Scope CurrentUser -Force

# Import modules
Import-Module Microsoft.PowerApps.Administration.PowerShell
Import-Module Microsoft.PowerApps.PowerShell

Step 2: Authenticate to Power Platform

Connect to Power Platform as an admin.

powershellCopyEdit# Login with admin credentials
Add-PowerAppsAccount

For service principal authentication:

$AppId = "YOUR_APP_ID"
$TenantId = "YOUR_TENANT_ID"
$CertificateThumbprint = "YOUR_CERT_THUMBPRINT"

Connect-AdminPowerAppEnvironment -ApplicationId $AppId -TenantId $TenantId -CertificateThumbprint $CertificateThumbprint

Step 3: Retrieve API Rate Limit Usage

Check API usage limits for a specific environment.

# Get all environments
$environments = Get-AdminPowerAppEnvironment

# Loop through each environment and get API limits
foreach ($env in $environments) {
Write-Host "Checking API Limits for Environment: $($env.DisplayName)"

# Retrieve API rate limit details
$limits = Get-AdminPowerAppApiUsage -EnvironmentName $env.EnvironmentName

$limits | Select-Object EnvironmentName, UserId, EntitlementName, Consumed, Limit, Remaining, StartTime, EndTime |
Format-Table -AutoSize
}

Step 4: Export API Usage Data to CSV

To analyze API consumption over time, export the data to a CSV file.

$apiUsage = Get-AdminPowerAppApiUsage

# Export API consumption details
$apiUsage | Select-Object EnvironmentName, UserId, EntitlementName, Consumed, Limit, Remaining, StartTime, EndTime |
Export-Csv -Path "C:\PowerPlatform\APIUsageReport.csv" -NoTypeInformation

Step 5: Monitor API Consumption for Specific Users

To track API usage per user:

$UserId = "user@example.com"  # Replace with actual user email

$usage = Get-AdminPowerAppApiUsage | Where-Object { $_.UserId -eq $UserId }

$usage | Format-Table EnvironmentName, EntitlementName, Consumed, Limit, Remaining, StartTime, EndTime

Step 6: Set Up an Alert for High API Usage

Automatically alert when API consumption reaches 90% of the limit.

# Get API consumption details
$apiUsage = Get-AdminPowerAppApiUsage

# Define threshold (90% usage)
$threshold = 0.9

# Check if any user exceeds the threshold
$highUsage = $apiUsage | Where-Object { ($_.Consumed / $_.Limit) -ge $threshold }

if ($highUsage) {
Write-Host "WARNING: High API consumption detected!"
$highUsage | Format-Table EnvironmentName, UserId, EntitlementName, Consumed, Limit, Remaining
}

Step 7: Automate API Monitoring with a Scheduled Task

Save the script as MonitorAPIUsage.ps1 and create a Windows Task Scheduler job to run it daily.

# Schedule a task to monitor API usage daily
$Trigger = New-ScheduledTaskTrigger -Daily -At 12:00PM
$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "C:\Scripts\MonitorAPIUsage.ps1"
Register-ScheduledTask -TaskName "PowerPlatformAPIMonitoring" -Trigger $Trigger -User "Administrator" -Action $Action -RunLevel Highest -Force

Step 8: Disconnect Power Platform Session

(Optional) To close the session after executing commands:

Disconnect-AdminPowerAppEnvironment

Leave a Reply

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