|
23 | 23 | Set-VMKeystrokes -VMName $VM -StringInput "root" -DebugOn $true
|
24 | 24 | #>
|
25 | 25 | 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 |
30 | 30 | )
|
31 | 31 |
|
32 | 32 | # Map subset of USB HID keyboard scancodes
|
33 | 33 | # https://gist.github.com/MightyPork/6da26e382a7ad91b5496ee55fdc73db2
|
34 | 34 | $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"; |
103 | 103 | }
|
104 | 104 |
|
105 |
| - $vm = Get-View -ViewType VirtualMachine -Filter @{"Name"="^$($VMName)$"} |
| 105 | + $vm = Get-View -ViewType VirtualMachine -Filter @{"Name" = "^$($VMName)$" } |
106 | 106 |
|
107 | 107 | # Verify we have a VM or fail
|
108 |
| - if(!$vm) { |
| 108 | + if (!$vm) { |
109 | 109 | Write-host "Unable to find VM $VMName"
|
110 | 110 | return
|
111 | 111 | }
|
112 | 112 |
|
113 | 113 | $hidCodesEvents = @()
|
114 |
| - foreach($character in $StringInput.ToCharArray()) { |
| 114 | + foreach ($character in $StringInput.ToCharArray()) { |
115 | 115 | # Check to see if we've mapped the character to HID code
|
116 |
| - if($hidCharacterMap.ContainsKey([string]$character)) { |
| 116 | + if ($hidCharacterMap.ContainsKey([string]$character)) { |
117 | 117 | $hidCode = $hidCharacterMap[[string]$character]
|
118 | 118 |
|
119 | 119 | $tmp = New-Object VMware.Vim.UsbScanCodeSpecKeyEvent
|
120 | 120 |
|
121 | 121 | # 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 "[!|@|#|$|%|^|&|(|)|_|+|{|}|||:|~|<|>|?|*]") ) { |
123 | 123 | $modifer = New-Object Vmware.Vim.UsbScanCodeSpecModifierType
|
124 | 124 | $modifer.LeftShift = $true
|
125 | 125 | $tmp.Modifiers = $modifer
|
126 | 126 | }
|
127 | 127 |
|
128 | 128 | # Convert to expected HID code format
|
129 |
| - $hidCodeHexToInt = [Convert]::ToInt64($hidCode,"16") |
| 129 | + $hidCodeHexToInt = [Convert]::ToInt64($hidCode, "16") |
130 | 130 | $hidCodeValue = ($hidCodeHexToInt -shl 16) -bor 0007
|
131 | 131 |
|
132 | 132 | $tmp.UsbHidCode = $hidCodeValue
|
133 |
| - $hidCodesEvents+=$tmp |
| 133 | + $hidCodesEvents += $tmp |
134 | 134 |
|
135 |
| - if($DebugOn) { |
| 135 | + if ($DebugOn) { |
136 | 136 | Write-Host "Character: $character -> HIDCode: $hidCode -> HIDCodeValue: $hidCodeValue"
|
137 | 137 | }
|
138 |
| - } else { |
| 138 | + } |
| 139 | + else { |
139 | 140 | Write-Host "The following character `"$character`" has not been mapped, you will need to manually process this character"
|
140 | 141 | break
|
141 | 142 | }
|
142 | 143 | }
|
143 | 144 |
|
144 | 145 | # Add return carriage to the end of the string input (useful for logins or executing commands)
|
145 |
| - if($ReturnCarriage) { |
| 146 | + if ($ReturnCarriage) { |
146 | 147 | # Convert return carriage to HID code format
|
147 |
| - $hidCodeHexToInt = [Convert]::ToInt64("0x28","16") |
| 148 | + $hidCodeHexToInt = [Convert]::ToInt64("0x28", "16") |
148 | 149 | $hidCodeValue = ($hidCodeHexToInt -shl 16) + 7
|
149 | 150 |
|
150 | 151 | $tmp = New-Object VMware.Vim.UsbScanCodeSpecKeyEvent
|
151 | 152 | $tmp.UsbHidCode = $hidCodeValue
|
152 |
| - $hidCodesEvents+=$tmp |
| 153 | + $hidCodesEvents += $tmp |
153 | 154 | }
|
154 | 155 |
|
155 | 156 | # Call API to send keystrokes to VM
|
|
0 commit comments