5

Problem: I need to figure out which volume corresponds to what partition(s) which corresponds to what disk in an extremely efficient script able manor. I know how to correspond which partition corresponds to what disk because the disk id is directly in the results of a simple wmic query. However, the first part of the problem is more difficult. How to correlate which volume belongs to which partitions?

Is there a way, using wmic, PowerShell, cmd prompt, to reverse-engineer which volume maps to which partition(s), that works across all currently supported versions of Windows Server (Windows Server 2008R2 - Windows Server 2016)?

If so how would this query look?

Using diskpart to get information is NOT an option. Although it can be used to script out disk operations, it is terrible to use diskpart as a tool to return information about disk configurations. Diskpart output is NOT parsible.

1

One possible solution, is the following power shell script. Output should be in JSON format. The problem with this solution is that it relies on the get-partition cmdlet. This cmdlet was introduced after windows 2008R2. This would work as a solution if the get-partition cmdlet were to be ported to windows 2008R2, or if I found a solution that would do the same thing on windows 2008R2. Although not a complete solution, it didn't fit as a comment.

$CimPartInfo = get-partition
        "{"
        foreach ($CimPart in $CimPartInfo) {
            if ($CimPart.Guid -eq $null) {
                $PartGUID = [regex]::match($CimPart.AccessPaths, 'Volume({[^}]+})').Groups[1].Value
                }
             else {
                $PartGUID = $CimPart.Guid
                }
            "`"$PartGUID`": {"
            "`"DiskId`": $($CimPart.DiskNumber),"
            "`"PartitionId`": $($CimPart.PartitionNumber),"
            "`"Type`": `"$($CimPart.Type)`","
            "`"Size`": $($CimPart.Size),"
            "`"Offset`": $($CimPart.Offset),"
            "`"GUID`": `"$($CimPart.Guid)`","
            $x = $CimPart.IsBoot
            "`"Bootable`": $($x.ToString().ToLower()),"
            "`"Status`": `"$($CimPart.OperationalStatus)`""
            if ($CimPart -eq $CimPartInfo[-1]){ "}"}
            else {"},"}
            } #foreach CimPart
        "}"
0

I cannot currently post comments, however this seems very close to a solution for you. I found documentation from WMI Tasks entitled "...detect which drive letter is associated with a logical disk partition?" in VBScript.

Please note: I am by no means well versed in VBScript, so I cannot speak to how versatile and modifiable the below method is, however I believe it has all information needed to modify a new script that's parsable in a format to your liking.

Source: https://msdn.microsoft.com/en-us/library/aa394592(v=vs.85).aspx#code-snippet-11

ComputerName = "."
Set wmiServices  = GetObject ( _
    "winmgmts:{impersonationLevel=Impersonate}!//" & ComputerName)
' Get physical disk drive
Set wmiDiskDrives =  wmiServices.ExecQuery ( "SELECT Caption, DeviceID FROM Win32_DiskDrive")

For Each wmiDiskDrive In wmiDiskDrives
    WScript.Echo "Disk drive Caption: " & wmiDiskDrive.Caption & VbNewLine & "DeviceID: " & " (" & wmiDiskDrive.DeviceID & ")"

    'Use the disk drive device id to
    ' find associated partition
    query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" _
        & wmiDiskDrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"    
    Set wmiDiskPartitions = wmiServices.ExecQuery(query)

    For Each wmiDiskPartition In wmiDiskPartitions
        'Use partition device id to find logical disk
        Set wmiLogicalDisks = wmiServices.ExecQuery _
            ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _
             & wmiDiskPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition") 

        For Each wmiLogicalDisk In wmiLogicalDisks
            WScript.Echo "Drive letter associated" _
                & " with disk drive = " _ 
                & wmiDiskDrive.Caption _
                & wmiDiskDrive.DeviceID _
                & VbNewLine & " Partition = " _
                & wmiDiskPartition.DeviceID _
                & VbNewLine & " is " _
                & wmiLogicalDisk.DeviceID
        Next      
    Next
Next

When I test this script I get all requested information in WScript.Echo dialogs. As an example, on my system, the dialog shows: VBScript Dialog Output

  • Unfortunately. logical disks are not the same as volumes. Logical disks do not include disks that have been mounted to the file system like in Linux. Also, having a partition which has not been formatted, still creates a volume object in windows. I need this to help me map which blank partitions map to which volumes. It's in the right direction, but not quite there. This script outputs nothing for the case in which you have a RAW partition even though there is very much so a volume created or when the drive is mounted to a folder like in linux. – Rusty Weber Oct 6 '16 at 0:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.