从VolumeID / VolumeIndex / PartitionID / PartitionIndex获取驱动器号

时间:2019-06-07 01:37:56

标签: cmd wmi wmic

我想从VolumeID / VolumeIndex / PartitionID / PartitionIndex获取驱动器号

我找到了碎片,但它是从标签获取磁盘号

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
::Get disk number from label

IF "%1."=="." ECHO Missing or Wrong Parameter&GOTO :EOF

SET MyDisk=
SET Mylabel=%1
FOR /F "delims=" %%A IN ('Echo List Volume^|Diskpart^|FIND /I "%Mylabel%"') DO SET Myvol=%%A
IF NOT DEFINED Myvol ECHO ERROR: NO LABEL FOUND&GOTO :EOF
FOR /F "tokens=1 delims=0123456789" %%A IN ("%Myvol%") DO SET /A Myvol=!Myvol:%%A=! 2>nul

FOR /F "delims=" %%A IN ('
^(
ECHO Sel Vol %MyVol%^&ECHO det vol
^)^|diskpart^|FIND "*" 
') DO SET Mydisk=%%A

IF NOT DEFINED Mydisk ECHO ERROR: NO DISK NUMBER FOUND&GOTO :EOF

FOR /F "tokens=1 delims=0123456789" %%A IN ("%MyDisk%") DO SET /A Mydisk=!Mydisk:%%A=! 2>nul

ECHO Volume with label %Mylabel% is on DISK %MyDisk% and it is Volume #%Myvol%

这是其他组件,它们是PowerShell,而不是CMD。 Correlate Physical Device ID to Volume Device ID

Get-WmiObject Win32_DiskDrive | ForEach-Object {
  $disk = $_
  $partitions = "ASSOCIATORS OF " +
                "{Win32_DiskDrive.DeviceID='$($disk.DeviceID)'} " +
                "WHERE AssocClass = Win32_DiskDriveToDiskPartition"
  Get-WmiObject -Query $partitions | ForEach-Object {
    $partition = $_
    $drives = "ASSOCIATORS OF " +
              "{Win32_DiskPartition.DeviceID='$($partition.DeviceID)'} " +
              "WHERE AssocClass = Win32_LogicalDiskToPartition"
    Get-WmiObject -Query $drives | ForEach-Object {
      $driveLetter = $_.DeviceID
      $fltr        = "DriveLetter='$driveLetter'"
      New-Object -Type PSCustomObject -Property @{
        Disk        = $disk.DeviceID
        DriveLetter = $driveLetter
        VolumeName  = $_.VolumeName
        VolumeID    = Get-WmiObject -Class Win32_Volume -Filter $fltr |
                      Select-Object -Expand DeviceID
      }
    }
  }
}

[void][System.Console]::ReadKey($true)

有人帮助我。 谢谢。

1 个答案:

答案 0 :(得分:0)

我发现了:

FOR /F delims^=\^"^ tokens^=5^,11 %%A IN ('WMIC Path Win32_LogicalDiskToPartition Get /Format:csv ^|FIND "Disk #1, Partition #0"') DO ECHO %%B

如何在同一位置添加

WMIC Path Win32_LogicalDiskToPartition Where "Disk #1, Partition #0" get PartitionLetter

我需要得到“磁盘#1,分区#0”的字母。