Advertisement
TrunghieuTH10

USB Detect

Oct 21st, 2015
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 2.75 KB | None | 0 0
  1. #include <Array.au3>
  2. Opt("MustDeclareVars", 1)
  3. Local $arrResults = _GetLogicalDriveToPartition()
  4. _ArrayDisplay($arrResults, "USB Drives")
  5. Exit
  6.  
  7. Func _GetLogicalDriveToPartition()
  8. ; Error monitoring. This will trap all COM errors while alive.
  9.     ; This particular object is declared as local, meaning after the function returns it will not exist.
  10. Local $oErrorHandler, $oWMIService, $arr[1][4], $iPos, $iPhysDrive, $iUB, $oColDevice, $oColItems
  11.     $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")
  12. If Not IsObj($oErrorHandler) Then ConsoleWrite("!No COM error handler" & @CRLF)
  13. $oWMIService = ObjGet("winmgmts:{impersonationLevel=Impersonate}!\\.\root\cimv2")
  14. If Not IsObj($oWMIService) Then Return -1 ;Failed to Connect to WMI on Local Machine
  15. $oColDevice = $oWMIService.ExecQuery("SELECT * from Win32_LogicalDiskToPartition", "WQL", 48)
  16. If Not IsObj($oColDevice) Then Return -1
  17. For $objItem In $oColDevice
  18.   $iPos = StringInStr($objItem.Antecedent, "Disk #")
  19.   If Not @error And $iPos <> 0 Then
  20.    $iPhysDrive = StringMid($objItem.Antecedent, $iPos +6, 1)
  21.    $oColItems = $oWMIService.ExecQuery("SELECT InterfaceType,Model FROM Win32_DiskDrive WHERE DeviceID = '\\\\.\\PHYSICALDRIVE"&$iPhysDrive&"'", "WQL", 48)
  22.    If Not IsObj($oColItems) Then Return -1
  23.    For $obj In $oColItems
  24.     If $obj.InterfaceType == "USB" Then
  25.      $iUB = UBound($arr)
  26.      ReDim $arr[$iUB + 1][4]
  27.      $arr[$iUB][0] = StringTrimLeft($objItem.Antecedent, StringInStr($objItem.Antecedent, "=", 0, -1))
  28.      $arr[$iUB][1] = StringReplace(StringTrimLeft($objItem.Dependent, StringInStr($objItem.Dependent, "=", 0, -1)), """", "")
  29.      $arr[$iUB][2] = DriveGetLabel($arr[$iUB][1])
  30.      $arr[$iUB][3] = $obj.Model
  31.      ;ConsoleWrite("+ Drive " & $arr[$iUB][1]& " is USB" & @CRLF)
  32.     EndIf
  33.    Next
  34.   EndIf
  35. Next
  36. $arr[0][0] = "Disk #, Partition #"
  37. $arr[0][1] = "Drive Letter"
  38. $arr[0][2] = "Drive Label"
  39. $arr[0][3] = "Drive Model"
  40. Return $arr
  41. EndFunc   ;==>_GetLogicalDriveToPartition
  42.  
  43. ; User's COM error function. Will be called if COM error occurs
  44. Func _ErrFunc($oError)
  45.     ; Do anything here.
  46.     ConsoleWrite("err.number is: " & @TAB & $oError.number & @CRLF & _
  47.             "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
  48.             "err.description is: " & @TAB & $oError.description & @CRLF & _
  49.             "err.source is: " & @TAB & $oError.source & @CRLF & _
  50.             "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
  51.             "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
  52.             "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
  53.             "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
  54.             "err.retcode is: " & @TAB & $oError.retcode & @CRLF & @CRLF)
  55. EndFunc   ;==>_ErrFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement