在 PowerShell 中组合“Get-Disk"信息和“LogicalDisk"信息? [英] Combine `Get-Disk` info and `LogicalDisk` info in PowerShell?

查看:17
本文介绍了在 PowerShell 中组合“Get-Disk"信息和“LogicalDisk"信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扫描所有逻辑磁盘信息的查询:

Write-Host "$env:ComputerName 的驱动器信息"Get-WmiObject -Class Win32_LogicalDisk |Where-Object {$_.DriveType -ne 5} |排序对象 - 属性名称 |选择对象名称、卷名、卷序列号、序列号、文件系统、描述、卷脏、`@{"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

:

问题:

我想结合这两个命令.我想查看每个磁盘下方的 Disk 、 - 它的逻辑磁盘信息:

  • 磁盘编号 1 : ....(信息)
    >它的逻辑磁盘信息.....
  • 磁盘编号 2 : ....(信息)
    >这是逻辑磁盘信息......
  • 磁盘编号 3 : ....(信息)
    >这是逻辑磁盘信息......
  • 等等...

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

解决方案

您需要查询多个 WMI 类才能获得您想要的所有信息.

可以使用 将分区映射到它们的磁盘Win32_DiskDriveToDiskPartition 类,并且驱动器可以通过 Win32_LogicalDiskToPartition 类.

Get-WmiObject Win32_DiskDrive |ForEach-Object {$磁盘 = $_$partitions = "协会" +"{Win32_DiskDrive.DeviceID='$($disk.DeviceID)'} " +WHERE AssocClass = Win32_DiskDriveToDiskPartition"Get-WmiObject -Query $partitions |ForEach-Object {$分区= $_$drives = "Associators of " +"{Win32_DiskPartition.DeviceID='$($partition.DeviceID)'} " +WHERE AssocClass = Win32_LogicalDiskToPartition"Get-WmiObject -Query $drives |ForEach-Object {New-Object -Type PSCustomObject -Property @{磁盘 = $disk.DeviceID磁盘大小 = $disk.Size磁盘模型 = $disk.Model分区 = $partition.NameRawSize = $partition.SizeDriveLetter = $_.DeviceID卷名 = $_.卷名尺寸 = $_.Size自由空间 = $_.自由空间}}}}

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

The output is :

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

So - for physical disks I have this command :

Get-Disk

Result :

Question :

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

  • 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 ?

解决方案

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

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 中组合“Get-Disk"信息和“LogicalDisk"信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆