Help us understand the problem. What is going on with this article?

生のUI Automationの実行

More than 3 years have passed since last update.

生の 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 に対して「生」と言っている。 

yumura_s
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away