Retrieve VolumeGUIDs via Win32_PnPEntity using VBA

雨燕双飞 提交于 2019-12-09 23:20:15

问题


Is there a way to list the VolumeGUID's from a USB Drive based on the DeviceID from Win32_PnPEntity

I am able to retrieve all the VolumeGUID's from Win32_Volume. But I do not know which ones are associated with the USB Drive other than the Drive Letter.

I use the following to get USB Names and DeviceID's

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

I use the following code to get all the VolumeGUID's:

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

I just dont know how to relate the DeviceID from Win32_PnPEntity the VolumeGUID's of the device

来源:https://stackoverflow.com/questions/34873073/retrieve-volumeguids-via-win32-pnpentity-using-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!