Skip to content

Commit ecd107c

Browse files
authored
Merge pull request #91 from alagoutte/enhance-vmkeystrokes
Enhance VMKeyStrokes
2 parents 5f63e17 + 40e3e74 commit ecd107c

File tree

1 file changed

+176
-102
lines changed

1 file changed

+176
-102
lines changed

powershell/VMKeystrokes.ps1

Lines changed: 176 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -10,151 +10,225 @@
1010
.DESCRIPTION
1111
This function sends a series of character keystrokse to a particular VM
1212
.PARAMETER VMName
13-
The name of a VM to send keystrokes to
14-
.PARAMETER StringInput
15-
The string of characters to send to VM
16-
.PARAMETER DebugOn
17-
Enable debugging which will output input charcaters and their mappings
13+
The name of a VM to send keystrokes to
14+
.PARAMETER StringInput
15+
The string of characters to send to VM
16+
.PARAMETER DebugOn
17+
Enable debugging which will output input charcaters and their mappings
1818
.EXAMPLE
1919
Set-VMKeystrokes -VMName $VM -StringInput "root"
20+
21+
Push "root" to VM $VM
2022
.EXAMPLE
2123
Set-VMKeystrokes -VMName $VM -StringInput "root" -ReturnCarriage $true
24+
25+
Push "root" with return line to VM $VM
2226
.EXAMPLE
2327
Set-VMKeystrokes -VMName $VM -StringInput "root" -DebugOn $true
28+
29+
Push "root" to VM $VM with some debug
30+
===========================================================================
31+
Modified by: David Rodriguez
32+
Organization: Sysadmintutorials
33+
Blog: www.sysadmintutorials.com
34+
Twitter: @systutorials
35+
===========================================================================
36+
.MODS
37+
Made $StringInput Optional
38+
Added a $SpecialKeyInput - See PARAMETER SpecialKeyInput below
39+
Added description to write-hosts [SCRIPTINPUT] OR [SPECIALKEYINPUT]
40+
.PARAMETER StringInput
41+
The string of single characters to send to the VM
42+
.PARAMETER SpecialKeyInput
43+
All Function Keys i.e. F1 - F12
44+
Keyboard TAB, ESC, BACKSPACE, ENTER
45+
Keyboard Up, Down, Left Right
46+
.EXAMPLE
47+
Set-VMKeystrokes -VMName $VM -SpecialKeyInput "F2"
48+
49+
Push SpecialKeyInput F2 to VM $VM
50+
2451
#>
2552
param(
26-
[Parameter(Mandatory=$true)][String]$VMName,
27-
[Parameter(Mandatory=$true)][String]$StringInput,
28-
[Parameter(Mandatory=$false)][Boolean]$ReturnCarriage,
29-
[Parameter(Mandatory=$false)][Boolean]$DebugOn
53+
[Parameter(Mandatory = $true)][String]$VMName,
54+
[Parameter(Mandatory = $false)][String]$StringInput,
55+
[Parameter(Mandatory = $false)][String]$SpecialKeyInput,
56+
[Parameter(Mandatory = $false)][Boolean]$ReturnCarriage,
57+
[Parameter(Mandatory = $false)][Boolean]$DebugOn
3058
)
3159

3260
# Map subset of USB HID keyboard scancodes
3361
# https://gist.github.com/MightyPork/6da26e382a7ad91b5496ee55fdc73db2
3462
$hidCharacterMap = @{
35-
"a"="0x04";
36-
"b"="0x05";
37-
"c"="0x06";
38-
"d"="0x07";
39-
"e"="0x08";
40-
"f"="0x09";
41-
"g"="0x0a";
42-
"h"="0x0b";
43-
"i"="0x0c";
44-
"j"="0x0d";
45-
"k"="0x0e";
46-
"l"="0x0f";
47-
"m"="0x10";
48-
"n"="0x11";
49-
"o"="0x12";
50-
"p"="0x13";
51-
"q"="0x14";
52-
"r"="0x15";
53-
"s"="0x16";
54-
"t"="0x17";
55-
"u"="0x18";
56-
"v"="0x19";
57-
"w"="0x1a";
58-
"x"="0x1b";
59-
"y"="0x1c";
60-
"z"="0x1d";
61-
"1"="0x1e";
62-
"2"="0x1f";
63-
"3"="0x20";
64-
"4"="0x21";
65-
"5"="0x22";
66-
"6"="0x23";
67-
"7"="0x24";
68-
"8"="0x25";
69-
"9"="0x26";
70-
"0"="0x27";
71-
"!"="0x1e";
72-
"@"="0x1f";
73-
"#"="0x20";
74-
"$"="0x21";
75-
"%"="0x22";
76-
"^"="0x23";
77-
"&"="0x24";
78-
"*"="0x25";
79-
"("="0x26";
80-
")"="0x27";
81-
"_"="0x2d";
82-
"+"="0x2e";
83-
"{"="0x2f";
84-
"}"="0x30";
85-
"|"="0x31";
86-
":"="0x33";
87-
"`""="0x34";
88-
"~"="0x35";
89-
"<"="0x36";
90-
">"="0x37";
91-
"?"="0x38";
92-
"-"="0x2d";
93-
"="="0x2e";
94-
"["="0x2f";
95-
"]"="0x30";
96-
"\"="0x31";
97-
"`;"="0x33";
98-
"`'"="0x34";
99-
","="0x36";
100-
"."="0x37";
101-
"/"="0x38";
102-
" "="0x2c";
63+
"a" = "0x04";
64+
"b" = "0x05";
65+
"c" = "0x06";
66+
"d" = "0x07";
67+
"e" = "0x08";
68+
"f" = "0x09";
69+
"g" = "0x0a";
70+
"h" = "0x0b";
71+
"i" = "0x0c";
72+
"j" = "0x0d";
73+
"k" = "0x0e";
74+
"l" = "0x0f";
75+
"m" = "0x10";
76+
"n" = "0x11";
77+
"o" = "0x12";
78+
"p" = "0x13";
79+
"q" = "0x14";
80+
"r" = "0x15";
81+
"s" = "0x16";
82+
"t" = "0x17";
83+
"u" = "0x18";
84+
"v" = "0x19";
85+
"w" = "0x1a";
86+
"x" = "0x1b";
87+
"y" = "0x1c";
88+
"z" = "0x1d";
89+
"1" = "0x1e";
90+
"2" = "0x1f";
91+
"3" = "0x20";
92+
"4" = "0x21";
93+
"5" = "0x22";
94+
"6" = "0x23";
95+
"7" = "0x24";
96+
"8" = "0x25";
97+
"9" = "0x26";
98+
"0" = "0x27";
99+
"!" = "0x1e";
100+
"@" = "0x1f";
101+
"#" = "0x20";
102+
"$" = "0x21";
103+
"%" = "0x22";
104+
"^" = "0x23";
105+
"&" = "0x24";
106+
"*" = "0x25";
107+
"(" = "0x26";
108+
")" = "0x27";
109+
"_" = "0x2d";
110+
"+" = "0x2e";
111+
"{" = "0x2f";
112+
"}" = "0x30";
113+
"|" = "0x31";
114+
":" = "0x33";
115+
"`"" = "0x34";
116+
"~" = "0x35";
117+
"<" = "0x36";
118+
">" = "0x37";
119+
"?" = "0x38";
120+
"-" = "0x2d";
121+
"=" = "0x2e";
122+
"[" = "0x2f";
123+
"]" = "0x30";
124+
"\" = "0x31";
125+
"`;" = "0x33";
126+
"`'" = "0x34";
127+
"," = "0x36";
128+
"." = "0x37";
129+
"/" = "0x38";
130+
" " = "0x2c";
131+
"F1" = "0x3a";
132+
"F2" = "0x3b";
133+
"F3" = "0x3c";
134+
"F4" = "0x3d";
135+
"F5" = "0x3e";
136+
"F6" = "0x3f";
137+
"F7" = "0x40";
138+
"F8" = "0x41";
139+
"F9" = "0x42";
140+
"F10" = "0x43";
141+
"F11" = "0x44";
142+
"F12" = "0x45";
143+
"TAB" = "0x2b";
144+
"KeyUp" = "0x52";
145+
"KeyDown" = "0x51";
146+
"KeyLeft" = "0x50";
147+
"KeyRight" = "0x4f";
148+
"KeyESC" = "0x29";
149+
"KeyBackSpace" = "0x2a";
150+
"KeyEnter" = "0x28";
103151
}
104152

105-
$vm = Get-View -ViewType VirtualMachine -Filter @{"Name"="^$($VMName)$"}
153+
$vm = Get-View -ViewType VirtualMachine -Filter @{"Name" = "^$($VMName)$" }
106154

107-
# Verify we have a VM or fail
108-
if(!$vm) {
155+
# Verify we have a VM or fail
156+
if (!$vm) {
109157
Write-host "Unable to find VM $VMName"
110158
return
111159
}
112160

113-
$hidCodesEvents = @()
114-
foreach($character in $StringInput.ToCharArray()) {
115-
# Check to see if we've mapped the character to HID code
116-
if($hidCharacterMap.ContainsKey([string]$character)) {
117-
$hidCode = $hidCharacterMap[[string]$character]
161+
#Code for -StringInput
162+
if ($StringInput) {
163+
$hidCodesEvents = @()
164+
foreach ($character in $StringInput.ToCharArray()) {
165+
# Check to see if we've mapped the character to HID code
166+
if ($hidCharacterMap.ContainsKey([string]$character)) {
167+
$hidCode = $hidCharacterMap[[string]$character]
118168

119-
$tmp = New-Object VMware.Vim.UsbScanCodeSpecKeyEvent
169+
$tmp = New-Object VMware.Vim.UsbScanCodeSpecKeyEvent
120170

121-
# Add leftShift modifer for capital letters and/or special characters
122-
if( ($character -cmatch "[A-Z]") -or ($character -match "[!|@|#|$|%|^|&|(|)|_|+|{|}|||:|~|<|>|?|*]") ) {
123-
$modifer = New-Object Vmware.Vim.UsbScanCodeSpecModifierType
124-
$modifer.LeftShift = $true
125-
$tmp.Modifiers = $modifer
171+
# Add leftShift modifer for capital letters and/or special characters
172+
if ( ($character -cmatch "[A-Z]") -or ($character -match "[!|@|#|$|%|^|&|(|)|_|+|{|}|||:|~|<|>|?|*]") ) {
173+
$modifer = New-Object Vmware.Vim.UsbScanCodeSpecModifierType
174+
$modifer.LeftShift = $true
175+
$tmp.Modifiers = $modifer
176+
}
177+
178+
# Convert to expected HID code format
179+
$hidCodeHexToInt = [Convert]::ToInt64($hidCode, "16")
180+
$hidCodeValue = ($hidCodeHexToInt -shl 16) -bor 0007
181+
182+
$tmp.UsbHidCode = $hidCodeValue
183+
$hidCodesEvents += $tmp
184+
185+
if ($DebugOn) {
186+
Write-Host "[StringInput] Character: $character -> HIDCode: $hidCode -> HIDCodeValue: $hidCodeValue"
187+
}
188+
}
189+
else {
190+
Write-Host "[StringInput] The following character `"$character`" has not been mapped, you will need to manually process this character"
191+
break
126192
}
127193

128-
# Convert to expected HID code format
129-
$hidCodeHexToInt = [Convert]::ToInt64($hidCode,"16")
194+
}
195+
}
196+
197+
#Code for -SpecialKeyInput
198+
if ($SpecialKeyInput) {
199+
if ($hidCharacterMap.ContainsKey([string]$SpecialKeyInput)) {
200+
$hidCode = $hidCharacterMap[[string]$SpecialKeyInput]
201+
$tmp = New-Object VMware.Vim.UsbScanCodeSpecKeyEvent
202+
$hidCodeHexToInt = [Convert]::ToInt64($hidCode, "16")
130203
$hidCodeValue = ($hidCodeHexToInt -shl 16) -bor 0007
131204

132205
$tmp.UsbHidCode = $hidCodeValue
133-
$hidCodesEvents+=$tmp
206+
$hidCodesEvents += $tmp
134207

135-
if($DebugOn) {
136-
Write-Host "Character: $character -> HIDCode: $hidCode -> HIDCodeValue: $hidCodeValue"
208+
if ($DebugOn) {
209+
Write-Host "[SpecialKeyInput] Character: $character -> HIDCode: $hidCode -> HIDCodeValue: $hidCodeValue"
137210
}
138-
} else {
139-
Write-Host "The following character `"$character`" has not been mapped, you will need to manually process this character"
211+
}
212+
else {
213+
Write-Host "[SpecialKeyInput] The following character `"$character`" has not been mapped, you will need to manually process this character"
140214
break
141215
}
142216
}
143217

144218
# Add return carriage to the end of the string input (useful for logins or executing commands)
145-
if($ReturnCarriage) {
219+
if ($ReturnCarriage) {
146220
# Convert return carriage to HID code format
147-
$hidCodeHexToInt = [Convert]::ToInt64("0x28","16")
221+
$hidCodeHexToInt = [Convert]::ToInt64("0x28", "16")
148222
$hidCodeValue = ($hidCodeHexToInt -shl 16) + 7
149223

150224
$tmp = New-Object VMware.Vim.UsbScanCodeSpecKeyEvent
151225
$tmp.UsbHidCode = $hidCodeValue
152-
$hidCodesEvents+=$tmp
226+
$hidCodesEvents += $tmp
153227
}
154228

155229
# Call API to send keystrokes to VM
156230
$spec = New-Object Vmware.Vim.UsbScanCodeSpec
157231
$spec.KeyEvents = $hidCodesEvents
158232
Write-Host "Sending keystrokes to $VMName ...`n"
159233
$results = $vm.PutUsbScanCodes($spec)
160-
}
234+
}

0 commit comments

Comments
 (0)