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