如何仅获取物理驱动器?

如何仅获取物理驱动器?

下面是我的示例代码。我不需要 CD-ROM 之类的驱动器。

$drive = Get-WmiObject Win32_LogicalDisk -ComputerName $compName | ForEach-Object -Process {$_.DeviceID}

答案1

使用接口类型或媒体类型的两种方法

第一个选项 interfacetype,

gwmi win32_diskdrive | ?{$_.interfacetype -eq "IDE" -or $_.interfacetype -eq "SCSI" }

物理磁盘驱动器的接口类型。

这些值是:

  • 小型计算机系统接口
  • 硬质合金
  • 集成开发环境
  • USB
  • 1394

第二个选项 mediatype

gwmi win32_diskdrive | ?{$_.mediatype -eq "Fixed hard disk media"}

可能的值包括:

  • 外部硬盘介质
  • 可移动媒体(“软盘以外的可移动媒体”)
  • 固定硬盘(“固定硬盘介质”)
  • 未知(“格式未知”)

获得身体动力

gwmi win32_diskdrive | ?{$_.mediatype -eq "Fixed hard disk media"} | % -Process {$_.DeviceID}

获取逻辑驱动器

gwmi win32_diskdrive | ?{$_.mediatype -eq "Fixed hard disk media"} | %{gwmi -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($_.DeviceID.replace('\','\\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition"} |  %{gwmi -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($_.DeviceID)`"} WHERE AssocClass = Win32_LogicalDiskToPartition"} | %{$_.deviceid}

答案2

如果你只想要物理驱动器,那么很简单

PS>  Get-PhysicalDisk
FriendlyName               SerialNumber         MediaType   CanPool OperationalStatus HealthStatus Usage           Size
------------               ------------         ---------   ------- ----------------- ------------ -----           ----
Generic USB  SD Reader     x                    Unspecified False   OK                Healthy      Auto-Select 14.49 GB
Samsung SSD 840 PRO Series xxxxxxxxxxxxxxx      SSD         True    OK                Healthy      Auto-Select ...47 GB
ST2000DL003-9VT166         xxxxxxxx             HDD         False   OK                Healthy      Auto-Select  1.82 TB
ASMT 2105                  xxxxxxxxxxxxxxxxxxxx Unspecified False   OK                Healthy      Auto-Select ...51 GB

如果你想要关联链这可能会有帮助

$diskdrive = gwmi win32_diskdrive

foreach($drive in $diskdrive){
    out-host -InputObject "`nDrive: deviceid-$($drive.deviceid.substring(4)) Model - $($drive.model)"
    ##partition
    $partitions = gwmi -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($drive.DeviceID.replace('\','\\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
    foreach($part in $partitions){
        Out-Host -InputObject "`tPartition: $($part.name)"
        $vols = gwmi -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($part.DeviceID)`"} WHERE AssocClass = Win32_LogicalDiskToPartition"
        foreach($vol in $vols){
            out-host -InputObject "`t`t$($vol.name)"
        }
    }
}

示例输出

Drive: deviceid-PHYSICALDRIVE0 Model - ST2000DL003-9VT166
        Partition: Disk #0, Partition #0
                D:

Drive: deviceid-PHYSICALDRIVE1 Model - Samsung SSD 840 PRO Series
        Partition: Disk #1, Partition #0
                H:
        Partition: Disk #1, Partition #1
                C:
        Partition: Disk #1, Partition #2

Drive: deviceid-PHYSICALDRIVE2 Model - Generic USB  SD Reader USB Device
        Partition: Disk #2, Partition #0
                I:

Drive: deviceid-PHYSICALDRIVE3 Model - ASMT 2105 USB Device
        Partition: Disk #3, Partition #0

相关内容