Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Array.au3>
- Opt("MustDeclareVars", 1)
- Local $arrResults = _GetLogicalDriveToPartition()
- _ArrayDisplay($arrResults, "USB Drives")
- Exit
- Func _GetLogicalDriveToPartition()
- ; Error monitoring. This will trap all COM errors while alive.
- ; This particular object is declared as local, meaning after the function returns it will not exist.
- Local $oErrorHandler, $oWMIService, $arr[1][4], $iPos, $iPhysDrive, $iUB, $oColDevice, $oColItems
- $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")
- If Not IsObj($oErrorHandler) Then ConsoleWrite("!No COM error handler" & @CRLF)
- $oWMIService = ObjGet("winmgmts:{impersonationLevel=Impersonate}!\\.\root\cimv2")
- If Not IsObj($oWMIService) Then Return -1 ;Failed to Connect to WMI on Local Machine
- $oColDevice = $oWMIService.ExecQuery("SELECT * from Win32_LogicalDiskToPartition", "WQL", 48)
- If Not IsObj($oColDevice) Then Return -1
- For $objItem In $oColDevice
- $iPos = StringInStr($objItem.Antecedent, "Disk #")
- If Not @error And $iPos <> 0 Then
- $iPhysDrive = StringMid($objItem.Antecedent, $iPos +6, 1)
- $oColItems = $oWMIService.ExecQuery("SELECT InterfaceType,Model FROM Win32_DiskDrive WHERE DeviceID = '\\\\.\\PHYSICALDRIVE"&$iPhysDrive&"'", "WQL", 48)
- If Not IsObj($oColItems) Then Return -1
- For $obj In $oColItems
- If $obj.InterfaceType == "USB" Then
- $iUB = UBound($arr)
- ReDim $arr[$iUB + 1][4]
- $arr[$iUB][0] = StringTrimLeft($objItem.Antecedent, StringInStr($objItem.Antecedent, "=", 0, -1))
- $arr[$iUB][1] = StringReplace(StringTrimLeft($objItem.Dependent, StringInStr($objItem.Dependent, "=", 0, -1)), """", "")
- $arr[$iUB][2] = DriveGetLabel($arr[$iUB][1])
- $arr[$iUB][3] = $obj.Model
- ;ConsoleWrite("+ Drive " & $arr[$iUB][1]& " is USB" & @CRLF)
- EndIf
- Next
- EndIf
- Next
- $arr[0][0] = "Disk #, Partition #"
- $arr[0][1] = "Drive Letter"
- $arr[0][2] = "Drive Label"
- $arr[0][3] = "Drive Model"
- Return $arr
- EndFunc ;==>_GetLogicalDriveToPartition
- ; User's COM error function. Will be called if COM error occurs
- Func _ErrFunc($oError)
- ; Do anything here.
- ConsoleWrite("err.number is: " & @TAB & $oError.number & @CRLF & _
- "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
- "err.description is: " & @TAB & $oError.description & @CRLF & _
- "err.source is: " & @TAB & $oError.source & @CRLF & _
- "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
- "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
- "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
- "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
- "err.retcode is: " & @TAB & $oError.retcode & @CRLF & @CRLF)
- EndFunc ;==>_ErrFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement