Automatically Mounting USB Passthrough Drives for Windows Backup in a Hyper-V VM

We recently have started deploying Hyper-V in addition to ESXI. One thing we immediately noticed was that USB passthrough was going to be a challenge. As we worked through the problem we found that we could pass a USB Hard Drive through, but as we rotated through the set, our disks did not reconnect. Immediately my mind turned to Powershell. “There must be a way”, I thought, “to remount these disks on the hypervisor before the backup tries to run.” Indeed I was right. With the release of Server 2012 Microsoft has added some great Powershell management cmdlets to manage both VMs and disks.

Below is the script that we have running just before the backup kicks off. Note that you will need to edit the serial numbers appropriately so you can mount your disks. I’m sure this will be able to be dolled up to include a dynamic approach to the controller location (we even batted around the idea of a dedicated backup SCSI controller on the machine). Pay specific attention to the Controller Number, Type and location and the VMName.

Note: As we have had this running for a while we have noticed that this script prevents the machine from starting up if the USB disk last mounted is not present. We are working on a solution to that problem.

 

remove-vmharddiskdrive -vmname YOURVM -controllertype scsi -controllernumber 0 -controllerlocation 1 -ErrorAction SilentlyContinue
if ($diskarray = get-disk | where-object {$_.serialnumber -in “InsertNewHere”} | select number,isoffline,serialnumber){
$disknumber=$diskarray[0].number
$diskserial=$diskarray[0].serialnumber
if ($diskarray[0].isoffline -eq $false){set-disk -number $disknumber -IsOffline $true}
Add-VMHardDiskDrive -vmname YOURVMNAME -ControllerType scsi -controllernumber 0 -controllerlocation 1 -Passthru -DiskNumber $disknumber
write-output “Mounting drive with serial number $diskserial”
}
else {write-output “Not Mounting anything because no valid drives were found”}

 

Tags: , , , , ,

No comments yet.

Leave a Reply