Is there a way to identify which USB port a mass storage drive uses?

Viewed 151

I am trying to identify which port a USB mass storage is connected to. Here is my scenario. I have a vbscript program that monitors the USB mass storage drive plug-ins and do some routines on them and its process flow needs to know which port the drive was connected to.

So far I am not able to find a link between the USB events, the identified usb mass storage drive and the port on the computer (for eg., Location - Port_#0001.Hub_#0006 as shown in Windows device manager->usb controllers->USB mass storage). My search was mainly using 'VbsEdit' tool by connecting a storage device and stepping through the available snippets.

Sub Monitor_usb_storage_events()
    Dim WsShell,strComputer,wmi, wmiEvent,usb,objDisk,fileSys,colDisks,Drive
    Set WsShell = CreateObject("WScript.Shell")
    strComputer = "." 
    Set wmi = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set wmiEvent = wmi.ExecNotificationQuery("select * from __InstanceOperationEvent within 1 where TargetInstance ISA 'Win32_PnPEntity' and TargetInstance.Description='USB Mass Storage Device'")
    While True
    Set usb = wmiEvent.NextEvent()
    Select Case usb.Path_.Class
        Case "__InstanceCreationEvent" 
            'WScript.Echo("USB device found")
            Set colDisks = wmi.ExecQuery _
            ("SELECT * FROM Win32_LogicalDisk")
            For Each objDisk in colDisks
              If objDisk.DriveType = 2 Then
                 Set fileSys = CreateObject("Scripting.FileSystemObject")
                 For Each Drive In fileSys.Drives
                    If Drive.IsReady Then
                       '*Need port number where this drive is connected*
                    End If   
                 Next
              End If   
           Next
        Case "__InstanceDeletionEvent" 
            'WScript.Echo("USB device removed")
        Case "__InstanceModificationEvent" 
            'WScript.Echo("USB device modified")
    End Select
    Wend
End Sub
1 Answers

Here is a script I've had a for while that queries the Win32_USBControllerDevice and then returns the dependent devices.

Option Explicit
Dim strComputer: strComputer = "."

Dim objWMIService: Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Dim colDevices: Set colDevices = objWMIService.ExecQuery _
   ("Select * From Win32_USBControllerDevice")
Dim objDevice

For Each objDevice in colDevices
   Dim strDeviceName: strDeviceName = objDevice.Dependent
   strDeviceName = Replace(strDeviceName, """", "")
   Dim arrDeviceNames: arrDeviceNames = Split(strDeviceName, "=")
   strDeviceName = arrDeviceNames(1)
   Dim colUSBDevices: Set colUSBDevices = objWMIService.ExecQuery _
       ("Select * From Win32_PnPEntity Where DeviceID = '" & strDeviceName & "'")
   Dim objUSBDevice

   For Each objUSBDevice in colUSBDevices
       Wscript.Echo objUSBDevice.Description
       WScript.Echo objUSBDevice.PnPDeviceID
   Next   
Next

If you combined this with your __InstanceOperationEvent WMI query you should be able to modify the above script to query the DeviceID returned in the matched Win32_PnPEntity instance.

You can get the DeviceID in your above script using:

strDeviceName = usb.TargetInstance.DeviceID
Related