ここ数日、話題のPower Automate Desktop(PAD)を触っています。
多数のアクションが用意されていて、コードを書くことなく様々な処理が実行できるので非常に面白いサービスです。
ただ、何かをトリガーにしてフローを実行したいときは、現状ですとPower Automate(Desktopではない)のDesktop flowsコネクタを使用する必要があるため、手間が掛かると言いますか、個人的には、“ショートカットをダブルクリックしたらフロー実行”くらいにお手軽に実行したかったので、フロー名を指定してPADのフローを実行する簡単なスクリプトを書いてみました。
スクリプトの第一引数でフロー名を、第二引数でフロー実行後にPADを終了するかどうかを指定します。
1 | PowerShell -NoProfile -ExecutionPolicy Unrestricted .\StartPADFlow.ps1 "テスト" $false |
| # Power Automate for desktopのフローを実行するPowerShellスクリプト | |
| # @param [string] $flowName 実行するフロー名 | |
| # @param [bool] $flgExit PAD終了フラグ | |
| # | |
| # 動作確認:バージョン 2.52.00293.25009 (ストアアプリ版) | |
| # | |
| Param( | |
| [parameter(mandatory=$true)][string]$flowName, | |
| [bool]$flgExit = $false | |
| ) | |
| #Power Automate for desktop起動 | |
| Start-Process -FilePath "ms-powerautomate://" | |
| #UI Automation | |
| Add-Type -AssemblyName "UIAutomationClient" | |
| Add-Type -AssemblyName "UIAutomationTypes" | |
| $uiAuto = [System.Windows.Automation.AutomationElement] | |
| $pcdn = [System.Windows.Automation.PropertyCondition] | |
| $acdn = [System.Windows.Automation.AndCondition] | |
| $tree = [System.Windows.Automation.TreeScope] | |
| $iptn = [System.Windows.Automation.InvokePattern]::Pattern | |
| $wptn = [System.Windows.Automation.WindowPattern]::Pattern | |
| $icptn = [System.Windows.Automation.ItemContainerPattern]::Pattern | |
| $siptn = [System.Windows.Automation.ScrollItemPattern]::Pattern | |
| $selptn = [System.Windows.Automation.SelectionItemPattern]::Pattern | |
| $root = $uiAuto::RootElement | |
| #Power Automate for desktopウィンドウ取得 | |
| $cndPadWindowId = New-Object $pcdn($uiAuto::AutomationIdProperty, "ConsoleMainWindow") | |
| $cndPadWindowClassName = New-Object $pcdn($uiAuto::ClassNameProperty, "WinAutomationWindow") | |
| $cndPadWindow = New-Object $acdn($cndPadWindowId, $cndPadWindowClassName) | |
| do{ | |
| Start-Sleep -m 200 | |
| $elmPadWindow = $root.FindFirst($tree::Children, $cndPadWindow) | |
| }while($elmPadWindow -eq $null) | |
| #タブ取得 | |
| $cndTab = New-Object $pcdn($uiAuto::AutomationIdProperty, "ProcessesTabControl") | |
| $elmTab = $elmPadWindow.FindFirst($tree::Subtree, $cndTab) | |
| #タブ項目取得・選択 | |
| if($elmTab -ne $null){ | |
| $cndTabItem = New-Object $pcdn($uiAuto::AutomationIdProperty, "FlowsTab") | |
| $elmTabItem = $elmTab.FindFirst($tree::Children, $cndTabItem) | |
| if($elmTabItem -ne $null){ | |
| $selTabItem = $elmTabItem.GetCurrentPattern($selptn) | |
| $selTabItem.Select() | |
| } | |
| } | |
| #データグリッド取得 | |
| if($elmPadWindow -ne $null){ | |
| $cndDataGrid = New-Object $pcdn($uiAuto::AutomationIdProperty, "MyFlowsListGrid") | |
| $elmDataGrid = $elmPadWindow.FindFirst($tree::Subtree, $cndDataGrid) | |
| } | |
| #データ項目取得・選択 | |
| if($elmDataGrid -ne $null){ | |
| $icDataGrid = $elmDataGrid.GetCurrentPattern($icptn) | |
| $elmDataItem = $icDataGrid.FindItemByProperty($null, $uiAuto::NameProperty, $flowName) | |
| if($elmDataItem -ne $null){ | |
| $siDataItem = $elmDataItem.GetCurrentPattern($siptn) | |
| $siDataItem.ScrollIntoView() | |
| $selDataItem = $elmDataItem.GetCurrentPattern($selptn) | |
| $selDataItem.Select() | |
| } | |
| } | |
| #実行ボタン取得・押下 | |
| if($elmDataItem -ne $null){ | |
| $cndStartButton = New-Object $pcdn($uiAuto::AutomationIdProperty, "StartFlowButton") | |
| $elmStartButton = $elmDataItem.FindFirst($tree::Subtree, $cndStartButton) | |
| if($elmStartButton -ne $null){ | |
| $ivkStartButton = $elmStartButton.GetCurrentPattern($iptn) | |
| $ivkStartButton.Invoke() | |
| } | |
| } | |
| if($flgExit){ | |
| #フロー終了待ち | |
| if($elmStartButton -ne $null){ | |
| do{ | |
| Start-Sleep -m 800 | |
| }while($elmStartButton.GetCurrentPropertyValue($uiAuto::IsEnabledProperty) -eq $false) | |
| } | |
| #Power Automate for desktop終了 | |
| $winPadWindow = $elmPadWindow.GetCurrentPattern($wptn) | |
| $winPadWindow.Close() | |
| } |
仕組みは単純で、Start-ProcessでPADを実行した後、UI Automationで画面操作を行っているだけです(動作イメージは下記ツイート参照)。
下記のようなバッチファイルを用意しておけば、わざわざコマンドを叩かなくても簡単に実行できますし、タスクスケジューラで時間を指定して実行することもできるでしょう。
1 2 3 4 | @echo offcd /d %~dp0PowerShell -NoProfile -ExecutionPolicy Unrestricted .\StartPADFlow.ps1 "テスト"pause |
とりあえずこれで目的が達成できたわけですが、このスクリプトを実際の運用で使用するのは、私としてはお薦めできません。
というのも、上で書いた通り本スクリプトはPADのUIに依存する仕組みとなっているため、アップデートで画面の構造が変わればすぐに使えなくなってしまいます。
実務で何かをトリガーにしてPADのフローを実行したいときは、やはりDesktop flowsコネクタを使用した方が良いだろうと思います。
本スクリプトを使用する際は、自己責任でよろしくお願いいたします。
関連記事
Outlookマクロを使って、メール受信をトリガーにしてフローを実行する仕組みも考えてみました。
こんにちは。
こちらの記事を参考にPower Automate Desktopとタスクスケジューラでフローを自動化に成功し大変助かっていたのですが
最新バージョン2.44.46.24141へのアップグレードでフローが実行されなくなってしまいました。
前のバージョンのインストーラーも探しているのですがなかなか見つからず…(何も考えずにアップグレードしてしまった自分の責任なのですが…)
もし簡単な修正で動くようになるのであればご教授いただければと思い不躾ですがコメントさせていただきました。
失礼いたしました。
> Leo 様
ブログ管理者のきぬあさです。
コメントいただいた件につきまして、Windows 11 24H2、Windows 10 22H2 + Power Automate for desktop 2.44.46.24141 環境でテストしてみましたが、私の環境ではどちらも問題なく動作しました。
PowerShellスクリプトのどの部分で引っ掛かったのか確認ができないため、大変申し訳ないのですが私の方では対応が難しいです。
記事中にも記載しておりますが、本記事で紹介しているスクリプトはUI依存で確実に動作する保証がありませんので、下記記事でも紹介しているショートカットからのフロー実行をお薦めいたします(※有償ライセンスが必要となります)。
・[Power Automate for desktop]URL経由でデスクトップフローを実行する
https://www.ka-net.org/blog/?p=14696
>>きぬあさ 様
返信ありがとうございます。
PADからのログアウトとPCの再起動をしたところ問題なくフローが実行されました。
テストまでしていただいて本当に申し訳ないです大変失礼いたしました…。