-
Notifications
You must be signed in to change notification settings - Fork 0
/
Send-sjKeystrokes-Function.ps1
44 lines (36 loc) · 1.03 KB
/
Send-sjKeystrokes-Function.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
function Send-sjKeystrokes {
param(
[parameter(Mandatory)]
[string]$WindowTitle,
[byte]$SecondsToWait = 2,
[parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[string[]]$Keystrokes
)
begin{
try {
$wshell = New-Object -ComObject wscript.shell
[Void][System.Reflection.Assembly]::Load('System.Windows.Forms')
}
catch {
throw 'Unable to load required assemblies'
}
}
process{
foreach($item in $Keystrokes){
if($wshell.AppActivate($WindowTitle)){
[System.Windows.Forms.SendKeys]::SendWait($item)
Start-Sleep -Seconds $SecondsToWait
} else {
Write-Warning "Unable to focus on Window: $WindowTitle"
}
}
}
end {}
}
# Test cases
# Send-sjKeystrokes -WindowTitle 'Notepad' -SecondsToWait 1 -Keystrokes 'asdf'
# $SendSjKeys = @{
# WindowTitle = 'Load Balancer Administration System and 37 more pages'
# SecondsToWait = 0
# }
# '{tab 12}web1021{tab}','10.200.10.56{tab}','0{tab 4}' | Send-sjKeystrokes @SendSjKeys