Results 1 to 3 of 3

Thread: SQL to identify USB drive letters

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2012
    Location
    West Yorkshire
    Posts
    13

    SQL to identify USB drive letters

    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.

    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
    Last edited by Apprentice13; Jul 30th, 2013 at 11:49 AM. Reason: missed details of purpose of VBS
    Steve
    There's nothing quite so rare as Common Sense!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width