Power Automate Desktop無償版をPowerShell (UI Automation)で自動実行させる方法 タスクスケジューラーなどで使うことができます

バージョンアップで画面が変わる場合があるので自分で修正できる方がお使いください

バッチファイル

フローの名前をパラメーターとして与えたい場合は4行目以降をパワーシェルのファイルとして保存して実行してください powershell Test.ps1 flowName

PowerShell -Command "& (Invoke-Expression -Command ('{' + (((Get-Content %~f0 )[4..199]) | Out-String )+ '}'))" %*
timeout /t 55 & goto:eof


# Power Automate for desktop script parameters
Param(
  [string]$flowName="n",
  [bool]$flgExit = $true
#  [bool]$flgExit = $false
)
# Import necessary assemblies
Add-Type -AssemblyName "UIAutomationTypes"
Add-Type -AssemblyName "UIAutomationClient"

# Define properties as variables
$UIAElement = [System.Windows.Automation.AutomationElement]
$UIAPropertyCondition = [System.Windows.Automation.PropertyCondition]
$UIAAndCondition = [System.Windows.Automation.AndCondition]
$UIATreeScope = [System.Windows.Automation.TreeScope]
$UIAInvokePattern = [System.Windows.Automation.InvokePattern]::Pattern
$UIAWindowPattern = [System.Windows.Automation.WindowPattern]::Pattern
$UIAItemContainerPattern = [System.Windows.Automation.ItemContainerPattern]::Pattern
$UIAScrollItemPattern = [System.Windows.Automation.ScrollItemPattern]::Pattern
$UIASelectionItemPattern = [System.Windows.Automation.SelectionItemPattern]::Pattern
$UIARootElement = $UIAElement::RootElement


# Launch Power Automate for desktop
Start-Process -FilePath "ms-powerautomate://"
Start-Sleep -s 2

# Retrieve Power Automate for desktop window
$cndPadWindowId = New-Object $UIAPropertyCondition($UIAElement::AutomationIdProperty, "ConsoleMainWindow")
$cndPadWindowClassName = New-Object $UIAPropertyCondition($UIAElement::ClassNameProperty, "WinAutomationWindow")
$cndPadWindow = New-Object $UIAAndCondition($cndPadWindowId, $cndPadWindowClassName)
$elmPadWindow = $UIARootElement.FindFirst($UIATreeScope::Children, $cndPadWindow)

if ($elmPadWindow -eq $null) {
    Write-Host "Unable to retrieve powerautomate's main window."
    exit 1
}

# Retrieve the tab
$cndTab = New-Object $UIAPropertyCondition($UIAElement::AutomationIdProperty, "ProcessesTabControl")
$elmTab = $elmPadWindow.FindFirst($UIATreeScope::Subtree, $cndTab)

# Retrieve and select tab item
if($elmTab -ne $null){
  $cndTabItem = New-Object $UIAPropertyCondition($UIAElement::AutomationIdProperty, "FlowsTab")
  $elmTabItem = $elmTab.FindFirst($UIATreeScope::Children, $cndTabItem)
  if($elmTabItem -ne $null){
    $elmTabItem.GetCurrentPattern($UIASelectionItemPattern).Select()
  }
}

# Retrieve the data grid
if($elmPadWindow -ne $null){
  $cndDataGrid = New-Object $UIAPropertyCondition($UIAElement::AutomationIdProperty, "MyFlowsListGrid")
  $elmDataGrid = $elmPadWindow.FindFirst($UIATreeScope::Subtree, $cndDataGrid)
}

# Retrieve and select the data item
if($elmDataGrid -ne $null){
  $icDataGrid = $elmDataGrid.GetCurrentPattern($UIAItemContainerPattern)
  $elmDataItem = $icDataGrid.FindItemByProperty($null, $UIAElement::NameProperty, $flowName)
  if($elmDataItem -ne $null){
    $elmDataItem.GetCurrentPattern($UIAScrollItemPattern).ScrollIntoView()
    $elmDataItem.GetCurrentPattern($UIASelectionItemPattern).Select()
  }
}

# Retrieve and press the Start button
if($elmDataItem -ne $null){
  $cndStartButton = New-Object $UIAPropertyCondition($UIAElement::AutomationIdProperty, "StartFlowButton")
  $elmStartButton = $elmDataItem.FindFirst($UIATreeScope::Subtree, $cndStartButton)
  if($elmStartButton -ne $null){
    $elmStartButton.GetCurrentPattern($UIAInvokePattern).Invoke()
  }
}
if ($flgExit) {
    # Wait
    while ($elmStartButton -ne $null -and !$elmStartButton.GetCurrentPropertyValue($UIAElement::IsEnabledProperty)) {
        Start-Sleep -Milliseconds 800
    }

    # Close Power Automate for desktop
    $elmPadWindow.GetCurrentPattern($UIAWindowPattern).Close()
}

参考

[Power Automate Desktop]名前を指定してフローを実行するPowerShellスクリプト | 初心者備忘録

こちらを参考に自分用に作り直しました

説明

スクリプトの全体構成

このスクリプトは、Power Automate for desktopを起動し、特定のフローを選択し、開始ボタンを押してフローを実行するためのものです。また、オプションでPower Automate for desktopを終了することができます。

コマンドライン部分

PowerShell -Command "& (Invoke-Expression -Command ('{' + (((Get-Content %~f0 )[4..199]) | Out-String )+ '}'))" %*
timeout /t 55 & goto:eof
  • PowerShell -Command "..." は、PowerShellスクリプトを実行するためのコマンドです。
  • Invoke-Expression を使って、指定したスクリプトを評価し、実行します。
  • Get-Content %~f0 は、スクリプトファイルの内容を取得するコマンドです。[4..199] は、5行目から200行目までを取得する部分です。
  • timeout /t 55 は、55秒間待機するコマンドです。
  • goto:eof は、スクリプトの終わりにジャンプするコマンドです。

パラメータの定義

Param(
  [string]$flowName="n",
  [bool]$flgExit = $true
#  [bool]$flgExit = $false
)
  • Param ブロックは、スクリプトに渡されるパラメータを定義します。
  • $flowName は実行するフローの名前を指定するパラメータです。
  • $flgExit は、スクリプト終了後にPower Automate for desktopを終了するかどうかを決定するブール値のパラメータです。

アセンブリのインポート

Add-Type -AssemblyName "UIAutomationTypes"
Add-Type -AssemblyName "UIAutomationClient"
  • 必要なUI Automationのアセンブリをインポートしています。これにより、UIの操作が可能になります。

プロパティと変数の定義

$UIAElement = [System.Windows.Automation.AutomationElement]
$UIAPropertyCondition = [System.Windows.Automation.PropertyCondition]
$UIAAndCondition = [System.Windows.Automation.AndCondition]
$UIATreeScope = [System.Windows.Automation.TreeScope]
$UIAInvokePattern = [System.Windows.Automation.InvokePattern]::Pattern
$UIAWindowPattern = [System.Windows.Automation.WindowPattern]::Pattern
$UIAItemContainerPattern = [System.Windows.Automation.ItemContainerPattern]::Pattern
$UIAScrollItemPattern = [System.Windows.Automation.ScrollItemPattern]::Pattern
$UIASelectionItemPattern = [System.Windows.Automation.SelectionItemPattern]::Pattern
$UIARootElement = $UIAElement::RootElement
  • UI Automationで使用するプロパティと変数を定義しています。

Power Automate for desktopの起動

Start-Process -FilePath "ms-powerautomate://"
Start-Sleep -s 2
  • Start-Process コマンドでPower Automate for desktopを起動し、2秒間待機します。

Power Automate for desktopウィンドウの取得

$cndPadWindowId = New-Object $UIAPropertyCondition($UIAElement::AutomationIdProperty, "ConsoleMainWindow")
$cndPadWindowClassName = New-Object $UIAPropertyCondition($UIAElement::ClassNameProperty, "WinAutomationWindow")
$cndPadWindow = New-Object $UIAAndCondition($cndPadWindowId, $cndPadWindowClassName)
$elmPadWindow = $UIARootElement.FindFirst($UIATreeScope::Children, $cndPadWindow)

if ($elmPadWindow -eq $null) {
    Write-Host "Unable to retrieve powerautomate's main window."
    exit 1
}
  • Power Automate for desktopのメインウィンドウを取得します。取得できない場合は、エラーメッセージを表示してスクリプトを終了します。

タブの取得と選択

$cndTab = New-Object $UIAPropertyCondition($UIAElement::AutomationIdProperty, "ProcessesTabControl")
$elmTab = $elmPadWindow.FindFirst($UIATreeScope::Subtree, $cndTab)

if($elmTab -ne $null){
  $cndTabItem = New-Object $UIAPropertyCondition($UIAElement::AutomationIdProperty, "FlowsTab")
  $elmTabItem = $elmTab.FindFirst($UIATreeScope::Children, $cndTabItem)
  if($elmTabItem -ne $null){
    $elmTabItem.GetCurrentPattern($UIASelectionItemPattern).Select()
  }
}
  • タブコントロールとフロースタブを取得し、選択します。

データグリッドの取得とデータアイテムの選択

if($elmPadWindow -ne $null){
  $cndDataGrid = New-Object $UIAPropertyCondition($UIAElement::AutomationIdProperty, "MyFlowsListGrid")
  $elmDataGrid = $elmPadWindow.FindFirst($UIATreeScope::Subtree, $cndDataGrid)
}

if($elmDataGrid -ne $null){
  $icDataGrid = $elmDataGrid.GetCurrentPattern($UIAItemContainerPattern)
  $elmDataItem = $icDataGrid.FindItemByProperty($null, $UIAElement::NameProperty, $flowName)
  if($elmDataItem -ne $null){
    $elmDataItem.GetCurrentPattern($UIAScrollItemPattern).ScrollIntoView()
    $elmDataItem.GetCurrentPattern($UIASelectionItemPattern).Select()
  }
}
  • データグリッドを取得し、指定されたフロー名のアイテムを選択します。

スタートボタンの取得と実行

if($elmDataItem -ne $null){
  $cndStartButton = New-Object $UIAPropertyCondition($UIAElement::AutomationIdProperty, "StartFlowButton")
  $elmStartButton = $elmDataItem.FindFirst($UIATreeScope::Subtree, $cndStartButton)
  if($elmStartButton -ne $null){
    $elmStartButton.GetCurrentPattern($UIAInvokePattern).Invoke()
  }
}
  • スタートボタンを取得し、そのボタンを押してフローを開始します。

終了フラグの確認とPower Automate for desktopの終了

if ($flgExit) {
    # Wait
    while ($elmStartButton -ne $null -and !$elmStartButton.GetCurrentPropertyValue($UIAElement::IsEnabledProperty)) {
        Start-Sleep -Milliseconds 800
    }

    # Close Power Automate for desktop
    $elmPadWindow.GetCurrentPattern($UIAWindowPattern).Close()
}
  • $flgExit フラグがtrueの場合、スタートボタンが無効になるまで待機し、その後Power Automate for desktopを終了します。