VMware Cloud Community
mwpreston
Hot Shot
Hot Shot

Discover USB Devices available for passthru

So here's a challenge that I seem to be needing a little bit of help with.

Basically I what I'm trying to do is trigger an alarm in vCenter when the Host USB Configuration has changed, once that alarm is triggered I want to run a PowerCLI or any script for that matter to detect if that USB config change had anything to do with a UPS that I have plugged into the host.  If it did (meaning the UPS was either unplugged and/or moved to a different USB Port) I want to go through the process of re-adding the UPS usb device through to the VM.

So, I've gotten as far as triggering and alarm, and determining the connection status of the UPS's USB connection.  What I can't seem to do is figure out how to re-add the usb device if it was moved to a different port.  I tried using ONYX to gen some code for me, however it appears I need to know what the device.backing.deviceName is - something along the lines of "path:2/0/7 version:2" - which would obviously change depeding on what usb port it was plugged into...

So any help on either adding a USB device to a VM through PowerCLI or vCO or any sort of CLI fashion would be great

or

if at the very least I could poll the host's USB devices to see what was connected to what port that might get me through as well..

make sense?

Thanks,

Mike

Reply
0 Kudos
13 Replies
LucD
Leadership
Leadership

Do you have the PID and the VID for that USB device ?

If yes, include that in the autoconnect pattern, together with the HostId, in the VirtualUSBUSBBackingInfo spec.

Add the device in this way to the desired VM, and ESXi will connect the correct USB device (when it finds it).

You could also wait for Conrad's session and see how he did it :smileydevil:


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
mwpreston
Hot Shot
Hot Shot

Haha - well Conrad says he's just gonna tell me to shut up!!! Smiley Happy

Thanks for the help...

So the following is the code that Onyx generated

# ------- ReconfigVM_Task -------
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.changeVersion = "2013-05-02T20:01:06.056633Z"
$spec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec[] (1)
$spec.deviceChange[0] = New-Object VMware.Vim.VirtualDeviceConfigSpec
$spec.deviceChange[0].operation = "add"
$spec.deviceChange[0].device = New-Object VMware.Vim.VirtualUSB
$spec.deviceChange[0].device.key = -100
$spec.deviceChange[0].device.backing = New-Object VMware.Vim.VirtualUSBUSBBackingInfo
$spec.deviceChange[0].device.backing.deviceName = "path:2/0/7 version:2"
$spec.deviceChange[0].device.connectable = New-Object VMware.Vim.VirtualDeviceConnectInfo
$spec.deviceChange[0].device.connectable.startConnected = $true
$spec.deviceChange[0].device.connectable.allowGuestControl = $false
$spec.deviceChange[0].device.connectable.connected = $true
$spec.deviceChange[0].device.connected = $false
$_this = Get-View -Id 'VirtualMachine-vm-208370'
$_this.ReconfigVM_Task($spec)

Let's just say my PID is 111 and my VID is 222 - I have the real ones, just can't find where I stored them right now Smiley Happy  Where would I go about adding these autoconnectpatterns in this code?  I asume I need to remove the device.backing.deviceName line?  Also, is hostID the same as moref?  and how does that play in?

Thanks for your help Luc Smiley Happy

Mike

Reply
0 Kudos
LucD
Leadership
Leadership

You would be replacing this line

$spec.deviceChange[0].device.backing.deviceName = "path:2/0/7 version:2"

That value contains the actual path, which you don't know since the USB device was possibly moved.

To specify the USB device without knowing the actual path, you use the VID and the PID in that pattern.

That way the ESXi will look for that specific USB device.

The only problem is when you have 2 or more identical USB devices connected.

I don't know what ESXi will do in that case. Perhaps select the 1st one it encounters ?

Btw the explanation of such a pattern is specified at the top of the VirtualUSBUSBBackingInfo object description in the SDK.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
mwpreston
Hot Shot
Hot Shot

Awesome - thanks again Luc -

So I have it working now by passing pid and vid - all works great, even as I move it to different ports.

As for hostId I'm not sure where to find that information - the doc specifies a big long id - the one i'm familiar with is HostSystem-host-###### - which will not work when passed - is there another hostId I should be using?  And where might I get access to that?

Reply
0 Kudos
CRad14
Hot Shot
Hot Shot

Hah Perfect LucD!!!! Forum heckling suits you Smiley Happy

Conrad www.vnoob.com | @vNoob | If I or anyone else is helpful to you make sure you mark their posts as such! 🙂
Reply
0 Kudos
mwpreston
Hot Shot
Hot Shot

So I found the proper hostId inside of the vmware.log file of the VM - now just need to figure out how to get that programitically...

Reply
0 Kudos
mwpreston
Hot Shot
Hot Shot

Nevermind guys!  I found it - Thanks for all your help with this Luc - works like a charm - found it at summary.hardware.uuid - just needs a little bit of formating to pass through to the autoconnect string!

Thanks,

Mike

Reply
0 Kudos
LucD
Leadership
Leadership

Nice, glad you found it.

I might do a post on the creation of USB devices in VMs


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
mwpreston
Hot Shot
Hot Shot

Well I'm looking forward to it...

If it helps - this is the complete script I ended up using ...

Add-PSSnapin VMware.VIMAutomation.Core
$vcserver = "vcenterserver"
$vcusername = "username"
$vcpassword = "password"
$myhost = "Host that USB is attached to"
$myvm = "VM which to pass USB through to"
$pid = "PID of USB Device"
$vid = "VID of USB Device"
Connect-VIserver $vcserver -user $vcusername -pass $vcpassword
#get id of VM
$vm = get-vm $myvm
$vmid = $vm.ID
#get host uuid from BIOS
$vmhost = get-vmhost $myhost
$vmhostId =  $vmhost.ExtensionData.Hardware.SystemInfo.Uuid
#reformat vmhostID to the proper format for autoconnect string
$vmhostd = $vmhostid.Replace("-","")
$section1 = $vmhostid.substring(0,16)
$section2 = $vmhostid.substring(16)
$newsec1 = (&{for ($i = 0;$i -lt $section1.length;$i += 2)
   {
     $section1.substring($i,2)
   }}) -join '\ '
$newsec2 = (&{for ($i = 0;$i -lt $section2.length;$i += 2)
   {
     $section2.substring($i,2)
   }}) -join '\ '
$hostId = "$newsec1-$newsec2"
#create usb device and add it to the VM.
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec[] (1)
$spec.deviceChange[0] = New-Object VMware.Vim.VirtualDeviceConfigSpec
$spec.deviceChange[0].operation = "add"
$spec.deviceChange[0].device = New-Object VMware.Vim.VirtualUSB
$spec.deviceChange[0].device.key = -100
$spec.deviceChange[0].device.backing = New-Object VMware.Vim.VirtualUSBUSBBackingInfo
$spec.deviceChange[0].device.backing.deviceName = "pid:$pid vid:$vid hostId:$hostId"
$spec.deviceChange[0].device.connectable = New-Object VMware.Vim.VirtualDeviceConnectInfo
$spec.deviceChange[0].device.connectable.startConnected = $true
$spec.deviceChange[0].device.connectable.allowGuestControl = $false
$spec.deviceChange[0].device.connectable.connected = $true
$spec.deviceChange[0].device.connected = $false
$_this = Get-View -Id "$vmid"
$_this.ReconfigVM_Task($spec)
Reply
0 Kudos
thomas_root1
Enthusiast
Enthusiast

Thanks for that great script!

Is there also a way to pass trough usb devices from a client?
Unfortunatly, Onyx doesn't work with this operation in vSphere-Client Smiley Sad

Best Regards,

Thomas

Reply
0 Kudos
mwpreston
Hot Shot
Hot Shot

Not that I know of - I think the passthru of USB client devices only works when using the console.

If you ever do find a way post back here and let me know Smiley Happy

Mike

Reply
0 Kudos
waterZhen
Contributor
Contributor

Thanks a lot for your greate script, It's so helpfull~

But one thing I still fell confused.  Where should I get my PID and VID?

wish to get your replying, thanks again~

water

Reply
0 Kudos
laheller
Contributor
Contributor

Hi!

In what format to specify the VID and PID to attach USB device to VM? Decimal or hexadecimal string?

Is the hostId really required when VID and PID are specified?

BR,

Ladislav

Reply
0 Kudos