PowerShell UIAutomation脚本未返回预期结果

时间:2010-09-14 14:04:22

标签: c# scripting powershell operating-system ui-automation

[我之前在PowerShell Technet论坛上发布了这个问题,但没有回复]

我正在尝试更改Windows XP Quick Launch设置(使用PowerShell启用/禁用它)。现有的VBScript解决方案依赖于Registry或SendKeys,所以我认为这在PowerShell中通过UIAutomation是可行的。我的第一次尝试只是获取AutomationElement的引用,该引用表示需要更改的复选框(控制面板 - >任务栏和开始菜单 - >任务栏选项卡 - >显示快速启动复选框)。这是脚本:

[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
}

脚本不会出错,但会返回意外结果:

显示快速启动

ControlType.Pane

脚本将AutomationElement视为ControlType.Pane,而不是ControlType.CheckBox。等效(至少我认为是这样)C#控制台应用程序返回预期的结果:

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);
      }
    }
  }
}

显示快速启动

ControlType.CheckBox

我做错了什么? PowerShell脚本是从ISE(所以V2)执行的,脚本和C#程序都假定applet已经打开/可见。 (XP SP3)

1 个答案:

答案 0 :(得分:1)

尝试此处提供的解决方案:http://theadminblog.blogspot.com/2010/09/wpf-ui-automation-powershell-problem.html作者遇到了同样的问题并解决了它。