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
}