获取USB存储驱动器字母VBSCript

时间:2015-02-17 07:31:29

标签: vbscript usb-drive

这是显示连接的USB存储设备列表。

On Error Resume Next
    strComputer = "."
    Dim oFSO, oDrive
    Set oFSO = WScript.CreateObject("Scripting.FileSystemObject")
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive WHERE InterfaceType = 'USB'")
    For Each objItem in colItems
      Wscript.Echo objItem.Caption
    Next

我需要获取驱动器号,但上面的代码是不可能的。关于colItems的Drive Letter没有任何属性。我可以使用另一个支持Drive Letter的脚本,这里是:

Dim oFSO, oDrive
Const USBDRIVE=1
Set oFSO = WScript.CreateObject("Scripting.FileSystemObject")
For Each oDrive In oFSO.Drives
If oDrive.DriveType = USBDRIVE And oDrive.DriveLetter <> "A" Then
WScript.Echo oDrive.DriveLetter
End If
Next

但是在这个脚本中我不能使用Caption Property,因为它不受支持:(我需要结合这两个脚本来获取每个USB存储标题和信件。我怎么能这样做?可能吗?

2 个答案:

答案 0 :(得分:3)

尝试此代码:

'Show drive letters associated with each
ComputerName = "."
Set wmiServices  = GetObject ( _
    "winmgmts:{impersonationLevel=Impersonate}!//" _
    & ComputerName)
' Get physical disk drive
Set wmiDiskDrives =  wmiServices.ExecQuery ( "SELECT Caption, DeviceID FROM Win32_DiskDrive WHERE InterfaceType = 'USB'")

For Each wmiDiskDrive In wmiDiskDrives
   ' x = wmiDiskDrive.Caption & Vbtab & " " & wmiDiskDrive.DeviceID 
    'Use the disk drive device id to
    ' find associated partition
    query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" & wmiDiskDrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"    
    Set wmiDiskPartitions = wmiServices.ExecQuery(query)

    For Each wmiDiskPartition In wmiDiskPartitions
        'Use partition device id to find logical disk
        Set wmiLogicalDisks = wmiServices.ExecQuery ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _
             & wmiDiskPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition") 
x = ""
        For Each wmiLogicalDisk In wmiLogicalDisks
            x = x & wmiDiskDrive.Caption & " " & wmiDiskPartition.DeviceID & " = " & wmiLogicalDisk.DeviceID
Wscript.echo x

        Next      
    Next
Next

答案 1 :(得分:1)

从下一个脚本中获取所需的输出(根据需要进行评论并使用不言自明的输出)。写入使用wscriptcscript主持人

启动
' VB Script Document 28556806.vbs
option explicit
On Error Goto 0

Dim ComputerName, strRslt, strQuery
Dim wmiServices _
  , wmiDiskDrives, wmiDiskDrive _
  , wmiDiskPartitions, wmiDiskPartition _
  , wmiLogicalDisks, wmiLogicalDisk

strRslt = Wscript.ScriptName _
    & vbTab & "Drive letters associated with disk drives" _ 

ComputerName = "."

Set wmiServices  = GetObject ( _
    "winmgmts:{impersonationLevel=Impersonate}!//" _
    & ComputerName)
' Get physical disk drive
Set wmiDiskDrives =  wmiServices.ExecQuery ( _
    "SELECT Caption, DeviceID, InterfaceType FROM Win32_DiskDrive")

For Each wmiDiskDrive In wmiDiskDrives
    strRslt = strRslt & vbNewLine
    strRslt = strRslt & vbNewLine _
      & "DiskDrive.Caption = " & wmiDiskDrive.Caption _ 
      & vbNewLine & "DiskDrive.InterfaceType = " _
          & wmiDiskDrive.InterfaceType

    'Use the disk drive device id to find associated partition
    strQuery = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" _
        & wmiDiskDrive.DeviceID _
        & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"    
    Set wmiDiskPartitions = wmiServices.ExecQuery(strQuery)

    For Each wmiDiskPartition In wmiDiskPartitions
        'Use partition device id to find logical disk
        Set wmiLogicalDisks = wmiServices.ExecQuery _
            ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _
             & wmiDiskPartition.DeviceID _
             & "'} WHERE AssocClass = Win32_LogicalDiskToPartition") 

        For Each wmiLogicalDisk In wmiLogicalDisks
            strRslt = strRslt _
              & vbNewLine & "DiskDrive.Caption = " _
                  & wmiDiskDrive.Caption _
              & vbNewLine & "DiskDrive.DeviceID = " _
                  & wmiDiskDrive.DeviceID _
              & vbNewLine & "DiskPartition.Partition = " _
                  & wmiDiskPartition.DeviceID _
              & vbNewLine & "LogicalDisk.DeviceID = " _
                  & wmiLogicalDisk.DeviceID 
        Next      
    Next
Next

WScript.Echo strRslt

我已经用更广泛的方法构思了我的答案,以便更容易地决定连接更多的USB驱动器;可以在Computer System Hardware Classes

找到适当的属性