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

发布于 2024-12-04 02:02:47 字数 79 浏览 0 评论 0原文

我正在寻找一个 VBScript,它可以将连接的 USB 设备的 VID/PID 存储在变量中。那么用于执行此操作的 VBScript 是什么?

I'm looking for a VBScript which can store VID/PID of connected USB devices in a variable. So What is the VBScript for doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

篱下浅笙歌 2024-12-11 02:02:47

您可以使用 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文