Skip to content

Commit 3290db1

Browse files
committed
VMKeyStrokes: fix indent (using Visual Code Formmater)
1 parent 360d843 commit 3290db1

File tree

1 file changed

+85
-84
lines changed

1 file changed

+85
-84
lines changed

powershell/VMKeystrokes.ps1

Lines changed: 85 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -23,133 +23,134 @@
2323
Set-VMKeystrokes -VMName $VM -StringInput "root" -DebugOn $true
2424
#>
2525
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
26+
[Parameter(Mandatory = $true)][String]$VMName,
27+
[Parameter(Mandatory = $true)][String]$StringInput,
28+
[Parameter(Mandatory = $false)][Boolean]$ReturnCarriage,
29+
[Parameter(Mandatory = $false)][Boolean]$DebugOn
3030
)
3131

3232
# Map subset of USB HID keyboard scancodes
3333
# https://gist.github.com/MightyPork/6da26e382a7ad91b5496ee55fdc73db2
3434
$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";
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";
103103
}
104104

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

107107
# Verify we have a VM or fail
108-
if(!$vm) {
108+
if (!$vm) {
109109
Write-host "Unable to find VM $VMName"
110110
return
111111
}
112112

113113
$hidCodesEvents = @()
114-
foreach($character in $StringInput.ToCharArray()) {
114+
foreach ($character in $StringInput.ToCharArray()) {
115115
# Check to see if we've mapped the character to HID code
116-
if($hidCharacterMap.ContainsKey([string]$character)) {
116+
if ($hidCharacterMap.ContainsKey([string]$character)) {
117117
$hidCode = $hidCharacterMap[[string]$character]
118118

119119
$tmp = New-Object VMware.Vim.UsbScanCodeSpecKeyEvent
120120

121121
# Add leftShift modifer for capital letters and/or special characters
122-
if( ($character -cmatch "[A-Z]") -or ($character -match "[!|@|#|$|%|^|&|(|)|_|+|{|}|||:|~|<|>|?|*]") ) {
122+
if ( ($character -cmatch "[A-Z]") -or ($character -match "[!|@|#|$|%|^|&|(|)|_|+|{|}|||:|~|<|>|?|*]") ) {
123123
$modifer = New-Object Vmware.Vim.UsbScanCodeSpecModifierType
124124
$modifer.LeftShift = $true
125125
$tmp.Modifiers = $modifer
126126
}
127127

128128
# Convert to expected HID code format
129-
$hidCodeHexToInt = [Convert]::ToInt64($hidCode,"16")
129+
$hidCodeHexToInt = [Convert]::ToInt64($hidCode, "16")
130130
$hidCodeValue = ($hidCodeHexToInt -shl 16) -bor 0007
131131

132132
$tmp.UsbHidCode = $hidCodeValue
133-
$hidCodesEvents+=$tmp
133+
$hidCodesEvents += $tmp
134134

135-
if($DebugOn) {
135+
if ($DebugOn) {
136136
Write-Host "Character: $character -> HIDCode: $hidCode -> HIDCodeValue: $hidCodeValue"
137137
}
138-
} else {
138+
}
139+
else {
139140
Write-Host "The following character `"$character`" has not been mapped, you will need to manually process this character"
140141
break
141142
}
142143
}
143144

144145
# Add return carriage to the end of the string input (useful for logins or executing commands)
145-
if($ReturnCarriage) {
146+
if ($ReturnCarriage) {
146147
# Convert return carriage to HID code format
147-
$hidCodeHexToInt = [Convert]::ToInt64("0x28","16")
148+
$hidCodeHexToInt = [Convert]::ToInt64("0x28", "16")
148149
$hidCodeValue = ($hidCodeHexToInt -shl 16) + 7
149150

150151
$tmp = New-Object VMware.Vim.UsbScanCodeSpecKeyEvent
151152
$tmp.UsbHidCode = $hidCodeValue
152-
$hidCodesEvents+=$tmp
153+
$hidCodesEvents += $tmp
153154
}
154155

155156
# Call API to send keystrokes to VM

0 commit comments

Comments
 (0)