-
Notifications
You must be signed in to change notification settings - Fork 64
/
Control_GUI_App.ps1
156 lines (136 loc) · 5.85 KB
/
Control_GUI_App.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
##############################################################################
# Script: Control_GUI_App.ps1
# Date: 27.Mar.2007
# Version: 2.0
# Author: Jason Fossen (www.WindowsPowerShellTraining.com)
# Purpose: Demos use of SendKeys(), Start-Sleep, and AppActivate().
# Legal: Script provided "AS IS" without warranties or guarantees of any
# kind. USE AT YOUR OWN RISK. Public domain, no rights reserved.
##############################################################################
# Create the famous oWshShell object used in many VBScripts.
$oWshShell = New-Object -COMobject "WScript.Shell" -Strict 2>$Null
# Delete the exported file from last time...(housecleaning)
Remove-Item "$env:userprofile\desktop\EventLog.evt" 2>$Null
# Launch the MMC snap-in for Event Viewer.
Invoke-Expression "$Env:SystemRoot\system32\eventvwr.msc /s"
# Make the script pause for two seconds to let the snap-in show.
# If you try to send keystrokes to an application that has not yet fully
# launched, the application will simply miss the keystrokes.
Start-Sleep -seconds 2
# Though Event Viewer should be in the foreground, this helps to ensure it.
# AppActivate() looks for the exact titlebar name of a running application and
# then brings that application to the foreground and gives it the focus.
# You can use this method to switch between different applications. You can also
# give AppActivate() a process ID number (PID) to bring to the foreground.
$Result = $oWshShell.AppActivate("Event Viewer")
# Now send keystrokes to Event Viewer. The Start-Sleep commands are not really needed,
# but slowing the execution down a bit makes the demo look better!
$oWshShell.SendKeys("{PGUP}") # Hit Page Up key to put focus at top of tree.
$oWshShell.SendKeys("{DOWN}") # Hit the down-arrow key.
Start-Sleep -milliseconds 400
$oWshShell.SendKeys("{DOWN}")
Start-Sleep -milliseconds 400
$oWshShell.SendKeys("{DOWN}")
Start-Sleep -milliseconds 400
$oWshShell.SendKeys("%A") # Hit Alt-A for the Action menu.
Start-Sleep -milliseconds 400
$oWshShell.SendKeys("{DOWN}")
Start-Sleep -milliseconds 400
$oWshShell.SendKeys("{ENTER}") # Hit Enter key.
Start-Sleep -milliseconds 400
$oWshShell.SendKeys("$env:userprofile\desktop\EventLog.evt")
Start-Sleep -seconds 2
$oWshShell.SendKeys("{TAB}") # Tab over to Save button.
Start-Sleep -milliseconds 400
$oWshShell.SendKeys("{TAB}")
Start-Sleep -milliseconds 400
$oWshShell.SendKeys("{ENTER}")
Start-Sleep -milliseconds 500
$oWshShell.SendKeys("%{F4}") # Alt-F4 closes the MMC window.
# Now just to do something fun.... But also notice that parentheses (()), the
# tilde character (~), the percentage sign (%), the power sign (^), and the plus
# sign (+) must be placed inside of curly brackets into order to be sent to the
# application because these characters are meaningful to the SendKeys() method.
notepad.exe
Start-Sleep -seconds 1
# And, again, the following Sleep commands are just for effect. Otherwise, the
# picture would show up so fast as to appear to have been from a file that was opened.
$Result = $oWshShell.AppActivate("Untitled")
$oWshShell.SendKeys("{ENTER}")
$oWshShell.SendKeys(" /\_/\")
Start-Sleep -milliseconds 400
$oWshShell.SendKeys("{ENTER}")
$oWshShell.SendKeys(" / 0 0 \")
Start-Sleep -milliseconds 400
$oWshShell.SendKeys("{ENTER}")
$oWshShell.SendKeys(" ====v====")
Start-Sleep -milliseconds 400
$oWshShell.SendKeys("{ENTER}")
$oWshShell.SendKeys(" \ W /")
Start-Sleep -milliseconds 400
$oWshShell.SendKeys("{ENTER}")
$oWshShell.SendKeys(" | | _")
Start-Sleep -milliseconds 400
$oWshShell.SendKeys("{ENTER}")
$oWshShell.SendKeys(" / ___ \ /")
Start-Sleep -milliseconds 400
$oWshShell.SendKeys("{ENTER}")
$oWshShell.SendKeys(" / / \ \ |")
Start-Sleep -milliseconds 400
$oWshShell.SendKeys("{ENTER}")
$oWshShell.SendKeys(" {(}{(}{(}-----{)}{)}{)}-'") # Notice the curly brackets.
Start-Sleep -milliseconds 400
$oWshShell.SendKeys("{ENTER}")
$oWshShell.SendKeys(" / \")
Start-Sleep -milliseconds 400
$oWshShell.SendKeys("{ENTER}")
$oWshShell.SendKeys(" {(} ___{)}") # More curly brackets.
Start-Sleep -milliseconds 400
$oWshShell.SendKeys("{ENTER}")
$oWshShell.SendKeys(" \__.=|___E")
Start-Sleep -milliseconds 400
$oWshShell.SendKeys("{ENTER}")
$oWshShell.SendKeys(" /")
$oWshShell.SendKeys("{ENTER}")
Start-Sleep -seconds 2
$oWshShell.SendKeys("%{F4}") # To close Notepad.
$oWshShell.SendKeys("%N") # To say No to saving.
# The following is the syntax for sending special keystrokes with SendKeys().
# Shift Key +
# Ctrl Key ^
# Alt Key %
# Backspace {BACKSPACE}, {BS} or {BKSP}
# Break {BREAK}
# Caps Lock {CAPSLOCK}
# Delete {DELETE} or {DEL}
# Cursor Up {UP}
# Cursor Down {DOWN}
# Cursor Right {RIGHT}
# Cursor Left {LEFT}
# End {END}
# Enter {ENTER} or ~
# Esc {ESC}
# Home {HOME}
# Insert {INSERT} or {INS}
# Num Lock {NUMLOCK}
# Page Down {PGDN}
# Page Up {PGUP}
# Scroll Lock {SCROLLOCK}
# Tab {TAB}
# F1, F2, F3... {F1}, {F2}, {F3}...
#
# To close a program, use Alt-F4 = %{F4}
#
# To access menu commands, look for the underlined letters on the menus, then
# enter Alt-letter, e.g., %F to pull down the File menu, then %X to Exit.
#
# Note: It's not possible to script Ctrl-Alt-Del.
#
# To hold down one key and press others, the symbol for the held key should
# be followed with parentheses in which the other keys should be listed. For
# example, to send Shift-A-B, enter +(AB), or to send Alt-C-D-E, enter %(CDE).
#
# To repeat a keystroke multiple times, inside curley brackets place the keystroke,
# a space character, and the multiple number. For example, to enter the letter R
# tenty times, use {R 20}.
# END OF SCRIPT ****************************************************************