Reputation: 4319
Here is a which shows list of connected USB storage devices.
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
I need to get the drive letter, but it is impossible with the code above. There is no property about Drive Letter for colItems. I can use another script which supports Drive Letter, here it is:
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
But in this script I can not use Caption Property because it is not supported :( I need to combine these two scripts to get every USB Storage Caption and Letter. How Can I do that? Possible?
Upvotes: 1
Views: 5295
Reputation: 30268
Derive your desired output from next script (commented as necessary and with self-explanatory output). Written to launch with either wscript
or cscript
host)
' 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
I have conceived my answer in a wider approach to make easier deciding among more USB drives attached; appropriate properties could be found at Computer System Hardware Classes
Upvotes: 1
Reputation: 18857
Try This code :
'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
Upvotes: 3