[CmdletBinding()] param( [string]$WindowTitlePattern = 'Library Manager', [int]$ProcessId = 0, [Parameter(Mandatory)][string]$OutputJson ) Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' Add-Type -AssemblyName UIAutomationClient Add-Type -AssemblyName UIAutomationTypes $root = [System.Windows.Automation.AutomationElement]::RootElement $windows = @($root.FindAll( [System.Windows.Automation.TreeScope]::Children, [System.Windows.Automation.Condition]::TrueCondition ) | Where-Object { [string]$_.Current.Name -match $WindowTitlePattern }) if ($ProcessId -gt 0) { $windows = @($windows | Where-Object { [int]$_.Current.ProcessId -eq $ProcessId }) } if ($windows.Count -ne 1) { throw "Expected exactly one Library Manager window, found $($windows.Count)." } function Get-ElementRecord { param([Parameter(Mandatory)]$Element) $patterns = New-Object 'System.Collections.Generic.List[string]' foreach ($pattern in @( [System.Windows.Automation.InvokePattern]::Pattern, [System.Windows.Automation.ExpandCollapsePattern]::Pattern )) { try { if ($null -ne $Element.GetCurrentPattern($pattern)) { [void]$patterns.Add([string]$pattern.ProgrammaticName) } } catch { } } [pscustomobject]@{ name = [string]$Element.Current.Name automationId = [string]$Element.Current.AutomationId controlType = [string]$Element.Current.ControlType.ProgrammaticName className = [string]$Element.Current.ClassName processId = [int]$Element.Current.ProcessId nativeWindowHandle = [int]$Element.Current.NativeWindowHandle isEnabled = [bool]$Element.Current.IsEnabled patterns = @($patterns) } } $tree = @($windows[0].FindAll( [System.Windows.Automation.TreeScope]::Descendants, [System.Windows.Automation.Condition]::TrueCondition ) | ForEach-Object { Get-ElementRecord -Element $_ }) $document = [ordered]@{ schemaVersion = 1 capturedAtUtc = [datetime]::UtcNow.ToString('o') windowTitlePattern = $WindowTitlePattern processId = [int]$windows[0].Current.ProcessId window = Get-ElementRecord -Element $windows[0] elements = $tree } $parent = Split-Path -Parent ([IO.Path]::GetFullPath($OutputJson)) if ($parent -and -not (Test-Path -LiteralPath $parent -PathType Container)) { New-Item -ItemType Directory -Path $parent -Force | Out-Null } $document | ConvertTo-Json -Depth 8 | Set-Content -LiteralPath $OutputJson -Encoding UTF8 $document | ConvertTo-Json -Depth 8