Daily automation: dumping control’s genealogy

Note: this post is deprecated, please refer to my comment below and the last edition of this post.

One more way to find out where in the Automation tree a control resides is to use the Get-UIAControlAncestors cmdlet. We are told that TreeScope.Parent and TreeScope.Ancestors used as filter for search will call the functionality that is not yet implemented? However, the cmdlet does perform the search from the very bottom of the hierarchy to the topmost window. Let’s take Calculator and dump the control hierarchy:

Task: demonstrate using the search to the top cmdlet.

Requirements: perform search that consumes a part of control’s name and retrieve all the ancestors of the search result.

Solution: the following code prints out all the hierarchy in reverse order:

Set-StrictMode -Version Latest;
ipmo [path]\UIAutomation.dll;
# here we are searching for all the element whose names start with 'a'
Start-Process calc -PassThru | Get-UIAWindow | Search-UIAControl -Name A* | `
 %{Write-Host "===========================================================";
 Write-Host "@{Name='$($_.Current.Name)'; AutomaitonId='$($_.Current.AutomaitonId); ControlType='$($_.Current.ControlType.ProgrammaticName)'}"; $_ | Get-UIAControlAncestors | `
 %{Write-Host "@{Name='$($_.Current.Name)'; AutomaitonId='$($_.Current.AutomaitonId); ControlType='$($_.Current.ControlType.ProgrammaticName)'}";}};

4 responses

  1. Good day! Where did the cmdlet “Search-UIAControl” or where do I look to find that his? I try use instead cmdlet “Search-UIAControl” cmdlet “Get-UIAControl”. In this case I have not received any “AutomaitonId”. I apologize for my English. Sincerely, Iliya.

    1. I’m sorry, this post is not only bearded old, this contains mistakes (the spelling of the AutomationId property). I re-posted it, the link is in the note at the top.

  2. Hello, this post is now deprecated (I’ll mark it) and a lot has changed since the post had been published.
    The time the post has been published, Get-UIAWindow and Get-UIA[ControlType] cmdlets could return only ONE AutomationElement. It was embedded restriction and Search- cmdlets were introduced to meet two goals: allow people to collect all controls by a criterion and not to change Get- cmdlets’ code for that.
    This summer or spring, people asked for modifying Get- cmdlets so that they’d act as any Get- cmdlet, for example, Get-ChildItem. The Get-ChildItem or Get-Process cmdlet returns all that matches the criterion and now our our Get- cmdlets do the same. At the same time as Get- cmdlets were re-written, many other cmdlets, including Read- cmdlets, were also changed. Now, Read- cmdlets easily read elements from collections and the code got much simpler:
    Start-Process calc -PassThru | Get-UIAWindow | Get-UIAControl -Name A* | Read-UIAControlAutomationId

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: