slloyd has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to determine a particular drive's InterfaceType (USB,IDE or SCSI)? Using Win32::API, I can call GetDriveType to tell if it is a fixed drive, cd-rom drive, etc but I need a way to determine whether the drive is USB, IDE, or SCSI. Any ideas?

UPDATE: Taking the excellent comments made, I have been able to come up with the following but when I run it, it accesses the floppy drive every time... I will still need to work on figuring out how to exclude the floppy drive seek unless specifically requested.

use strict; use warnings; use Win32::OLE qw(in); my $objWMIService = Win32::OLE->GetObject("winmgmts:{impersonationLeve +l=impersonate,(security)}//."); die "Error" unless $objWMIService; my $wmiDiskDrives = $objWMIService->ExecQuery("SELECT Caption, DeviceI +D, InterfaceType FROM Win32_DiskDrive"); print "Device Caption -> InterfaceType -> Drive Letter\n"; print "=" x 60 . "\n"; foreach my $wmiDiskDrive ( in( $wmiDiskDrives ) ) { my $deviceID=$wmiDiskDrive->{DeviceId}; $deviceID=~s/\\/\\\\/sg; my $map=qq|ASSOCIATORS OF {Win32_DiskDrive.DeviceID="$deviceID"} W +HERE AssocClass = Win32_DiskDriveToDiskPartition|; #print "$map\n"; my $wmipartitions = $objWMIService->ExecQuery($map); foreach my $wmipartition ( in( $wmipartitions ) ) { my $deviceID=$wmipartition->{DeviceId}; my $map=qq|ASSOCIATORS OF {Win32_DiskPartition.DeviceID="$devi +ceID"} WHERE AssocClass = Win32_LogicalDiskToPartition|; #print "\t$map\n"; my $wmiLogicalDisks = $objWMIService->ExecQuery($map); foreach my $wmiLogicalDisk ( in( $wmiLogicalDisks ) ) { my $drive=$wmiLogicalDisk->{DeviceId}; print join(" -> ", $wmiDiskDrive->{Caption}, $wmiDisk +Drive->{InterfaceType} , $wmiLogicalDisk->{DeviceId}); print "\n"; } } }

-------------------------------
by me
http://www.basgetti.com
http://www.kidlins.com

Replies are listed 'Best First'.
Re: Is a drive USB, IDE or SCSI?
by BrowserUk (Patriarch) on Dec 02, 2005 at 08:34 UTC

    See msdn for where to get the info (on newer versions), and see Win32::Process::Info (WMI.pm) for examples of how to get at it from perl.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      I have tried WMI using the Win32_DiskDrive class but this tells partition information. It does return InterfaceType but how do I map a particular drive letter to a partiton? I spent several hours on it last night but could not get it to work.

      -------------------------------
      by me
      http://www.basgetti.com
      http://www.kidlins.com

        You will probably need to query information from one of the other WMI classes to make the connection between drive letters, logical partitions and physical drives. OS classes page gives a list of them.

        Of particular interest to you will be Win32_DiskDriveToDiskPartition, Win32_LogicalDisk, Win32_LogicalDiskToPartition amongst others.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Is a drive USB, IDE or SCSI?
by kulls (Hermit) on Dec 02, 2005 at 08:23 UTC
    I guess DriveInfo module will give the details about the drive type. for example,
    $type = Win32::DriveInfo::DriveType('a');

    -kulls
      DriveType in the Win32::DriveInfo module only tells if a drive is fixed, cdrom, or removable. It cannot determine if a drive is USB, IDE, or SCSI.

      -------------------------------
      by me
      http://www.basgetti.com
      http://www.kidlins.com

Re: Is a drive USB, IDE or SCSI?
by psychotic (Beadle) on Dec 02, 2005 at 17:41 UTC
    Hello. After reading up on MSDN, i wrote the following little snippet that performs what you wish. Enjoy!

    use strict; use warnings; use Win32::OLE qw(in); my $objWMIService = Win32::OLE->GetObject("winmgmts:{impersonationLeve +l=impersonate,(security)}//."); die "Error" unless $objWMIService; my $colDisks = $objWMIService->ExecQuery("Select * from Win32_DiskDriv +e"); print "Device Caption -> InterfaceType -> DeviceId\n"; print "=" x 60 . "\n"; foreach my $obj ( in( $colDisks ) ) { print join(" -> ", $obj->{Caption}, $obj->{InterfaceType} , $obj-> +{DeviceId}); print "\n"; }

    On my machine, it produces the following:

    Device Caption -> InterfaceType -> DeviceId ============================================================ WDC WD400BB-00CLB0 -> IDE -> \\.\PHYSICALDRIVE0 WDC WD800JB-00JJA0 -> IDE -> \\.\PHYSICALDRIVE1

    Update: minor edit.

        Well, with all the respect, the above code i wrote performs exactly what you indicated in your first post. Should you already had accomplished the very task you were asking for, what was the point of even starting this node? Why didn't you post your code upfront, so the rest of us can work on it rather than having to go through MSDN and come up with something from scratch?

        As for your question, it appears you are confusing some fundamentals here. The physical drive is associated with an InterfaceType, and not the partition. This is what is getting listed here.

Re: Is a drive USB, IDE or SCSI?
by planetscape (Chancellor) on Dec 02, 2005 at 19:02 UTC
A reply falls below the community's threshold of quality. You may see it by logging in.