Windows UI Automation with PowerShell

博客首页 » Windows UI Automation with PowerShell

发布于 22 Jun 2014 15:27
标签 blog
使用PowerShell + 自己定制的.Net CLR,我们可以用shell判断并自动化Windows GUI

http://msdn.microsoft.com/en-us/magazine/cc163301.aspx

经过定义库,我们可以做这样的事:

write-host "Checking contents of TextBox for 'ad'" 
[System.Threading.Thread]::Sleep(2000) 
$text = get-textBox $tb

这是作者考虑的库集合

  • get-window
  • get-control
  • get-controlByIndex
  • send-chars
  • send-click
  • get-listBox
  • get-textBox
  • send-menu

库代码

using System;
using System.Management.Automation;
using System.ComponentModel;
using System.Configuration.Install;
using System.Runtime.InteropServices;
namespace CustomUICmdletsLib { 

[Cmdlet(VerbsCommon.Get, "Window")] public class GetWindowCommand : Cmdlet { . . . } 
[Cmdlet(VerbsCommon.Get, "Control")] public class GetControlCommand : Cmdlet { . . . } 
[Cmdlet(VerbsCommon.Get, "ControlByIndex")] public class GetControlByIndexCommand : Cmdlet { . . . } 
[Cmdlet(VerbsCommunications.Send, "Chars")] public class SetCharsCommand : Cmdlet { . . . } 
[Cmdlet(VerbsCommunications.Send, "Click")] public class SetClickCommand : Cmdlet { . . . } 
[Cmdlet(VerbsCommon.Get, "ListBox")] public class GetListBoxCommand : Cmdlet { . . . } 
[Cmdlet(VerbsCommon.Get, "TextBox")] public class GetTextBoxCommand : Cmdlet { . . . } 
[Cmdlet(VerbsCommunications.Send, "Menu")] public class SetMenuCommand : Cmdlet { . . . } 
[RunInstaller(true)] public class LibPSSnapIn : PSSnapIn { . . . } 
}

[Cmdlet(VerbsCommon.Get, "Window")] public class GetWindowCommand : Cmdlet { 
  [DllImport("user32.dll", CharSet=CharSet.Auto)] static extern IntPtr FindWindow( string lpClassName, string lpWindowName);
  private string windowName;

  [Parameter(Position = 0)] public string WindowName { 
    get { 
      return windowName;
    } 
    set { 
      windowName = value;
    } 
  } 
  protected override void ProcessRecord() { 
    IntPtr wh = FindWindow(null, windowName);
    WriteObject(wh);
  } 
}

Register the cmdlets

# file: Microsoft.PowerShell_profile.ps1 
function RegisterUILib { 
write-host "registering custom cmdlets for UI automation`n" 
$env:path = $env:path += ";C:\Windows\Microsoft.NET\Framework\v2.0.50727" 
sl 'C:\UIautomationWithPowerShell\CustomUICmdletsLib\bin\Debug' 
installutil.exe CustomUICmdletsLib.dll | out-null 
add-pssnapin LibPSSnapin 
write-host "get-window, get-control, get-controlByIndex, send-chars, send-click" write-host "get-listBox, send-menu, and get-textBox custom cmdlets are enabled `n" 
} 
RegisterUILib 
# invoke function set-location C:\UIautomationWithPowerShell 
# end startup script

本页面的文字允许在知识共享 署名-相同方式共享 3.0协议和GNU自由文档许可证下修改和再使用,仅有一个特殊要求,请用链接方式注明文章引用出处及作者。请协助维护作者合法权益。


系列文章

文章列表

  • Windows UI Automation with PowerShell

这篇文章对你有帮助吗,投个票吧?

rating: 0+x

留下你的评论

Add a New Comment
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License