你能用VBA返回磁盘的卷GUID吗?

时间:2016-01-19 05:34:19

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

我遇到了另一篇帖子VBA Open a USB device using it's unique id in wmi  海报展示了通过VBA和WMI获取USB设备UID的方法,但我不知道如何用VBA从WMI中检索VolumeGUID。

如果特定设备与DeviceID匹配,我想获取DeviceID然后返回VolumeGUID(如果可能)。

这篇文章How to get the volume GUID似乎表明它是可能的,但它适用于C ++

我只是查询错误的WMI类吗?

1 个答案:

答案 0 :(得分:3)

我曾经让VBS这样做,你可以减少和修改它以便在VBA中使用。

strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

Volume
MountPoint


Sub Volume()
    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Volume") ' Windows XP and earlier:  not available.
    wscript.echo "[ Win32_Volume ] - " & colItems.Count & " items"
    For Each objItem in colItems
        ShowT "Access", objItem.Access
        ShowT "Automount", objItem.Automount
        ShowT "Availability", objItem.Availability
        ShowT "BlockSize", objItem.BlockSize
        ShowT "Capacity", objItem.Capacity
        ShowT "Caption", objItem.Caption
        ShowT "Compressed", objItem.Compressed
        ShowT "Description", objItem.Description
        ShowT "DeviceID", objItem.DeviceID
        ShowT "DirtyBitSet", objItem.DirtyBitSet
        ShowT "DriveLetter", objItem.DriveLetter
        ShowT "DriveType", objItem.DriveType
        ShowT "FileSystem", objItem.FileSystem
        ShowT "FreeSpace", objItem.FreeSpace
        ShowT "IndexingEnabled", objItem.IndexingEnabled
        ShowT "Label", objItem.Label
        ShowT "MaximumFileNameLength", objItem.MaximumFileNameLength
        ShowT "Name", objItem.Name
        ShowT "NumberOfBlocks", objItem.NumberOfBlocks
        ShowT "PNPDeviceID", objItem.PNPDeviceID
        ShowT "Purpose", objItem.Purpose
        ShowT "Status", objItem.Status
        ShowT "StatusInfo", objItem.StatusInfo
        ShowT "SerialNumber", objItem.SerialNumber
        ShowT "SupportsDiskQuotas", objItem.SupportsDiskQuotas
        ShowT "SupportsFileBasedCompression", objItem.SupportsFileBasedCompression
        wscript.echo "-----"
    Next
    wscript.echo vbCrlf & "====================" & vbCrlf
End Sub

Sub MountPoint()
    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_MountPoint")
    wscript.echo "[ Win32_MountPoint ] - " & colItems.Count & " items"
    For Each objItem in colItems
        ShowT "Directory", objItem.Directory
        ShowT "Volume", objItem.Volume
        wscript.echo "-----"
    Next
    wscript.echo vbCrlf & "====================" & vbCrlf
End Sub

Sub ShowT(s, obj)
    If Len(obj) > 0 Then Wscript.Echo vbTab & s & ": " & obj
End Sub