Hi guys....
I am VERY new to using SQL under VBS (or at all!).
I am trying to detect which of 4 given USB devices are connected to the PC (using the Plug'N'Play id of the USB devices), and what their drive letters are.
The following code runs to completion, but returns no results.
I THINK the SQL statement is syntactically correct, but don't have any experience of the "Win32_USBHub" or "Win32_DiskDrive" 'table' (is 'table' the correct word?).
My code follows.
Can any kind soul help me get it working?
NOTE: I've tried using both "conQueryDrives" and "conQueryUSBs" in the SQL - conQueryDrives takes appreciably longer, but still has 0 results.
I am VERY new to using SQL under VBS (or at all!).
I am trying to detect which of 4 given USB devices are connected to the PC (using the Plug'N'Play id of the USB devices), and what their drive letters are.
The following code runs to completion, but returns no results.
I THINK the SQL statement is syntactically correct, but don't have any experience of the "Win32_USBHub" or "Win32_DiskDrive" 'table' (is 'table' the correct word?).
My code follows.
Can any kind soul help me get it working?
NOTE: I've tried using both "conQueryDrives" and "conQueryUSBs" in the SQL - conQueryDrives takes appreciably longer, but still has 0 results.
Code:
Option Explicit
'*
'** Script to identify the PnPId attached to an USB port.
'*
Const conComputer = "."
Const conQueryUSBs = "SELECT * FROM Win32_USBHub"
Const conQueryDrives = "SELECT * FROM Win32_DiskDrive"
Dim arrDevDets()
Dim colDiskDrives, objDrive
Dim colLogicalDisks, objLogicalDisk
Dim colPartitions, objPartition
Dim intMax
Dim intPtr
Dim objWMIService
Dim strDeviceID
Dim strQuery
Dim strPNPSearch
'*
'** Set up the known substrings of the possible USB
'** devices.
'*
ReDim arrDevDets(3)
arrDevDets(0) = "575837314143324C33323636"
arrDevDets(1) = "575832314143325935373537"
arrDevDets(2) = "575834314142323738313037"
arrDevDets(3) = "575838314143324130323234"
'*
'** Set up the SQL to recover the USB details.
'*
strPNPSearch = ""
intMax = UBound(arrDevDets)
For intPtr = 0 To intMax
strPNPSearch = strPNPSearch & "PNPDeviceId like '" & _
arrDevDets(intPtr) & "'"
If intPtr < intMax Then strPNPSearch = strPNPSearch & " OR "
Next
' strQuery = conQueryUSBs & " WHERE " & strPNPSearch
strQuery = conQueryDrives & " WHERE " & strPNPSearch
Call MsgBox(strQuery,vbInformation,"DIAGNOSTIC")
' WScript.Quit
Set objWMIService = GetObject("winmgmts:\\" & _
conComputer & _
"\root\cimv2")
Set colDiskDrives = objWMIService.ExecQuery(strQuery)
For Each objDrive In colDiskDrives
call MsgBox("Physical Disk: " & _
objDrive.Caption & _
" -- " & _
objDrive.DeviceID)
strDeviceID = Replace(objDrive.DeviceID, "\", "\\")
Set colPartitions = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & _
strDeviceID & """} WHERE AssocClass = " & _
"Win32_DiskDriveToDiskPartition")
For Each objPartition In colPartitions
Call MsgBox("Disk Partition: " & objPartition.DeviceID)
Set colLogicalDisks = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & _
objPartition.DeviceID & """} " & _
"WHERE AssocClass = Win32_LogicalDiskToPartition")
For Each objLogicalDisk In colLogicalDisks
Call MsgBox("Logical Disk: " & objLogicalDisk.DeviceID)
Next 'objLogicalDisk
Next 'objPartition
Next 'objDrive
WScript.Quit