question

LouiseSweeney-9869 avatar image
0 Votes"
LouiseSweeney-9869 asked RichMatheisen-8856 answered

Retrieve a power automate flow with powershell

Hello, I would like to make a PowerShell script where I use the "GetFlow" function to retrieve a Power Automate flow. But unfortunately when I run my powershell script nothing happens.
All that passes in the "GetFlowRuns" function are empty character strings...
Could you help me please ? ‌‌:( Thanks in advance !


Here is my config file:

  {
     "tenantId" : "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", 
     "certifThumb" : "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
     "appId" : "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz",
     "triggerHour" : "01:00",
     "outputFile" : ".\\flow-stats.txt",
     "errorsFile" : ".\\flow-errors.txt",
     "dataFile" : ".\\flow-data.json",
     "flows" : [
         {
             "name": "Flux note de frais",
             "mailbox": "gerge@fs.fr",
             "serviceAccount" : "PS_FLOW", 
 "id": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
         },
   {
             "name": "nombre annee",
             "mailbox": "ehrgtz@egz.fr",
             "serviceAccount" : "PS_FLOW", 
 "id": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
         },
 {
             "name": "Flux Occupation des locaux",
             "mailbox": "regrg@ef.fr",
             "serviceAccount" : "PS_FLOW", 
 "id": "aaaaaaaaaaaaaaaaaaaaaaaaaa"
         },
 {
             "name": "pomodori",
             "mailbox": "gsgqg@gsgdqg.fr",
             "serviceAccount" : "PS_FLOW", 
 "id": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
         }
     ]
 } 

(Obviously I changed the confidential values to send the script here)



Here is my powershell script:

 $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 "fg465gsqg65g4gffdf4fd.cp9"
    
    
    
    
 $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-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

Looking at you code, specifically lines 42 - 45, can you tell me what you expect this to do?

 $runs = @(Get-FlowRun -FlowName $FlowName | Where-Object {
          $date = Get-Date $_.StartTime
          return $date -ge $Start -and $date -lt $End
      })

Without actually running the code it looks to me like you'll exit the function GetFlowRuns at line 44 and return either a $True of $False value.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.