views:

118

answers:

1

[I have posted this question earlier in the PowerShell Technet forum, but without a response]

I am attempting to change Windows XP Quick Launch settings (enable / disable it using PowerShell). Existing VBScript solutions rely either on Registry or SendKeys, so I thought that this would be feasible in PowerShell through UIAutomation. My first attempt was just to get the reference to the AutomationElement that represents the check box that needs to be changed (Control Panel -> Taskbar and Start Menu -> Taskbar tab -> Show Quick Launch check box). Here is the script:

[void][System.Reflection.Assembly]::LoadWithPartialName("UIAutomationClient")
[void][System.Reflection.Assembly]::LoadWithPartialName("UIAutomationTypes")

$root = [Windows.Automation.AutomationElement]::RootElement

$condition1 = New-Object Windows.Automation.PropertyCondition([Windows.Automation.AutomationElement]::NameProperty, 'Taskbar and Start Menu Properties')
$properties = $root.FindFirst([Windows.Automation.TreeScope]::Descendants, $condition1)

$condition2 = New-Object Windows.Automation.PropertyCondition([Windows.Automation.AutomationElement]::NameProperty, 'Show Quick Launch')

$checkboxes = $properties.FindAll([Windows.Automation.TreeScope]::Descendants, $condition2)

foreach($checkbox in $checkboxes)
{
  $checkbox.Current.Name
  $checkbox.Current.ControlType.ProgrammaticName
}

The scripts doesn't error out, but it returns unexpected results:

Show Quick Launch

ControlType.Pane

Instead of ControlType.CheckBox the script sees the AutomationElement as ControlType.Pane. The equivalent (at least I think so) C# console application returns the expected results:

using System;
using System.Windows.Automation;

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      AutomationElement root = AutomationElement.RootElement;
      AutomationElement properties = root.FindFirst(
        TreeScope.Descendants,
        new PropertyCondition(AutomationElement.NameProperty,
          "Taskbar and Start Menu Properties"));

      AutomationElementCollection checkboxes = properties.FindAll(
        TreeScope.Descendants,
        new PropertyCondition(AutomationElement.NameProperty,
          "Show Quick Launch"));

      foreach (AutomationElement checkbox in checkboxes)
      {
        Console.WriteLine(checkbox.Current.Name);
        Console.WriteLine(checkbox.Current.ControlType.ProgrammaticName);
      }
    }
  }
}

Show Quick Launch

ControlType.CheckBox

What am I doing wrong? The PowerShell script is executed from the ISE (so V2) and both the script and the C# program assume that the applet is already open / visible. (XP SP3)

+2  A: 

Try solution provided here: http://theadminblog.blogspot.com/2010/09/wpf-ui-automation-powershell-problem.html The author ran into the same problem and solved it.

Athari
Yep, this is exactly it. I just came across this myself (I'll publish the resulting module as WASP 2 on wasp.codeplex.com later)
Jaykul
Thank you for the answer - that is my blog post :-) I apologize for not updating this thread.
Uros Calakovic