locked
detecting a window and verifying a text on that window RRS feed

  • Question

  • Hi,

    I'm trying to write a script where I can automate the installation of certain products  and also verify whether the updates are successful.

    here is the steps i will follow:

    install the procut using commandline arguments

    install updates using command line

    My problem is that when i update some products through commandline , it opens an UI automatically and displays the text "updating.... "  on the window and below it gives a status whether the updates are successful or failed.

    So i need to capture the texts on this window.

    I tried to get the process which might be running but the update window opens in a process which is shared by multiple windows.

    if updates failed i need to abort the script and perform some actions.

    Can this be done in powershell since its a 3rd party applicaton? I heard about WASP (automation snapin for powershell, not used it and I'm not sure whether it will solve my problem

    Any help in this regard will be appreciated.

    Thanks,

    -Dilip M


    Friday, June 10, 2011 3:16 PM

Answers

  • You can use WPF UI automation, here is code I used to get Windows XP Start button text:

    [void] [Reflection.Assembly]::Load('UIAutomationClient, ' + 
      'Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35')
    [void] [Reflection.Assembly]::Load('UIAutomationTypes, ' + 
      'Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35')
    
    $source = @"
    
    using System;
    using System.Windows.Automation;
    
    namespace UIAutTools
    {
      public class Element
      {
        public static AutomationElement RootElement
        {
          get
          {
            return AutomationElement.RootElement;
          }
        }
      }
    }
    
    "@
    
    Add-Type -TypeDefinition $source -ReferencedAssemblies( `
      "UIAutomationClient", "UIAutomationTypes")
    
    $root = [UIAutTools.Element]::RootElement
    
    $condition = New-Object Windows.Automation.PropertyCondition( `
      [Windows.Automation.AutomationElement]::NameProperty, `
      'start')
    
    $startButton = $root.FindFirst( `
      [Windows.Automation.TreeScope]::Descendants, $condition)
    
    $startButton.Current.Name
    $startButton.Current.ControlType.ProgrammaticName
    


    I needed the C# snippet to get the root automation element (see here). You can use AutomationElement.FindAll to enumerate all open windows.

     


    Uros Calakovic
    • Marked as answer by Tim Quan Monday, July 11, 2011 6:16 AM
    Monday, June 13, 2011 3:18 PM

All replies

  • Try this:

    Get-Process | ? {$_.MainWindowTitle} | select MainWindowTitle

    Friday, June 10, 2011 4:10 PM
  • Thanks Kazun for the reply. The code will give the window titles and I will be able to pick up the correct window.

    But how would I read any text displayed in that window.

    for e.g when you open an Antivirus console when scanning is going on.. it may display a text like "Scanning on..." which may get updated to scanning complete after sometime which i need to capture.

    Can this be done by getting some properties of control on that window. I read somewhere about SPY++ to capture the control properties like classname etc and provide it. but not sure whether its possible in powershell?

     

    Thanks,

    -Dilip M

    Friday, June 10, 2011 6:09 PM
  • You can use WPF UI automation, here is code I used to get Windows XP Start button text:

    [void] [Reflection.Assembly]::Load('UIAutomationClient, ' + 
      'Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35')
    [void] [Reflection.Assembly]::Load('UIAutomationTypes, ' + 
      'Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35')
    
    $source = @"
    
    using System;
    using System.Windows.Automation;
    
    namespace UIAutTools
    {
      public class Element
      {
        public static AutomationElement RootElement
        {
          get
          {
            return AutomationElement.RootElement;
          }
        }
      }
    }
    
    "@
    
    Add-Type -TypeDefinition $source -ReferencedAssemblies( `
      "UIAutomationClient", "UIAutomationTypes")
    
    $root = [UIAutTools.Element]::RootElement
    
    $condition = New-Object Windows.Automation.PropertyCondition( `
      [Windows.Automation.AutomationElement]::NameProperty, `
      'start')
    
    $startButton = $root.FindFirst( `
      [Windows.Automation.TreeScope]::Descendants, $condition)
    
    $startButton.Current.Name
    $startButton.Current.ControlType.ProgrammaticName
    


    I needed the C# snippet to get the root automation element (see here). You can use AutomationElement.FindAll to enumerate all open windows.

     


    Uros Calakovic
    • Marked as answer by Tim Quan Monday, July 11, 2011 6:16 AM
    Monday, June 13, 2011 3:18 PM