通过VBScript获取连接的USB设备的VID / PID

时间:2011-09-10 11:42:59

标签: vbscript usb device

我正在寻找一个可以将连接的USB设备的VID / PID存储在变量中的VBScript。那么这样做的VBScript是什么?

1 个答案:

答案 0 :(得分:4)

你可以通过使用WMI来获得它,但是WMI中没有连接USB设备的动态类。
需要一些关联信息usb带有插入设备的设备才能获得插件。
所以,请考虑这个例子:

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