Easily Access a USB Device from Within a Hyper-V Virtual Machine
Why I Needed to Access a USB Device from a VM
This article came about due to the fact that I needed a way to test how well various antivirus products block the execution of a script embedded in a USB device (which itself emulates a keyboard). The USB device in question is designed to inject keypresses into the Operating System (OS) upon device insertion. Of course, I didn't want to repeatedly install and then uninstall a variety of antivirus products on my own laptop in order to perform the tests.
As Hyper-V is included with Windows 10 Pro, I decided to use a Hyper-V Virtual Machine (VM) for the testing such that I could create a different VM instance for each antivirus product. However, I would need to be able to attach the physical USB device to the VM.
Not only did I need to attach a USB device (physically inserted into my laptop) to the running VM, ideally I needed the USB port to be attached to the VM prior to USB device insertion, such that the automatic injection of keypresses is directed to the VM, rather than the host OS.
Surprisingly, it turns out that this is really simple to achieve and I have shared the instructions below.
Note: There are alternative methods, but this one is simple and suits my use-case nicely.
Instructions
a) If your target Windows 10 VM is currently running in Hyper-V, shut it down.
b) In Hyper-V Manager, right-click on the name of the host machine and then select 'Hyper-V Settings...':
c) Within the 'Server' → 'Enhanced Session Mode Policy' section, ensure 'Allow enhanced session mode' is enabled:
d) Within the 'User' → 'Enhanced Session Mode' section, ensure 'Use enhanced session mode' is enabled, and then click 'OK' to save the changes:
e) The target VM should be off at this point, so start the VM running by right-clicking on the VM instance and selecting 'Start' (do not select 'Connect...'):
f) Shortly after the VM has started up, right-click on the VM instance and select 'Edit Session Settings...':
g) Click 'Show Options':
h) Select 'Local Resources' and then click 'More...':
i) Expand 'Other supported Plug and Play (PnP) devices' and then enable 'Devices that I plug in later', before clicking 'OK' to save the changes:
j) Select 'Display' and then enable 'Save my settings for future connections to this virtual machine' before clicking 'Connect':
That's it! From now on, if you have an active session running with the VM, any USB device that becomes attached to the physical machine will be seen has having become attached to the VM.
Senior Software Engineer
1 年Hi Sean Burns, the information above are so useful, did you try it on Ubuntu VM too?
21k+ followers | Recruiting IoT, Electronics, FW/SW, (C/C++), FPGAs, Electrical, Mechanical, Avionics, ML/AI, FS Skill-Sets | 97% Placement Retention | Quantity ??
4 年Nice one Sean Burns