本文介绍了在PowerShell中结合使用“获取磁盘"信息和“逻辑磁盘"信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个查询,它扫描所有逻辑磁盘信息:

I have this query which scans all logical disks information :

Write-Host "Drive information for $env:ComputerName"

Get-WmiObject -Class Win32_LogicalDisk |
    Where-Object {$_.DriveType -ne 5} |
    Sort-Object -Property Name | 
    Select-Object Name, VolumeName, VolumeSerialNumber,SerialNumber, FileSystem, Description, VolumeDirty, `
        @{"Label"="DiskSize(GB)";"Expression"={"{0:N}" -f ($_.Size/1GB) -as [float]}}, `
        @{"Label"="FreeSpace(GB)";"Expression"={"{0:N}" -f ($_.FreeSpace/1GB) -as [float]}}, `
        @{"Label"="%Free";"Expression"={"{0:N}" -f ($_.FreeSpace/$_.Size*100) -as [float]}} |
    Format-Table -AutoSize

输出为:

但是-我需要物理磁盘信息及其分区/卷信息:

However - I'm after the physical disks information and their partitions / volume information :

所以-对于物理磁盘,我有以下命令:

So - for physical disks I have this command :

Get-Disk

结果:

问题:

我想在这两个命令之间组合.我想查看每个磁盘下方的磁盘-逻辑磁盘信息:

I want to combine between those 2 commands . I want to see the Disk , and below each disk - its logical disk information :

  • 磁盘编号1:....(信息)
    >其逻辑磁盘信息.....
  • 磁盘编号2:....(信息)
    >这是逻辑磁盘信息.....
  • 磁盘编号3:....(信息)
    >这是逻辑磁盘信息.....
  • 等...
  • Disk Number 1 : ....(info)
    >Its logical disks info.....
  • Disk Number 2 : ....(info)
    >It's logical disks info.....
  • Disk Number 3 : ....(info)
    >It's logical disks info.....
  • etc...

如何在这两个查询之间进行组合?

How can I combine between those 2 queries ?

推荐答案

您需要查询几个WMI类以获取所需的所有信息.

You need to query several WMI classes to get all information you want.

可以使用 Win32_DiskDriveToDiskPartition将分区映射到其磁盘. 类,并且可以通过 Win32_LogicalDiskToPartition 类.

Partitions can be mapped to their disks using the Win32_DiskDriveToDiskPartition class, and drives can be mapped to their partitions via the Win32_LogicalDiskToPartition class.

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 {
      New-Object -Type PSCustomObject -Property @{
        Disk        = $disk.DeviceID
        DiskSize    = $disk.Size
        DiskModel   = $disk.Model
        Partition   = $partition.Name
        RawSize     = $partition.Size
        DriveLetter = $_.DeviceID
        VolumeName  = $_.VolumeName
        Size        = $_.Size
        FreeSpace   = $_.FreeSpace
      }
    }
  }
}

这篇关于在PowerShell中结合使用“获取磁盘"信息和“逻辑磁盘"信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 07:53