Scheduling Automated Power Platform Health Checks using PowerShell

Loading

Automating Power Platform health checks using PowerShell ensures that your environments, connectors, apps, and flows are operating optimally. This guide will help you schedule automated health checks and receive reports.


Step 1: Install and Import Power Platform PowerShell Modules

Ensure that Power Platform PowerShell modules are installed.

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

# Import the 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.

# Login interactively
Add-PowerAppsAccount

For service principal authentication, use:

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

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

Step 3: Define Health Check Parameters

Perform health checks for:
Environments – Ensure environments are active.
Apps – Identify broken or unused apps.
Flows – Detect failed flows.
Connectors – Check for unauthorized or broken connections.
API Limits – Monitor API consumption.


Step 4: Automate Power Platform Health Check

Create a PowerShell script to check environments, apps, flows, and connectors.

# Define report path
$ReportPath = "C:\PowerPlatformHealthCheck.csv"

# Get all environments
$environments = Get-AdminPowerAppEnvironment
$envResults = @()

foreach ($env in $environments) {
$envStatus = if ($env.State -eq "Ready") { "Healthy" } else { "Issue Detected" }

$envResults += [PSCustomObject]@{
EnvironmentName = $env.DisplayName
EnvironmentID = $env.EnvironmentName
Location = $env.Location
Status = $envStatus
}
}

# Get all apps
$apps = Get-AdminPowerApp
$appResults = @()

foreach ($app in $apps) {
$appStatus = if ($app.IsDisabled) { "Disabled" } else { "Active" }

$appResults += [PSCustomObject]@{
AppName = $app.DisplayName
EnvironmentID = $app.EnvironmentName
Status = $appStatus
LastModified = $app.LastModifiedTime
}
}

# Get all flows and check for failures
$flows = Get-AdminFlow
$flowResults = @()

foreach ($flow in $flows) {
$failedRuns = Get-AdminFlowRun -EnvironmentName $flow.EnvironmentName -FlowName $flow.FlowName | Where-Object { $_.Status -eq "Failed" }

$flowStatus = if ($failedRuns.Count -gt 0) { "Issues Detected" } else { "Healthy" }

$flowResults += [PSCustomObject]@{
FlowName = $flow.DisplayName
EnvironmentID = $flow.EnvironmentName
Status = $flowStatus
FailedRuns = $failedRuns.Count
}
}

# Export report
$envResults + $appResults + $flowResults | Export-Csv -Path $ReportPath -NoTypeInformation
Write-Host "Health check report generated: $ReportPath"

Step 5: Schedule the Health Check Script

To run the health check daily at 2 AM, use Task Scheduler:

  1. Open Task Scheduler → Click Create Basic Task
  2. Name: Power Platform Health Check
  3. Trigger: Daily, Time: 2:00 AM
  4. Action: Start a Program
  5. Program: powershell.exe -File "C:\Scripts\PowerPlatformHealthCheck.ps1"
  6. Click Finish

To verify the task:

Get-ScheduledTask -TaskName "Power Platform Health Check"

Step 6: Send Health Check Report via Email

To email the report, modify the script:

# Define email parameters
$SMTPServer = "smtp.office365.com"
$From = "admin@yourdomain.com"
$To = "it-team@yourdomain.com"
$Subject = "Power Platform Health Check Report"
$Body = "Attached is the latest Power Platform Health Check report."

# Send email with report attachment
Send-MailMessage -From $From -To $To -Subject $Subject -Body $Body -Attachments $ReportPath -SmtpServer $SMTPServer -UseSsl -Credential (Get-Credential)

Step 7: Monitor API Rate Limits

To track Dataverse API consumption, use:

Get-AdminPowerAppEnvironment -EnvironmentName "YourEnvironmentID" | Select-Object -ExpandProperty Capacity

Step 8: Disconnect the Session

(Optional) Close the session after execution.

Disconnect-AdminPowerAppEnvironment

Leave a Reply

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