Jump to content

GetDriveLetter from DiskID


Recommended Posts

I want to read the assigned drive letter of a disk.

During runtime I only got the DiskID and now I want to check if there is an assigned drive letter.

(In my special case I will have disks with one partition, but the scritp below will find all ...)

I do not want to read any DISKPART so I tried it with this:

#include <Array.au3>
global $DiskPartitionLetter[26][2]
$DiskID="4"
$driveLetter=GetDriveLetterFromDiskId($DiskID)
_arraydisplay($DiskPartitionLetter)
ConsoleWrite("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"&@LF&"Disk ID "&$DiskID&" returns this letter for first partition:" & $DiskPartitionLetter[1][1] & @LF)


func GetDriveLetterFromDiskId($DriveID)
$oWMISvc = ObjGet("winmgmts:\\" & @ComputerName & "\root\cimv2")
$colDiskDrives = $oWMISvc.ExecQuery("SELECT * FROM Win32_DiskDrive")

    ConsoleWrite("DiskDrive = \\.\PHYSICALDRIVE" & $DriveID & "  Caption = USB BACK DRIVE"  & @LF)

    $sQuery = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='\\.\PHYSICALDRIVE" & $DriveID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
    $colPartitions = $oWMISvc.ExecQuery($sQuery)
$partNo=1
    For $oPartition In $colPartitions
        ConsoleWrite("Partition = " & $oPartition.DeviceId & @LF)

        $sQuery = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" & $oPartition.DeviceId & "'} WHERE AssocClass = Win32_LogicalDiskToPartition"
        $colLogicalDisks = $oWMISvc.ExecQuery($sQuery)

        For $oLogicalDisk In $colLogicalDisks
            ConsoleWrite("LogicalDisk = " & $oLogicalDisk.DeviceId & @LF)
            $DiskPartitionLetter[0][0]=$partNo
            $DiskPartitionLetter[$partNo][0]=$oPartition.DeviceId
            $DiskPartitionLetter[$partNo][1]=$oLogicalDisk.DeviceId

            $partNo=$partNo+1
        Next
    Next
    return $DiskPartitionLetter
EndFunc

Is there something better?

I thought I remember something like a "dll call" post, but I'm unable to find it now.

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...