LoginSignup
9
12

More than 5 years have passed since last update.

生のUI Automationの実行

Posted at

生の UI Automation が使えるのか気になったので試してみた1
下記コードを手元のPowerShell ISEで実行したところ、PowerSehllのコンソールが立ち上がった。

# UI Automation 系の dll のロード
Add-Type -Path "C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\UIAutomationClient.dll"
Add-Type -Path "C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\UIAutomationTypes.dll"

$UIAElement       = [System.Windows.Automation.AutomationElement]
$UIAChildrenScope = [System.Windows.Automation.TreeScope]::Children
$UIATrue          = [System.Windows.Automation.Condition]::TrueCondition
$UIAInvokePattern = [System.Windows.Automation.InvokePattern]::Pattern

# PowerShell ISE のプロセスを一つ取得
$targetProcess =
    Get-Process powershell_ise |
    Select-Object -First 1

# プロセスからメインウィンドウの AutomationElement を取得
$mainWindow = $UIAElement::FromHandle($targetProcess.MainWindowHandle)

# ツールバーを取得
$toolBar =
    $mainWindow.FindAll($UIAChildrenScope, $UIATrue) |
    Where-Object {$_.Current.Name -eq "ツール バー"}

# ツールバーから「PowerShell.exe を起動」ボタンを取得
$startPowerShell =
    $toolBar.FindAll($UIAChildrenScope, $UIATrue) |
    Where-Object {$_.Current.Name -eq "PowerShell.exe を起動(_H)"}

# ボタンの実行
$startPowerShell.GetCurrentPattern($UIAInvokePattern).Invoke()

コードを書けば!ボタンが!押せる!!!


  1. UI Automation PowerShell Extensions に対して「生」と言っている。 

9
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
12