param ( [Parameter(Mandatory = $true)] [string]$WorkflowId ) $TimeoutSec = 300 $startTime = Get-Date $logRoot = Join-Path $env:LOCALAPPDATA "Microsoft\Power Automate Desktop\Console\Scripts" # ワークフローの開始 Start-Process "ms-powerautomate:/console/flow/run?workflowid=$WorkflowId" # 5分以内に作成されたこのワークフローのRunDefinition.jsonを探す # $logRoot\{WorkflowID}$Ruuns\{RunID}\RunDefinition.json $deadline = (Get-Date).AddSeconds($TimeoutSec) $runDefinition = $null while ((Get-Date) -lt $deadline) { $runDefinition = Get-ChildItem $logRoot -Recurse -Filter "RunDefinition.json" -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -ge $startTime -and ((Get-Content $_.FullName -Raw | ConvertFrom-Json).workflow.workflowId -eq $WorkflowId) } | Sort-Object LastWriteTime -Descending | Select-Object -First 1 if ($runDefinition) { break } Start-Sleep -Seconds 1 } if (-not $runDefinition) { Write-Error "RunDefinition.json not found. Is WorkflowId: $($WorkflowId) correct? " exit 9 } # 実行完了まで待機 do { Start-Sleep -Seconds 1 $json = Get-Content $runDefinition.FullName -Raw | ConvertFrom-Json $status = $json.status } while ($status -in @("Running", "NotStarted", "Queued")) $status switch ($status) { "Succeeded" { exit 0 } "Failed" { exit 1 } "Canceled" { exit 2 } default { exit 9 } }