确认USB设备已连接到特定的USB端口

时间:2017-07-01 14:49:26

标签: vbscript cmd

我正在尝试运行下面的Visual Basic脚本,它通过CMD执行。该脚本效果很好,但它只涵盖了一个非常基本的目的。我正在尝试通过检查设备名称或VID / PID来验证USB1端口是否连接了特定设备。有人可以帮我一个脚本,可以验证某些USB端口是否连接了特定的设备?

在将要运行的多台计算机上的环境相同,例如,我想验证USB端口1是否包含名称中包含“Scanner”的设备,或者检查设备的ID。这可能吗?

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

0 个答案:

没有答案