Jump to content

Detect Drive's name


Recommended Posts

Hello

I am newbie to AutoIT and I'm found the scripting which help me a lot. Recently, I found a snippet of code from the forum, which does what I want but only the output is not what I expected.

I would like to write a code which show the USB drive's name, ID and serial number. I looked through help but there are so many functions that all look similar, I'm getting lost.

Here is the code : 
 

#include <Array.au3>

$ComputerName = "."
$wmiServices = ObjGet ( _
"winmgmts:{impersonationLevel=Impersonate}!//" _
& $ComputerName)
$wmiDiskDrives = $wmiServices.InstancesOf("Win32_DiskDrive")
Dim $List[1][6], $i = 0
$List[0][0] = 0
$List[0][1] = "Drive"
$List[0][2] = "Model"
$List[0][3] = "InterfaceType"
$List[0][4] = "Device ID"
$List[0][5] = "Serial Number"


For $wmiDiskDrive In $wmiDiskDrives
ReDim $List[$List[0][0] + 2][6]
$List[$List[0][0] + 1][2] = $wmiDiskDrive.Model
$List[$List[0][0] + 1][3] = $wmiDiskDrive.InterfaceType
$List[0][0] = $List[0][0] + 1
Next
$wmiDiskDrives = $wmiServices.InstancesOf("Win32_DiskDrive")
For $wmiDiskDrive In $wmiDiskDrives
$i = $i + 1
$List[$i][4] = $wmiDiskDrive.PNPDeviceID
$List[$i][5] = $wmiDiskDrive.SerialNumber
Next
$aDrive= DriveGetDrive('ALL')
For $wmiDiskDrive In $wmiDiskDrives
$List[$i][1] = $aDrive

Next 

_ArrayDisplay($aDrive,$List,"Class List")

thank you 

Edited by LyndaSing7
Link to comment
Share on other sites

Hello and welcome to forum!  :)

Try this:

#include <Array.au3>

$USB = USBDevices()
If IsArray($USB) Then _ArrayDisplay($USB)

Func USBDevices()
    Local $USB
    $ComputerName = "."
    $wmiServices = ObjGet("winmgmts:{impersonationLevel=Impersonate}!//" & $ComputerName)
    $wmiDiskDrives =  $wmiServices.ExecQuery("SELECT Model, InterfaceType, PNPDeviceID, Caption, DeviceID, SerialNumber FROM Win32_DiskDrive")
    Local $USB[1][5]
    For $wmiDiskDrive In $wmiDiskDrives
        $wmiDiskPartitions = $wmiServices.ExecQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" & $wmiDiskDrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition")
        For $wmiDiskPartition In $wmiDiskPartitions
            $wmiLogicalDisks = $wmiServices.ExecQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" & $wmiDiskPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition")
            For $wmiLogicalDisk In $wmiLogicalDisks
                If $wmiDiskDrive.InterfaceType = 'USB' Then
                    $USB[0][0] += 1
                    ReDim $USB[$USB[0][0]+1][5]
                    $USB[$USB[0][0]][0] = $wmiLogicalDisk.DeviceID
                    $USB[$USB[0][0]][1] = $wmiDiskDrive.Model
                    $USB[$USB[0][0]][2] = $wmiDiskDrive.InterfaceType
                    $USB[$USB[0][0]][3] = $wmiDiskDrive.PNPDeviceID
                    $USB[$USB[0][0]][4] = $wmiDiskDrive.SerialNumber
                EndIf
            Next
        Next
    Next
    Return $USB
EndFunc
Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...