# Retrieve the executions of the flow power automate

**URL:** <https://forums.powershell.org/t/retrieve-the-executions-of-the-flow-power-automate/19542>\
**Category:** PowerShell Help\
**Created:** [June 9, 2022, 1:04pm UTC](https://forums.powershell.org/t/retrieve-the-executions-of-the-flow-power-automate/19542 "2022-06-09T13:04:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anya](https://avatars.discourse-cdn.com/v4/letter/a/ebca7d/32.png) [@Anya](https://forums.powershell.org/u/Anya)\
**Post date:** [June 9, 2022, 1:04pm UTC](https://forums.powershell.org/t/retrieve-the-executions-of-the-flow-power-automate/19542/1 "2022-06-09T13:04:08Z")

</div>

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:

```auto
efffqsf@dsqdqsd.onmicrosoft.com - PS_FLOW - 09/06/2022 01:00:00 - Flux note de frais - aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaa - 0 - 0
efffqsf@dsqdqsd.onmicrosoft.com - PS_FLOW - 09/06/2022 01:00:00 - nombre annee - aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaa - 0 - 0
efffqsf@dsqdqsd.onmicrosoft.com - PS_FLOW - 09/06/2022 01:00:00 - Flux Occupation des locaux - aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaa - 0 - 0
efffqsf@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!

```auto
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": "dsfdfqf@sqds.onmicrosoft.com",
            "serviceAccount" : "PS_FLOW", 
			"id": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaa"
        },
		  {
            "name": "nombre annee",
            "mailbox": "dsfdfqf@sqds.onmicrosoft.com",
            "serviceAccount" : "PS_FLOW", 
			"id": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaa"
        },
		{
            "name": "Flux Occupation des locaux",
            "mailbox": "dsfdfqf@sqds.onmicrosoft.com",
            "serviceAccount" : "PS_FLOW", 
			"id": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaa"
        },
		{
            "name": "pomodori",
            "mailbox": "dsfdfqf@sqds.onmicrosoft.com",
            "serviceAccount" : "PS_FLOW", 
			"id": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaa"
        }
    ]
}

```

```auto
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
}

```

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [June 9, 2022, 8:51pm UTC](https://forums.powershell.org/t/retrieve-the-executions-of-the-flow-power-automate/19542/2 "2022-06-09T20:51:45Z")

</div>

Your `GetFlowRuns` function is not returning the `[PSCustomObject]` you’re defining because of the `return` keyword on line 9 of that function. This will cause the function to exit at that point.

```auto
function returnExample {
    return (Get-Date)
    return [PSCustomObject] @{
        YesterDay = (Get-Date).AddDays(-1)
    }
}

returnExample

```

---

<div class="post-metadata">

**Author:** ![Anya](https://avatars.discourse-cdn.com/v4/letter/a/ebca7d/32.png) [@Anya](https://forums.powershell.org/u/Anya)\
**Post date:** [June 10, 2022, 9:25am UTC](https://forums.powershell.org/t/retrieve-the-executions-of-the-flow-power-automate/19542/3 "2022-06-10T09:25:44Z")

</div>

Thank you for your reply ! So I have to remove the return keyword? :o @matt-bloomfield

---

<div class="post-metadata">

**Author:** ![Anya](https://avatars.discourse-cdn.com/v4/letter/a/ebca7d/32.png) [@Anya](https://forums.powershell.org/u/Anya)\
**Post date:** [June 10, 2022, 9:42am UTC](https://forums.powershell.org/t/retrieve-the-executions-of-the-flow-power-automate/19542/4 "2022-06-10T09:42:34Z")

</div>

Can you show me how you put this function in my code please? :o @matt-bloomfield

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:17pm UTC](https://forums.powershell.org/t/retrieve-the-executions-of-the-flow-power-automate/19542/5 "2024-05-16T20:17:49Z")

</div>

Closed on May 16, 2024, 8:17pm UTC


