A PowerShell-Powered Approach to Automated Monitoring and Troubleshooting

PowerShell May 17, 2024

Every administrator knows the problem, flows... They are hard to administer. The most common problems with Microsoft flow are

  • Failing connections
  • Failing runs (due to errors in the flow itself)

So how can we get an automatic insight into it?

Monitoring Flows

Yes, that sounds simple. But hey it's not very well documented. Sure you can iterate through the complete UI and click every day into the flow details page and hope that you can get an error like this:

But seriously, nobody wants to check it every day or every morning. So there must be a smarter solution for this. For this PowerShell comes to the resqueue. So let's build a small function that will check the last flow run for a failing or a successful run. At first we need to import the required cmdlets with

Install-Module -Name Microsoft.PowerApps.PowerShell -Scope CurrentUser -AllowClobber
Install-Module -Name Microsoft.PowerApps.Administration.PowerShell -AllowClobber

Now after the cmdlets are imported, you will be able to access the powerplatform stuff. But wait there is one more thing.... authentication.... Before we can start we must authenticate against the platform like this:

$pass = ConvertTo-SecureString "SUPER SECRET PASSWORD" -AsPlainText -Force
Add-PowerAppsAccount -Username User@yourdomain.com -Password $pass

So now it's time to get operative. I created a checkfunction like this:


function CheckRun($flowName)
{

    $flow = Get-Flow -FlowName $flowName  
    Write-Host "Checking " $flow.DisplayName "..." -NoNewline
    $runs = Get-FlowRun -FlowName $flowName  
    $Sorted = $runs | Sort-Object -Descending $_.StartDate
    if ($flow.Enabled -eq $true)
    {
        if ($Sorted -eq $null)
        {
            write-host "never run"
        }
        else{
            if ($Sorted -ne $null -and $Sorted[0].Status -eq "Failed")
            {
                write-host "Failed" -ForegroundColor Red
            }
            else
            {
                write-host "Looks good" -ForegroundColor Green
            }
        }
    }else
    {
               write-host "Deactivated" -ForegroundColor Yellow
    }
 
}

This will take as parameter the internal flowname (mostly a guid). Then it will fetch the flow definition itself. This will be done only to get the displayname to print out. After that I will fetch the flow runs of the flow itself.

At least I do some checks against the flow run, especcially if the lst run was sucessfull or not. Also it will wirte a message when the flow is deactivated.

Now it's time to iterate through your defined flows, fo this you can use the get-flow cmdlet and do a foreach like this

$flows = Get-Flow
foreach ($flow in $flows)
{
    CheckRun -flowName $flow.FlowName
}

The result looks like this

Cool! So instead wo clicking through the portal, you will get an nice output. Now you have an automated script to check against the flow run.

Monitor Connections

At the first screenshot we saw only a failing connection. Yes it can be an error but you, we all know. The connections are the most common error sources. So for this we can check the connections separately. I creates this script to check the connections

function CheckConnections()
{
    $connections= Get-PowerAppConnection 
    foreach ($connection in $connections)
    {
        Write-Host "Checking Connection"  $connection.DisplayName " (" $connection.ConnectionName ") !..." -NoNewline
        if ($connection.Statuses[0].status -eq "Error")
        {
      
            Write-host $connection.Statuses[0].error -ForegroundColor Red    
        }
        else
        {
            Write-host "OK" -ForegroundColor Green
        }
    }
}

This will get all connnectison for that the current logged in user has access to. So it will iterare through each connection and check against the last status entry. If there is an error existing, it will output the error itself. This will look like this:

So now you are in full monitoring "control" at you flows as admin.

Restrictions

Actually you must login as a user that has access to all connection and flows that you want to monitor. There is acutally no possibility to access all flows in your organistion via powershell. Beacuse you cannot pass any service principal identity to the cmdlets. But maybe it will be implemented into the future.

Final words

In conclusion, the challenges of administering Microsoft flows, especially dealing with failing connections and runs, are well-known to every administrator. While the concept of monitoring flows for automatic insight seems straightforward, the lack of comprehensive documentation makes it a daunting task. Clicking through the UI daily is impractical, prompting the need for a smarter solution.

Enter PowerShell, a powerful tool for automating such tasks. By building custom functions, like the one presented for checking flow runs, administrators can efficiently monitor their flows' statuses. With a structured approach and the right cmdlets, PowerShell enables seamless integration with the Power Platform, facilitating effective flow management.

The script not only checks for failing runs but also ensures visibility into connection errors, which are often the primary culprits behind flow failures. By providing detailed insights and color-coded outputs, administrators gain full control over their flows' health without the need for manual intervention.

However, there are limitations, notably the requirement for user-level access to all connections and flows for effective monitoring. The inability to access all flows in an organization via PowerShell is a current drawback, but there's hope for future enhancements to address this limitation.

In essence, while challenges persist, leveraging PowerShell for flow monitoring offers a practical and efficient solution, empowering administrators to stay on top of their flow operations with ease and confidence.

Tags