2010년 9월 13일 월요일

파워 쉘 로 자동화하기 (Windows Automation Snapin for Powershell)

WASP(Windows Automation Snapin for Powershell) http://wasp.codeplex.com/

윈도우를 선택, 활성화, 창 크기 변경, 창 닫기, Keyboard,Mouse Action 등을 할 수 있는 PowerShell 자동화 Snapin

Autohot Key 와 비슷한 기능들을 이용 가능할 것 같다.

사용 가능한 명령어들은 아래와 같다.

•Select-Window - pick windows by process name or window caption (with wildcard support)

•Select-ChildWindow - pick all owned windows of another window (eg: dialogs, tool windows)

•Select-Control - pick controls (children) of a specific window, by class and/or name and/or index (with wildcard support) -- NOTE: the "Window" can be specified as "-Window 0" to get all parentless windows, which includes windows, dialogs, tooltips, etc... With -Window 0 this returns a true superset of the Select-Window output.

•Send-Click - send mouse clicks (any button, with any modifier keys) •Send-Keys - Windows.Forms.SendKeys lets you send keys ... try this: Select-Window notepad | Send-Keys "%(ea)Testing{Enter}{F5}" (and for extra fun, try it with multiple notepad windows open).

•Set-WindowActive - yeah, just activates the window

•Set-WindowPosition - set any one of (or all of) top, left, width, height on a window ... or maximize/minimize/restore

•Get-WindowPosition - get the position (kind-of redundant, actually, since the Window object has it's position as a property)

•Remove-Window - closes the specified window

WASP 1.3 RC 버전이 이글을 쓸때의 신버전이고(20100913) http://wasp.codeplex.com/releases/view/22118

설치는 압축을 해제하면 .아래 3개의 파일이 실행된다.

Install.ps1
UnInstall.ps1
WASP.dll

PowerShell 을 실행 후  Install.ps1 을 실행하면  2.0이 설치되어있다고 PSSnapin을 이용해 Import-Module 을 하거나 -Force를 이용해서 하라고 한다.

PS C:\WASP> .\Install.ps1
You're running PowerShell 2.0, so you don't need to Install this as a PSSnapin, you can use Import-Module (or Add-Module
in CTP2) to load it.  If you still want to install it as a PSSnapin, re-run this script with -Force

-Force 명령을 실행하면 아래와 같이 설치가 진행 된다.

PS C:\WASP> .\Install.ps1 -force
You're running PowerShell 2.0, so you don't need to Install this as a PSSnapin, you can use Import-Module (or Add-Module
in CTP2) to load it.  If you still want to install it as a PSSnapin, re-run this script with -Force
Microsoft (R) .NET Framework Installation utility Version 2.0.50727.4927
Copyright (c) Microsoft Corporation. All rights reserved.

트랜잭트 설치를 실행하고 있습니다.

설치의 Install 단계를 시작하고 있습니다.
C:\Users\kwang\Desktop\WASP\WASP\WASP.dll 어셈블리의 진행 상황을 보려면 로그 파일 내용을 검토하십시오.
파일은 C:\Users\kwang\Desktop\WASP\WASP\WASP.InstallLog 위치에 있습니다.
어셈블리 'C:\Users\kwang\Desktop\WASP\WASP\WASP.dll'을(를) 설치하고 있습니다.
영향을 받는 매개 변수:
   assemblypath = C:\Users\kwang\Desktop\WASP\WASP\WASP.dll
   logfile = C:\Users\kwang\Desktop\WASP\WASP\WASP.InstallLog
   logtoconsole =

Install 단계는 완료되었으며 Commit 단계를 시작하고 있습니다.
C:\Users\kwang\Desktop\WASP\WASP\WASP.dll 어셈블리의 진행 상황을 보려면 로그 파일 내용을 검토하십시오.
파일은 C:\Users\kwang\Desktop\WASP\WASP\WASP.InstallLog 위치에 있습니다.
어셈블리 'C:\Users\kwang\Desktop\WASP\WASP\WASP.dll'을(를) 커밋하고 있습니다.
영향을 받는 매개 변수:
   assemblypath = C:\Users\kwang\Desktop\WASP\WASP\WASP.dll
   logfile = C:\Users\kwang\Desktop\WASP\WASP\WASP.InstallLog
   logtoconsole =

Commit 단계가 완료되었습니다.

트랜잭트 설치가 완료되었습니다.

CommandType     Name                                                Definition
-----------     ----                                                ----------
Cmdlet          Get-WindowPosition                                  Get-WindowPosition [-Window] <WindowHandle[]> [-...
Cmdlet          Remove-Window                                       Remove-Window [-Window] <WindowHandle[]> [-Passt...
Cmdlet          Select-ChildWindow                                  Select-ChildWindow [-Window] <WindowHandle[]> [-...
Cmdlet          Select-Control                                      Select-Control [[-Index] <Int32>] -Window <Windo...
Cmdlet          Select-Window                                       Select-Window [[-ProcessName] <String[]>] [-Tool...
Cmdlet          Send-Click                                          Send-Click [[-Left] <Int32>] [[-Top] <Int32>] [[...
Cmdlet          Send-Keys                                           Send-Keys [-Keys] <String> [-Window] <WindowHand...
Cmdlet          Set-WindowActive                                    Set-WindowActive [-Window] <WindowHandle[]> [-Pa...
Cmdlet          Set-WindowPosition                                  Set-WindowPosition [[-Left] <Int32>] [[-Top] <In...
To load the Windows Automation Snapin in the future, you need to run:
Add-PSSnapin WASP

You can also add that line to your Profile script to load it automatically.

Sample Demo

## Open a couple windows(윈도우를 두개 실행)
notepad.exe
explorer.exe

## list the windows( 윈도우 리스트를 출력)
Select-Window | ft –auto

## Activate Notepad(노트패드를 활성화)
Select-Window notepad* | Set-WindowActive

## Close Explorer( 탐색기 종료)
Select-Window explorer | Select -First 1 | Remove-WIndow

## Run a few more copies of notepad(노트패드를 여러개 실행)
notepad; notepad; notepad; notepad;

## Move them around so we can see them all ... (Note the use of foreach with the incrementation)위치 변경
$i = 1;$t = 100; Select-Window notepad | ForEach { Set-WindowPosition -X 20 -Y (($i++)*$t) -Window $_ }

## Put some text into them(텍스트를 입력)

Select-Window notepad | Send-Keys "Test Send-Keys !!!"

#### Close the first notepad window by pressing ALT+F4, and pressing Alt+N
## In this case, you don't have to worry about shifting focus to the popup because it's modal
## THE PROBLEM with sending keys like that is:
## if there is no confirmation dialog because the file is unchanged, the Alt+N still gets sent

Select-Window notepad | Select -First 1 | Send-Keys "%{F4}"

## Close the next notepad window ...
## By asking nicely (Remove-Window) and then hitting "n" for "Don't Save"
## If there are no popups, Select-ChldWindow returns nothing, and that's the end of it

## Close the next notepad window the hard way
## Just to show off that our "Window" objects have a ProcessID and can be piped to kill

Select-Window notepad | Select -First 1 | kill

## A different way to confirm Don't Save (use CLICK instead of keyboard)
## Notice how I dive in through several layers of Select-Control to find the button?
## This can only work experimentally:
## use SPY++, or run the line repeatedly, adding "|Select-Control" until you see the one you want

Select-Window notepad | Select -First 1 | Remove-Window -Passthru | Select-childwindow | select-control| select-control| select-control -title "Do&n't Save" | Send-Click

## But now we have the new -Recurse parameter, so it's easy. Just find the window you want and ...

Select-Window notepad | Select -First 1 | Remove-Window -Passthru | Select-childwindow | select-control -title "Do&n't Save" -recurse | Send-Click

댓글 없음:

댓글 쓰기