Retrieve the executions of the flow power automate

Louise Sweeney 1 Reputation point
2022-06-09T13:47:33.253+00:00

Hello,
I have a powershell script which is supposed to retrieve all my Flow Power Automate specified in the config file (Flow-Stats.config). When the script retrieves these Flows using the PowerShell code (Flow-Stats.ps1), it generates a text file for me like this:

   ******@dsqdqsd.onmicrosoft.com - PS_FLOW - 09/06/2022 01:00:00 - Flux note de frais - aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaa - 0 - 0  
   ******@dsqdqsd.onmicrosoft.com - PS_FLOW - 09/06/2022 01:00:00 - nombre annee - aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaa - 0 - 0  
   ******@dsqdqsd.onmicrosoft.com - PS_FLOW - 09/06/2022 01:00:00 - Flux Occupation des locaux - aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaa - 0 - 0  
   ******@dsqdqsd.onmicrosoft.com - PS_FLOW - 09/06/2022 01:00:00 - pomodori - aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaa - 0 - 0  

But there is a problem, because for each flow I get "0 - 0" at the end of each line. However, that should tell me the number of times my Flows have been executed today and the number of errors that there were during the execution of these Flows.
Could you help me to solve this problem please? :( Thanks in advance!

   Flow-Stats.config :  
     
   {  
       "tenantId" : "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaa",   
       "certifThumb" : "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",  
       "appId" : "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaa",  
       "triggerHour" : "01:00",  
       "outputFile" : ".\\flow-stats.txt",  
       "errorsFile" : ".\\flow-errors.txt",  
       "dataFile" : ".\\flow-data.json",  
       "flows" : [  
           {  
               "name": "Flux note de frais",  
               "mailbox": "******@sqds.onmicrosoft.com",  
               "serviceAccount" : "PS_FLOW",   
   			"id": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaa"  
           },  
   		  {  
               "name": "nombre annee",  
               "mailbox": "******@sqds.onmicrosoft.com",  
               "serviceAccount" : "PS_FLOW",   
   			"id": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaa"  
           },  
   		{  
               "name": "Flux Occupation des locaux",  
               "mailbox": "******@sqds.onmicrosoft.com",  
               "serviceAccount" : "PS_FLOW",   
   			"id": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaa"  
           },  
   		{  
               "name": "pomodori",  
               "mailbox": "******@sqds.onmicrosoft.com",  
               "serviceAccount" : "PS_FLOW",   
   			"id": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaa"  
           }  
       ]  
   }  




   Flow-Stats.ps1 :  
     
   $ErrorActionPreference = "Stop"  
     
   $PSModulesNames = "Microsoft.PowerApps.PowerShell", "Microsoft.PowerApps.Administration.PowerShell"  
   $configFileName = "Flow-Stats.config"  
   $jsonDateFormat = "o" #2022-03-23T16:37:51.8011737+01:00  
     
   $PSModulesNames | ForEach-Object {  
       Import-Module $_ -EA SilentlyContinue  
       $module = Get-Module -Name $_  
       if($null -eq $module) {  
           throw "Module '$_' not found"  
       }  
   }  
     
   $config = Get-Content ".\$configFileName" -Encoding UTF8 -EA SilentlyContinue | ConvertFrom-Json -EA SilentlyContinue  
   if($null -eq $config) {  
       throw "Error reading config file '$configFileName'"  
   }  
     
   #Add-PowerAppsAccount -TenantID $config.tenantId -CertificateThumbprint $config.certifThumb -ApplicationId $config.appId -Endpoint "tip1"   
   Add-PowerAppsAccount -TenantID $config.tenantId -ApplicationId $config.appId -Endpoint "tip1" -ClientSecret "msjfpjfkp<sjfpjsfpjspfpipq<piq<dfkq"  
     
     
     
     
   $data = Get-Content ".\$($config.dataFile)" -Encoding UTF8 -EA SilentlyContinue | ConvertFrom-Json -EA SilentlyContinue  
   if($null -eq $data) {  
       $data = [PSCustomObject]@{  
           lastExec = [datetime]::MinValue.ToString($jsonDateFormat)  
       }  
   }  
   $lastExec = Get-Date $data.lastExec  
   $now = Get-Date  
   $flowIds = @{}  
     
   function GetFlowRuns {  
       param(  
           [string] $FlowName,  
           [datetime] $Start,  
           [datetime] $End  
       )  
       $runs = @(Get-FlowRun -FlowName $FlowName | Where-Object {  
           $date = Get-Date $_.StartTime  
           return $date -ge $Start -and $date -lt $End  
       })  
   	#Write-Host $runs  
       $runCount = $runs.Count  
   	#Write-Host $runCount  
       $errors = @($runs | Where-Object { $_.Status -eq "Failed" })  
   	#Write-Host $errors  
     
       $errorsCount = $errors.Count  
   	#Write-Host $errorsCount  
     
       return [PSCustomObject]@{  
           runCount = $runCount  
           errorCount = $errorsCount  
           errors = $errors | ForEach-Object {  
               return [PSCustomObject]@{  
                   date = (Get-Date $_.StartTime).ToString($jsonDateFormat)  
                   message = $_.Internal.properties.error.message  
               }  
           }  
       }  
   }  
     
   function GetData {  
       param(  
           [datetime] $Start,  
           [datetime] $End  
       )  
       $config.flows | ForEach-Object {  
           $name = $_.name  
           $flowIds[$name] = $_.id  
           $flowData = GetFlowRuns -FlowName $_.id -Start $Start -End $End  
           if(!$data.$name) {  
               $defaultValue = [PSCustomObject]@{  
                   runCount = 0  
                   errorCount = 0  
                   errors = @()  
               }  
               $data | Add-Member -NotePropertyName $name -NotePropertyValue $defaultValue  
           }  
     
           $data.$name.runCount += $flowData.runCount  
           $data.$name.errorCount += $flowData.errorCount  
           $data.$name.errors += $flowData.errors  
       }  
         
       $data.lastExec = $End.ToString($jsonDateFormat)  
       $data | ConvertTo-Json -Depth 100 -Compress | Out-File -Encoding utf8 -FilePath $config.dataFile  
   }  
     
   function DailyAggregation {  
       param(  
           [datetime] $Date  
       )  
       $dateFormatted = [string]::Join(" ", @($Date.ToShortDateString(), $Date.ToLongTimeString()))  
       $errors = @()  
       $config.flows | ForEach-Object {  
           $name = $_.name  
           $outRow = $_.mailbox, $_.serviceAccount, $dateFormatted, $name, $flowIds[$name], $data.$name.runCount, $data.$name.errorCount  
           [string]::Join(" - ", $outRow) | Out-File -Encoding utf8 -FilePath $config.outputFile -Append  
           $data.$name.errors | ForEach-Object {  
               $errors += [PSCustomObject]@{  
                   flowName = $name  
                   date = Get-Date $_.date  
                   message = $_.message  
               }  
           }  
       }  
       $errors | Sort-Object -Property date | ForEach-Object {  
           $errorDate = [string]::Join(" ", @($_.date.ToShortDateString(), $_.date.ToLongTimeString()))  
           $outRow = $_.flowName, $errorDate, $_.message  
           [string]::Join(" - ", $outRow) | Out-File -Encoding utf8 -FilePath $config.errorsFile -Append  
       }  
       $data = [PSCustomObject]@{  
           lastExec = $Date.ToString($jsonDateFormat)  
       }  
       $data | ConvertTo-Json -Depth 100 -Compress | Out-File -Encoding utf8 -FilePath $config.dataFile  
   }  
     
   $triggerDate = Get-Date $config.triggerHour  
   if($lastExec -gt $triggerDate) {    
   	$triggerDate = $triggerDate.AddDays(1)   
     
   }  
     
   if($now -lt $triggerDate) {  
       GetData -Start $lastExec -End $now  
   }  
   else {  
       GetData -Start $lastExec -End $triggerDate  
   	DailyAggregation -Date $triggerDate  
   	GetData -Start $triggerDate -End $now  
   }  
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Limitless Technology 39,926 Reputation points
    2022-06-13T07:18:23.27+00:00

    Hi there,

    Can you see run data in the run history of Flows in the admin center?
    Have you replaced “environment” with your environment display name at the line Get-FlowEnvironment?

    You can also programmatically check the flow by running the simple below script and see if that throws any output.

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

    $pass = ConvertTo-SecureString "password" -AsPlainText -Force
    Add-PowerAppsAccount -Username "***********" -Password $pass

    foreach ($env in (Get-FlowEnvironment "environment")) {
    foreach ($flow in (Get-AdminFlow -EnvironmentName $env.EnvironmentName)) {
    foreach ($run in (Get-FlowRun -EnvironmentName $env.EnvironmentName -FlowName $flow.FlowName)) {
    #do stuff!
    }
    }
    }

    If you have done the both but still getting the error then submit a Microsoft Support Ticket at: https://admin.powerplatform.microsoft.com

    -----------------------------------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept it as an answer–


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.