【问题标题】:PowerShell UIAutomation script not returning expected resultPowerShell UIAutomation 脚本未返回预期结果
【发布时间】:2011-04-12 04:43:54
【问题描述】:

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

我正在尝试更改 Windows XP 快速启动设置(使用 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# 程序都假定小程序已经打开/可见。 (XP SP3)

【问题讨论】:

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


    【解决方案1】:

    基于Using WPF UI Automation with PowerShell (CodeProject)Problem finding AutomationElement in PowerShell script (MSDN Forums)

    1. UI 自动化 API 仅适用于 STA 线程,并且 PowerShell 始终运行脚本 MTA。解决此问题的方法是使用 -sta 开关运行 PowerShell,或从默认为 STA 的 PowerShell“集成脚本环境”(ISE) 中运行脚本。

    2. PowerShell 处理值类型存在问题。解决方案是在脚本中插入一些内联 C# 代码来获取 UIAutomation 根元素。

    工作代码:

    [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
    

    【讨论】:

    • 是的,就是这样。我自己也遇到过这个(我稍后会在 wasp.codeplex.com 上将生成的模块作为 WASP 2 发布)
    • 感谢您的回答 - 这是我的博文 :-) 我很抱歉没有更新这个帖子。
    • 这篇博文好像没有了,能否请您分享一下您是如何修复它的详细信息?
    • @DmitryTrukhanov 我没有解决任何问题,问题的作者自己解决了所有问题并写了一篇关于它的博客文章。好吧,RIP。用户没了,用户写的博文也没了,没有存档版本。
    • 我在一个单独的博客codeproject.com/Tips/110824/… 中找到了这篇文章,希望它还能再活10 年,对于将来遇到类似问题的人来说。 :)
    猜你喜欢
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    • 2011-08-09
    • 2010-09-22
    • 2021-05-17
    • 2018-02-19
    • 2018-03-23
    • 2020-03-15
    相关资源
    最近更新 更多