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