Get VID/PID of the connected usb devices through VBScript
I'm looking for a VBScript which can store VID/PID of connected USB devices in a varia开发者_开发百科ble. So What is the VBScript for doing this?
You could get it by using WMI but there isn't a dynamic class for connected USB devices in WMI.
Need to some relation info usb devices with plugged devices to get plugged ones.
So, consider this example:
Option Explicit
Dim oWMISrv, collDvcs, collUSBDvcs, iUSBDvc , iDvc, sDvcID, sPID, sVID
Function ReplaceX(ByVal sValue, ByVal sPattern, ByVal sNValue)
Dim oReg : Set oReg = New RegExp
oReg.Pattern = sPattern
ReplaceX = oReg.Replace(sValue, sNValue)
Set oReg = Nothing
End Function
Set oWMISrv = GetObject("winmgmts:\\.\root\cimv2")
Set collDvcs = oWMISrv.ExecQuery("Select * From Win32_USBControllerDevice")
For Each iDvc In collDvcs
If InStr(iDvc.Dependent, "VID_") Then ' Except keychain drives
sDvcID = ReplaceX(iDvc.Dependent, ".*""(.*)""", "$1")
sPID = ReplaceX(sDvcID, ".*PID_([^\\]*).*", "$1")
sVID = ReplaceX(sDvcID, ".*VID_([^&]*).*", "$1")
Set collUSBDvcs = oWMISrv.ExecQuery _
("Select * From Win32_PnPEntity Where DeviceID = '" & sDvcID & "'")
For Each iUSBDvc in collUSBDvcs
Wscript.Echo "Name : "& iUSBDvc.Description
Wscript.Echo "VID : "& sVID
Wscript.Echo "PID : "& sPID
Wscript.Echo String(50, "-")
Next
Set collUSBDvcs = Nothing
End If
Next
Set collDvcs = Nothing
Set oWMISrv = Nothing
精彩评论