使用VBA通过Win32_PnPEntity检索VolumeGUID

时间:2016-01-19 09:32:29

标签: vba excel-vba wmi wmi-query excel

有没有办法根据Win32_PnPEntity中的DeviceID从USB驱动器列出VolumeGUID

我可以从Win32_Volume中检索所有VolumeGUID。但我不知道哪些与驱动器号以外的USB驱动器相关联。

我使用以下内容获取USB名称和DeviceID

Sub USBInfo()
    Dim strComputer     As String
    Dim strDeviceName   As String
    Dim objWMIService   As Object
    Dim colControllers  As Object
    Dim objController   As Object
    Dim colUSBDevices   As Object
    Dim objUSBDevice    As Object
    Dim i               As Integer
    On Error Resume Next

    strComputer = "."

    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colControllers = objWMIService.ExecQuery("Select * From Win32_USBControllerDevice")

    For Each objController In colControllers

       strDeviceName = Replace(objController.Dependent, Chr(34), "")
       strDeviceName = Right(strDeviceName, Len(strDeviceName) - WorksheetFunction.Find("=", strDeviceName))

       Set colUSBDevices = objWMIService.ExecQuery("Select * From Win32_PnPEntity Where DeviceID = '" & strDeviceName & "'")

       For Each objUSBDevice In colUSBDevices
            With ShUSB
                .Cells(i, 1).Value = objUSBDevice.Name
                .Cells(i, 2).Value = objUSBDevice.DeviceID
            End With
            i = i + 1
        Next
    Next
    DeviceInfo.Columns("A:D").AutoFit
End Sub

我使用以下代码获取所有VolumeGUID':

Sub VolumeInfo()
    Dim strComputer     As String
    Dim strDeviceName   As String
    Dim objWMIService   As Object
    Dim colItems   As Object
    Dim objItem   As Object
    Dim i               As Integer
    On Error Resume Next

    strComputer = "."

    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Volume")

    For Each objItem In colItems
        With DeviceInfo
            .Cells(i, 3).Value = "DeviceID: " & objItem.DeviceID
            .Cells(i, 4).Value = "SerialNumber: " & objItem.SerialNumber
        End With
        i = i + 1
    Next
    DeviceInfo.Columns("A:D").AutoFit
End Sub

我只是不知道如何将WinID_PnPEntity中的DeviceID与设备的VolumeGUID相关联

0 个答案:

没有答案