如何在 powershell 中使用 IUIAutomation?

How to use IUIAutomation in powershell?

我想使用 UI 自动化。如其他问题 () 中所述,System.Windows.Automation 已弃用。所以我的问题 - 如何将 COM IUIA​​utomation 与 powershell 一起使用?

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

System.Windows.Automation 有点过时但是:

  • 对于大多数操作来说它工作得很好,我不确定链接文章中提到的性能问题是否与它被弃用的事实有关。
  • 它依赖于包含所有新代码的原生 Windows-provided UIAutomationCore.dll 程序集,所以我的猜测 System.Windows.Automation 是间接使用下面的新 COM 对象。
  • 我相信它是您唯一可以从 PowerShell 使用的 API,因为您只能使用 COM 自动化对象(不要与 System.Windows.Automation 混淆),即:.NET、VB6、脚本等不是纯 IUnknown(不是 IDispatch)接口。

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

这里是一个小代码片段:

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