Success! It was actually prettier code than I thought it would be.

Thanks to slloyd for his "Is a drive USB, IDE or SCSI?" script (found here ) Also, thanks to Mr. Muskrat for his "(Win32) Hard Drive Information" script (found here)

Here's my script. Feel free to use it or comment. I'm sure it is not the most efficient way to do this, but it works fast enough for my needs. Sorry about the comments, I'll work on my obfuscation later. :-)

use strict; use warnings; use Win32::OLE qw(in); my $Machine = shift @ARGV || "."; $Machine =~ s/^[\\\/]+//; my $WMIServices = Win32::OLE->GetObject( "winmgmts:{impersonationLevel +=impersonate,(security)}//$Machine") || die "Could not get Win32::OLE + Object.\n"; my %serial; # Serial Number hash based on device name my %device; # Drive Letter Based on Device Name # Populate Serial Hash my $PhysMediaCollection = $WMIServices->InstancesOf( "Win32_PhysicalMe +dia" ); foreach my $PM (in ($PhysMediaCollection)) { my $name = (split(/=/, $PM->{Path_}->relpath))[1]; $serial{eval $name}=$PM->{SerialNumber}; } # Loop Over Hard Disks my $DiskDriveCollection = $WMIServices->InstancesOf( "Win32_DiskDrive" + ); #my $DiskDriveCollection = $WMIServices->ExecQuery("SELECT Caption, De +viceID FROM Win32_DiskDrive"); foreach my $DiskDrive (in($DiskDriveCollection)) { my $cap_disk = $DiskDrive->{Caption}; my $did_disk = $DiskDrive->{DeviceID}; my $query_did = $did_disk; $query_did =~ s/\\/\\\\/sg; # Very important to change slashy form +atting my $query = 'ASSOCIATORS OF ' . '{Win32_DiskDrive.DeviceID="' . $q +uery_did . '"} WHERE AssocClass = Win32_DiskDriveToDiskPartition'; my $DiskPartCollection = $WMIServices->ExecQuery($query); #Loop Over Partitions foreach my $Partition (in($DiskPartCollection)) { my $did_part = $Partition->{DeviceID}; my $query = 'ASSOCIATORS OF {Win32_DiskPartition.DeviceID=\'' +. $did_part . "\'} WHERE AssocClass = Win32_LogicalDiskToPartition"; my $LogDriveCollection = $WMIServices->ExecQuery($query); # Loop Over Logical Drives foreach my $LogDrive (in($LogDriveCollection)) { my $did_log = $LogDrive->{DeviceID}; $device{$did_log} = $did_disk; } # Next Logical Drive } # Next Partition } # Next Hard Disk # Print Results foreach my $letter (keys %device) { my $sn = $serial{$device{$letter}}; print "Drive $letter maps to drive with S/N $sn \n"; }


In reply to Re: Windows drive letter mapping question by AnthonyC
in thread Windows drive letter mapping question by AnthonyC

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.