Virtual PC Guy's Blog

-- Ben Armstrong, Virtualization Program Manager

Talking about core virtualization at Microsoft (Hyper-V, Virtual PC and Virtual Server).

Script to attach a USB device to a virtual machine [VPC]

Script to attach a USB device to a virtual machine [VPC]

  • Comments 8


A couple of people have asked me how to automate attaching a USB device to a Windows Virtual PC virtual machine, so here is a PowerShell script to do just that:

# Connect to Virtual PC
$vpc = new-object -com VirtualPC.Application
 
# Get VM name
$vmName = Read-host "Specify the name of the virtual machine that you want to use"
 
# List available USB devices
write-host "The following USB devices are available:"
$vpc.USBDeviceCollection | select -ExpandProperty DeviceString
 
# Get the USB device name
$usb = Read-host "Enter the name of the USB device that you want to connect to the virtual machine"
 
# Get the VM object
$vm = $vpc.findVirtualMachine($vmName)
 
# Get the USB object
$usbDevice = $vpc.USBDeviceCollection | ? {$_.DeviceString -eq $usb} | select -first 1
 
# Attach the device - this will fail if the VM is not running
$vm.AttachUSBDevice($usbDevice)

Note that I am using the “DeviceString” of the USB device – which is not guaranteed to be unique.  So what I do is just grab the first one that matches.

Cheers,
Ben

Attachment: AttachUSBDevice.zip
Leave a Comment
  • Please add 8 and 1 and type the answer here:
  • Post
  • Can you write a script that would work for a hyper-v vm?

  • You can't connect USB devices to Hyper-V

  • @Dave, not as such.  But if it's a USB drive, you can attach the whole drive as a SCSI device.

  • @Richard, but you're connecting the USB drive to the host, not a VM. So why would you need a script for that?

  • I tried the script and discovered (to no great surprise) that it requires integration components to be installed. I'm trying to test out several Win7 install/deployment methods and would love a way to configure a second (virtual) DVD drive, a virtual floppy drive, or some other type of storage attached to the virtual machine that would allow the Windown setup process to access the xml file. Any idea on how to get that done? Have I overlooked something obvious? I've got 22 browser tabs open, all dealing with ways to get this deployment done, and this script was one of the my last attempts.

  • For other looking, I found using vbscript much easier than getting powerscript running. Here is an excelent example I followed:

    social.technet.microsoft.com/.../031e26d1-7cd6-4d35-ae5a-4d8457631386

    Also, if unfamiliar with vbscript, the free trial program VbsEdit is great for debugging and understanding the objects and properties used in the example.

  • I am not a programmer.

    What do i do with the script ?

    (I have visual studio if necessary)

  • Hey guys, what do I have to do with this script in order use it? Do i just run it with Windows Power Shell? Do I have to imput the name of the device in the script?

    Thanks.

Page 1 of 1 (8 items)