如何在powershell中使用IUIAutomation?

时间:2017-02-10 13:24:22

标签: powershell com ui-automation

我想使用UI自动化。如其他问题(https://stackoverflow.com/a/41768047/3402056)中所述,不推荐使用System.Windows.Automation。所以我的问题 - 如何在PowerShell中使用COM IUIAutomation?

有-ComObject参数的命令行开关New-Object。该参数是否接受可创建CoClass的名称?我在哪里可以得到它?

2 个答案:

答案 0 :(得分:1)

System.Windows.Automation有点弃用,但是:

  • 它适用于大多数操作,并且我不确定链接文章中提到的性能问题与它已被弃用的事实有关。
  • 它依赖于本机Windows提供的 UIAutomationCore.dll 程序集,其中包含所有新代码,所以我的猜测System.Windows.Automation是间接使用下面的新COM对象。
  • 我相信它是您可以在PowerShell中使用的唯一API,因为您只能使用COM Automation对象(不要与System.Windows.Automation混淆),即:.NET,VB6,Scripting等不是纯粹的IUnknown (不是IDispatch)接口。

作为替代方案,您可以使用UI Automation COM-to-.NET Adapter这是UIAutomationClient程序集的更新版本(由Microsoft人员编写),但这仅在您需要Windows 8 UIA新属性和模式时才需要,或者如果需要你发现它对你有效。

答案 1 :(得分:0)

这是一个以以下代码开头的小代码段:

Add-Type -AssemblyName @('UIAutomationClient', 'UIAutomationTypes')
$ae = [System.Windows.Automation.AutomationElement]
$obj1 = $ae::FromHandle($winHandle)
相关问题