~UI Automationで画面情報取得!その5~ UI Automation with PowerShell 奮戦記 15日目
UI Automation*1で遊ぼう!
UI Automation の画面情報の取得機能を使用したWindows設定書生成の自動化に挑戦しています。Windowsの標準実装のみ、PowerShellのみで動作させることを目指しています。
UI Automationでの画面操作の捕捉をPowerShellで出来るようにEventタイプを.Net Framworkで実装しました。
実装した開発ライブラリ、共通で使用するスクリプトについては以下を確認してくださいね。
https://amon52280sub.hateblo.jp/entry/2019/01/23/032420
画面情報を取得してみましょう。
制御値を追加しました。 Windows設定値生成、Windows自動設定に使用することになります。
PowerShell Version3(管理者権限)で実行してみる。
[CmdletBinding()] Param ( [int]$global:delay = 500 ) # UI Automation 共通ファンクションを組み込む . .\UIA.ps1 # 初期化 $global:uia = UIA_Init $global:Window = @{} # Timer イベント捕捉 $global:timer = New-Object System.Timers.Timer $timer.Interval = 100 $timer.Enabled = $false Register-ObjectEvent -InputObject $timer -EventName Elapsed -Action { try { $timer.Enabled = $false $name = $ev[0].current.name $controltype = $ev[0].current.localizedcontroltype $classname = $ev[0].current.classname $eventid = $ev[1].EventId.ProgrammaticName if ($stop) {return} if ($name -match "powershell") {return} # ウインドウ or ダイアログ $p = UIA_GetParentElement $ev[0] Write-Host $($p | Out-String) Write-Host ($p -ne $null) if ($p -ne $null) { if ($p.current.name -eq "") {return} if ($p.current.name -match "powershell") {return} # ツリー構造で表示 設定値 制御値 表示 $tree = UIA_GetElementTreeWithOpt $p Write-Host $($tree | UIA_OutPut -mode ElementWithOpt -action -trigger -key | ft -wrap | Out-String) #Write-Host $($tree | UIA_OutPut -mode ElementWithOpt -action -trigger -key | Out-String) $Window[$p.current.name] = $tree } Write-Host $(UIA_OutPut -element $ev[0] -event $ev[1] -mode Event | Out-String) Write-Host $($ev[0].GetSupportedPatterns() | Out-String) $timer.Enabled = $false } catch { Write-Host $_ } } | Out-Null # UI Automation イベント捕捉 Register-ObjectEvent -InputObject $uia -EventName UIChange -Action { try { #$timer.Enabled = $false if ($args -eq $null) {return} if ($args[0] -eq $null) {return} if ($args -eq $ev) {return} $global:ev = $args $name = $ev[0].current.name $controltype = $ev[0].current.localizedcontroltype $classname = $ev[0].current.classname $eventid = $ev[1].EventId.ProgrammaticName if ($name -match "powershell") {return} switch ($eventid) { "AutomationElementIdentifiers.AutomationFocusChangedEvent" { #$uia.SetEvent($ev[0]) $timer.Interval = $delay $timer.Enabled = $true } "AutomationElementIdentifiers.AutomationPropertyChangedEvent" { $timer.Interval = $delay $timer.Enabled = $false } "WindowPatternIdentifiers.WindowOpenedProperty"{ # ツリー構造で表示 設定値表示 #Write-Host $(UIA_GetElementTreeWithOpt $ev[0] | UIA_OutPut -mode ElementWithOpt | Out-String) #Write-Host $(UIA_OutPut -element $ev[0] -event $ev[1] -mode Event | Out-String) $timer.Interval = $delay $timer.Enabled = $true } "WindowPatternIdentifiers.WindowClosedProperty"{ $timer.Interval = $delay $timer.Enabled = $false } default { Write-Host $(UIA_OutPut -element $ev[0] -event $ev[1] -mode Event | Out-String) $timer.Interval = $delay $timer.Enabled = $false } } } catch { Write-Host $_ } } | Out-Null # イベントスタート $uia.StartEvent() "画面情報取得スタート!!"
実行結果
PS D:\開発\15DAY> UIA_GetParentElement LocalizedControlType ClassName Name -------------------- --------- ---- タブ項目 コンピューター名 UIA_GetParentElement LocalizedControlType ClassName Name -------------------- --------- ---- タブ SysTabControl32 UIA_GetParentElement LocalizedControlType ClassName Name -------------------- --------- ---- ダイアログ #32770 システムのプロパティ Cached Current CachedParent CachedChildren ------ ------- ------------ -------------- System.Windows.Automation.AutomationElement+AutomationElementInformation System.Windows.Automation.AutomationElement+AutomationElementInformation True UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt _システムのプロパティ UIA_GetElementTreeWithOpt _システムのプロパティ UIA_GetElementTreeWithOpt _システムのプロパティ UIA_GetElementTreeWithOpt _システムのプロパティ UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt _システムのプロパティ_コンピューター名 UIA_GetElementTreeWithOpt _システムのプロパティ_コンピューター名 UIA_GetElementTreeWithOpt _システムのプロパティ_コンピューター名 UIA_GetElementTreeWithOpt _システムのプロパティ_コンピューター名 UIA_GetElementTreeWithOpt _システムのプロパティ_コンピューター名 UIA_GetElementTreeWithOpt _システムのプロパティ_コンピューター名 UIA_GetElementTreeWithOpt _システムのプロパティ_コンピューター名 UIA_GetElementTreeWithOpt _システムのプロパティ_コンピューター名 UIA_GetElementTreeWithOpt _システムのプロパティ_コンピューター名 UIA_GetElementTreeWithOpt _システムのプロパティ_コンピューター名 UIA_GetElementTreeWithOpt _システムのプロパティ_コンピューター名 UIA_GetElementTreeWithOpt _システムのプロパティ_コンピューター名 UIA_GetElementTreeWithOpt _システムのプロパティ_コンピューター名 UIA_GetElementTreeWithOpt _システムのプロパティ_コンピューター名 UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt _システムのプロパティ UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt ControlType ClassName Value action trigger Name key ----------- --------- ----- ------ ------- ---- --- ダイアログ #32770 システムのプロパティ _システムのプロパティ ボタン Button OK _システムのプロパティ_OK ボタン Button キャンセル _システムのプロパティ_キャンセル ボタン Button 適用(A) _システムのプロパティ_適用(A) タブ SysTabControl32 タブ項目 コンピューター名 _システムのプロパティ_コンピューター名 テキスト Static コンピューターの説明(D): 編集 Edit コンピューターの説明(D): _システムのプロパティ_コンピューター名_コンピューターの説明(D): テキスト Static 例: "キッチンのコンピューター"、"仕事用コンピューター" テキスト Static フル コンピューター名: 編集 Edit DESKTOP-CSQKB17 フル コンピューター名: _システムのプロパティ_コンピューター名_フル コンピューター名: テキスト Static ワークグループ: 編集 Edit WORKGROUP ワークグループ: _システムのプロパティ_コンピューター名_ワークグループ: テキスト Static ドメインまたはワークグループに参加するためのウィザードを使用するには [ネットワーク ID] をクリックしてください。 ボタン Button ネットワーク ID(N)... _システムのプロパティ_コンピューター名_ネットワーク ID(N)... テキスト Static このコンピューターの名前を変更するには、[変更] をクリックしてください。 ボタン Button 変更(C)... _システムのプロパティ_コンピューター名_変更(C)... テキスト Static イメージ Static テキスト Static 次の情報は、このコンピューターをネットワーク上で識別するために使われます。 タブ項目 ハードウェア _システムのプロパティ_ハードウェア タブ項目 詳細設定 _システムのプロパティ_詳細設定 タブ項目 システムの保護 _システムのプロパティ_システムの保護 タブ項目 リモート _システムのプロパティ_リモート タイトル バー システムのプロパティ メニュー バー システム メニュー バー メニュー項目 システム ボタン 閉じる _システムのプロパティ_閉じる ControlType : 編集 ClassName : Edit EventID : AutomationElementIdentifiers.AutomationFocusChangedEvent Property : Name : コンピューターの説明(D): Id ProgrammaticName -- ---------------- 10002 ValuePatternIdentifiers.Pattern 10014 TextPatternIdentifiers.Pattern PS D:\開発\15DAY> UIA_GetParentElement LocalizedControlType ClassName Name -------------------- --------- ---- タブ項目 詳細設定 UIA_GetParentElement LocalizedControlType ClassName Name -------------------- --------- ---- タブ SysTabControl32 UIA_GetParentElement LocalizedControlType ClassName Name -------------------- --------- ---- ダイアログ #32770 システムのプロパティ Cached Current CachedParent CachedChildren ------ ------- ------------ -------------- System.Windows.Automation.AutomationElement+AutomationElementInformation System.Windows.Automation.AutomationElement+AutomationElementInformation True UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt _システムのプロパティ UIA_GetElementTreeWithOpt _システムのプロパティ UIA_GetElementTreeWithOpt _システムのプロパティ UIA_GetElementTreeWithOpt _システムのプロパティ UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt _システムのプロパティ_詳細設定 UIA_GetElementTreeWithOpt _システムのプロパティ_詳細設定 UIA_GetElementTreeWithOpt _システムのプロパティ_詳細設定 UIA_GetElementTreeWithOpt _システムのプロパティ_詳細設定 UIA_GetElementTreeWithOpt _システムのプロパティ_詳細設定 UIA_GetElementTreeWithOpt _システムのプロパティ_詳細設定 UIA_GetElementTreeWithOpt _システムのプロパティ_詳細設定 UIA_GetElementTreeWithOpt _システムのプロパティ_詳細設定 UIA_GetElementTreeWithOpt _システムのプロパティ_詳細設定 UIA_GetElementTreeWithOpt _システムのプロパティ_詳細設定 UIA_GetElementTreeWithOpt _システムのプロパティ_詳細設定 UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt _システムのプロパティ UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt ControlType ClassName Value action trigger Name key ----------- --------- ----- ------ ------- ---- --- ダイアログ #32770 システムのプロパティ _システムのプロパティ ボタン Button OK _システムのプロパティ_OK ボタン Button キャンセル _システムのプロパティ_キャンセル ボタン Button 適用(A) _システムのプロパティ_適用(A) タブ SysTabControl32 タブ項目 コンピューター名 _システムのプロパティ_コンピューター名 タブ項目 ハードウェア _システムのプロパティ_ハードウェア タブ項目 詳細設定 _システムのプロパティ_詳細設定 テキスト Static Administrator としてログオンしない場合は、これらのほとんどは変更できません。 グループ Button パフォーマンス テキスト Static 視覚効果、プロセッサのスケジュール、メモリ使用、および仮想メモリ ボタン Button 設定(S)... _システムのプロパティ_詳細設定_設定(S)... グループ Button ユーザー プロファイル テキスト Static サインインに関連したデスクトップ設定 ボタン Button 設定(E)... _システムのプロパティ_詳細設定_設定(E)... グループ Button 起動と回復 テキスト Static システム起動、システム障害、およびデバッグ情報 ボタン Button 設定(T) _システムのプロパティ_詳細設定_設定(T) ボタン Button 環境変数(N)... _システムのプロパティ_詳細設定_環境変数(N)... タブ項目 システムの保護 _システムのプロパティ_システムの保護 タブ項目 リモート _システムのプロパティ_リモート タイトル バー システムのプロパティ メニュー バー システム メニュー バー メニュー項目 システム ボタン 閉じる _システムのプロパティ_閉じる ControlType : ボタン ClassName : Button EventID : AutomationElementIdentifiers.AutomationFocusChangedEvent Property : Name : 設定(S)... Id ProgrammaticName -- ---------------- 10000 InvokePatternIdentifiers.Pattern PS D:\開発\15DAY> UIA_GetParentElement Cached Current CachedParent CachedChildren ------ ------- ------------ -------------- System.Windows.Automation.AutomationElement+AutomationElementInformation System.Windows.Automation.AutomationElement+AutomationElementInformation True UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt _起動と回復 UIA_GetElementTreeWithOpt _起動と回復 UIA_GetElementTreeWithOpt _起動と回復 UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt _起動と回復 UIA_GetElementTreeWithOpt _起動と回復 UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt _起動と回復 UIA_GetElementTreeWithOpt _起動と回復 UIA_GetElementTreeWithOpt _起動と回復 UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt _起動と回復 UIA_GetElementTreeWithOpt _起動と回復 UIA_GetElementTreeWithOpt _起動と回復 UIA_GetElementTreeWithOpt _起動と回復 UIA_GetElementTreeWithOpt _起動と回復 UIA_GetElementTreeWithOpt _起動と回復 UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt _起動と回復 UIA_GetElementTreeWithOpt _起動と回復 UIA_GetElementTreeWithOpt _起動と回復 UIA_GetElementTreeWithOpt _起動と回復 UIA_GetElementTreeWithOpt _起動と回復 UIA_GetElementTreeWithOpt _起動と回復 UIA_GetElementTreeWithOpt _起動と回復 UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt UIA_GetElementTreeWithOpt ControlType ClassName Value action trigger Name key ----------- --------- ----- ------ ------- ---- --- ダイアログ #32770 起動と回復 _起動と回復 グループ Button 起動システム テキスト Static 既定のオペレーティング システム(S): コンボ ボックス ComboBox 既定のオペレーティング システム(S): 一覧 ComboLBox Windows 10 既定のオペレーティング システム(S): 一覧項目 True Windows 10 _起動と回復_Windows 10 ボタン ドロップ ダウン ボタン _起動と回復_ドロップ ダウン ボタン チェック ボックス Button On オペレーティング システムの一覧を表示する時間(T): _起動と回復_オペレーティング システムの一覧を表示する時間(T): スピン msctls_updown32 30 既定のオペレーティング システム(S): ボタン 進む _起動と回復_進む ボタン 戻る _起動と回復_戻る テキスト Static 秒間 チェック ボックス Button Off 必要なときに修復オプションを表示する時間(D): _起動と回復_必要なときに修復オプションを表示する時間(D): スピン msctls_updown32 30 秒間 ボタン 進む _起動と回復_進む ボタン 戻る _起動と回復_戻る テキスト Static 秒間 グループ Button システム エラー チェック ボックス Button On システム ログにイベントを書き込む(W) _起動と回復_システム ログにイベントを書き込む(W) チェック ボックス Button Off 自動的に再起動する(R) _起動と回復_自動的に再起動する(R) グループ Button デバッグ情報の書き込み コンボ ボックス ComboBox デバッグ情報の書き込み 一覧 ComboLBox 自動メモリ ダンプ デバッグ情報の書き込み 一覧項目 False (なし) _起動と回復_(なし) 一覧項目 False 最小メモリ ダンプ (256 KB) _起動と回復_最小メモリ ダンプ (256 KB) 一覧項目 False カーネル メモリ ダンプ _起動と回復_カーネル メモリ ダンプ 一覧項目 False 完全メモリ ダンプ _起動と回復_完全メモリ ダンプ 一覧項目 True 自動メモリ ダンプ _起動と回復_自動メモリ ダンプ 一覧項目 False アクティブ メモリ ダンプ _起動と回復_アクティブ メモリ ダンプ ボタン ドロップ ダウン ボタン _起動と回復_ドロップ ダウン ボタン テキスト Static ダンプ ファイル: 編集 Edit %SystemRoot%\MEMORY.DMP ダンプ ファイル: _起動と回復_ダンプ ファイル: チェック ボックス Button On 既存のファイルに上書きする(O) _起動と回復_既存のファイルに上書きする(O) チェック ボックス Button Off ディスク領域が少ないときでもメモリ ダンプの自動削除を無効にする(A) _起動と回復_ディスク領域が少ないときでもメモリ ダンプの自動削除を無効にする(A) ボタン Button OK _起動と回復_OK ボタン Button キャンセル _起動と回復_キャンセル タイトル バー 起動と回復 メニュー バー システム メニュー バー メニュー項目 システム ボタン 閉じる _起動と回復_閉じる ControlType : ダイアログ ClassName : #32770 EventID : WindowPatternIdentifiers.WindowOpenedProperty Property : Name : 起動と回復 Id ProgrammaticName -- ---------------- 10009 WindowPatternIdentifiers.Pattern 10016 TransformPatternIdentifiers.Pattern PS D:\開発\15DAY> exit PS D:\開発\15DAY>
これで、Windows設定書生成→Windows設定書→Windows自動設定を動作させる為のキー等の制御値の準備が出来ました。
あとはマージの方法ですかね。。