PowerCLI has a function to remove a USB device from a host, Remove-UsbDevice. However, what if you need to add a usb device to a VM. Well you can with a bit of PowerCLI magic. This solution assumes the device you wish to add remains in the same USB port and that the VM will be on the same host as the USB device of interest. If you wish you can hack around to insert the host ID and change from path: option to pid: and vid: options. You can read more about the option at VMware pubs.
Below is a simple function for Add-UsbDevice. It takes a VM name (wildcard if you know it will be unique for ease) and then the path is normally in the form of BUS/0/DEVICE i.e. “2/0/5”.
1 #
2 # Simple to use once connected to the server of interest (not vCenter)
3 # Set-UsbDevice -Name "My VM Name" -UsbPath "2/0/5"
4 #
5 # Find out the UsbPath from lsusb - typically BUS/0/DEVICE - but not guaranteed!
6 #
7
8 Function Set-UsbDevice
9 {
10
11 # We need both parameters
12 [CmdletBinding()]
13 Param(
14 [Parameter(Mandatory=$True,Position=1)]
15 [string]$Name,
16
17 [Parameter(Mandatory=$True)]
18 [string]$UsbPath
19 )
20
21 #Get our VM then then ID
22 $VM = Get-VM $Name
23 $Id = $VM.Id
24
25 #Create a new Virtual Machine Configuration Specification
26 $newSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
27 $newSpec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec[] (1)
28 $newSpec.deviceChange[0] = New-Object VMware.Vim.VirtualDeviceConfigSpec
29 $newSpec.deviceChange[0].operation = "add"
30 $newSpec.deviceChange[0].device = New-Object VMware.Vim.VirtualUSB
31 $newSpec.deviceChange[0].device.key = -100
32 $newSpec.deviceChange[0].device.backing = New-Object VMware.Vim.VirtualUSBUSBBackingInfo
33 $newSpec.deviceChange[0].device.backing.deviceName = "path:$UsbPath host:localhost"
34 $newSpec.deviceChange[0].device.connectable = New-Object VMware.Vim.VirtualDeviceConnectInfo
35 $newSpec.deviceChange[0].device.connectable.startConnected = $true
36 $newSpec.deviceChange[0].device.connectable.allowGuestControl = $false
37 $newSpec.deviceChange[0].device.connectable.connected = $true
38 $newSpec.deviceChange[0].device.connected = $false
39
40 $_this = Get-View -Id "$Id"
41 $_this.ReconfigVM_Task($newSpec)
42
43 }
If you want to find out the path on your host then using the shell you can use lsusb:
~ # lsusb
Bus 02 Device 06: ID 0424:4030 Standard Microsystems Corp.
Bus 02 Device 05: ID 03f0:1fe0 Hewlett-Packard
Bus 02 Device 04: ID 0fde:ca04
Bus 02 Device 03: ID 0424:2660 Standard Microsystems Corp.
Bus 02 Device 02: ID 8087:8000 Intel Corp.
Bus 02 Device 01: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 01 Device 04: ID 0a12:0001 Cambridge Silicon Radio
Bus 01 Device 03: ID 0403:6001 Future Technology Devices
Bus 01 Device 02: ID 8087:8008 Intel Corp.
Bus 01 Device 01: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 03 Device 01: ID 1d6b:0001 Linux Foundation 1.1 root hub
Use the command like this in PowerCLI once you have connected to the Host:
Set-UsbDevice -name "vSphere *" -UsbPath "2/0/5"