生の UI Automation が使えるのか気になったので試してみた1。
下記コードを手元のPowerShell ISEで実行したところ、PowerSehllのコンソールが立ち上がった。
.ps1
# 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()
コードを書けば!ボタンが!押せる!!!
-
UI Automation PowerShell Extensions に対して「生」と言っている。 ↩