在 PowerShell 中组合“Get-Disk”信息和“LogicalDisk”信息(但带有格式化输出)

Combine `Get-Disk` info and `LogicalDisk` info in PowerShell (but with formatted output)

这是一个关于此

答案的问题

这是我试图更改的答案,以使输出格式化为我想要的格式:

它需要像下面的代码一样以所需的格式适用于多个驱动器。

这是包含我尝试这样做的所有详细信息的代码:

$info_diskdrive_basic = 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 {
      [PSCustomObject][Ordered]@{
        Disk          = $disk.DeviceID
        DiskModel     = $disk.Model
        Partition     = $partition.Name
        RawSize       = '{0:d} GB' -f [int]($partition.Size/1GB)
        DriveLetter   = $_.DeviceID
        VolumeName    = $_.VolumeName
        Size          = '{0:d} GB' -f [int]($_.Size/1GB)
        FreeSpace     = '{0:d} GB' -f [int]($_.FreeSpace/1GB)
      }
    }
  }
}

# Here's my attempt at formatting the output of the code above.

# 1. This trims the dead whitespace from the output.
$info_diskdrive_basic = ($info_diskdrive_basic | Out-String) -replace '^\s+|\s+$', ('')

# 2. I then separate the DiskModel, RawSize, DriveLetter, VolumeName, FreeSpace with the regexp below so this becomes:
# Disk Model, Raw Size, Drive Letter, Volume Name, Free Space
$info_diskdrive_basic = ($info_diskdrive_basic) -replace '(?-i)(?=\B[A-Z][a-z])', (' ')

# 3. Here I then format the string to how I want:
$info_diskdrive_basic = ($info_diskdrive_basic) -replace '(.+?)(\s+):\s*(?!\S)', ($id2 + ':                                       ')

$info_diskdrive_basic

输出应如下所示:

我想像这样格式化属性和值:Properties: >spaces< value 其中值在右边并沿它们的左边对齐

# Disk:                                                 \.\PHYSICALDRIVE0
# Disk Model:                                           Crucial_CT512MX100SSD1
# Partition:                                            Disk #0, Partition #2
# Raw Size:                                             476 GB
# Drive Letter:                                         C:
# Volume Name:
# Size:                                                 476 GB
# Free Space:                                           306 GB

但我的输出结果是这样的:(注意文本是如何不对齐的)

# Disk:                                                \.\PHYSICALDRIVE0
# Disk Model:                                           Crucial_CT512MX100SSD1
# Partition:                                           Disk #0, Partition #2
# Raw Size:                                             476 GB
# Drive Letter:                                         C:
# Volume Name:
# Size:                                                476 GB
# Free Space:                                           306 GB

按原样使用您发布的代码,'stringify' 您的属性

例如:

($info_diskdrive_basic = 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 {
      [PSCustomObject][Ordered]@{
        Disk          = "$($disk.DeviceID)"
        DiskModel     = "$($disk.Model)"
        Partition     = "$($partition.Name)"
        RawSize       = "$('{0:d} GB' -f [int]($partition.Size/1GB))"
        DriveLetter   = "$($_.DeviceID)"
        VolumeName    = "$($_.VolumeName)"
        Size          = "$('{0:d} GB' -f [int]($_.Size/1GB))"
        FreeSpace     = "$('{0:d} GB' -f [int]($_.FreeSpace/1GB))"
      }
    }
  }
})
# Results
<#
Disk        : \.\PHYSICALDRIVE0
DiskModel   : Samsung SSD 950 PRO 512GB
Partition   : Disk #0, Partition #0
RawSize     : 477 GB
DriveLetter : D:
VolumeName  : Data
Size        : 477 GB
FreeSpace   : 364 GB
...
#>

注意事项:

我正在使用 PowerShell 变量压缩将结果赋值给变量并同时输出到屏幕。

更新

As for this...

"I want to format the properties and the values like so: Properties: >spaces< value"

$Spacer = ("`t")*8

($info_diskdrive_basic = 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 {
      [PSCustomObject][Ordered]@{
        Disk          = "$Spacer$($disk.DeviceID)"
        DiskModel     = "$Spacer$($disk.Model)"
        Partition     = "$Spacer$($partition.Name)"
        RawSize       = "$Spacer$('{0:d} GB' -f [int]($partition.Size/1GB))"
        DriveLetter   = "$Spacer$($PSItem.DeviceID)"
        VolumeName    = "$Spacer$($PSItem.VolumeName)"
        Size          = "$Spacer$('{0:d} GB' -f [int]($PSItem.Size/1GB))"
        FreeSpace     = "$Spacer$('{0:d} GB' -f [int]($PSItem.FreeSpace/1GB))"
      }
    }
  }
})
# Results
<#
Disk        :                               \.\PHYSICALDRIVE0
DiskModel   :                               Samsung SSD 950 PRO 512GB
Partition   :                               Disk #0, Partition #0
RawSize     :                               477 GB
DriveLetter :                               D:
VolumeName  :                               Data
Size        :                               477 GB
FreeSpace   :                               364 GB
...
#>

要输出您显然需要的信息,我们需要知道最大行长度(在您的示例中为 79 个字符)并以此为基础进行处理。

$maxLineLength  = 79  # counted from the longest line in your example
$maxValueLength = 0   # a counter to keep track of the largest value length in characters

$info_diskdrive_basic = 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 {
            $obj = [PSCustomObject]@{
                'Disk'         = $disk.DeviceID
                'Disk Model'   = $disk.Model
                'Partition'    = $partition.Name
                'Raw Size'     = '{0:d} GB' -f [int]($partition.Size/1GB)
                'Drive Letter' = $_.DeviceID
                'Volume Name'  = $_.VolumeName
                'Size'         = '{0:d} GB' -f [int]($_.Size/1GB)
                'Free Space'   = '{0:d} GB' -f [int]($_.FreeSpace/1GB)
            }
            # get the maximum length for all values
            $len = ($obj.PsObject.Properties.Value.ToString().Trim() | Measure-Object -Property Length -Maximum).Maximum
            $maxValueLength = [Math]::Max($maxValueLength, $len)
                                          
            # output the object to be collected in $info_diskdrive_basic
            $obj
        }
    }
}

# sort the returned array of objects on the DriveLetter property and loop through
$result = $info_diskdrive_basic | Sort-Object DriveLetter | ForEach-Object {
    # loop through all the properties and calculate the padding needed for the output
    $_.PsObject.Properties | ForEach-Object {
        $label   = '# {0}:' -f $_.Name.Trim()
        $padding = $maxLineLength - $maxValueLength - $label.Length
        # output a formatted line
        "{0}{1,-$padding}{2}" -f $label, '', $_.Value.ToString().Trim()
    }
    # add a separator line between the disks
    ''
}

# output the result on screen
$result

# write to disk
$result | Set-Content -Path 'X:\theResult.txt'

# format for HTML mail:
'<pre>{0}</pre>' -f ($result -join '<br>')

示例输出:

# Disk:                                              \.\PHYSICALDRIVE1
# Disk Model:                                        Samsung SSD 750 EVO 250GB
# Partition:                                         Disk #1, Partition #0
# Raw Size:                                          232 GB
# Drive Letter:                                      C:
# Volume Name:                                       System
# Size:                                              232 GB
# Free Space:                                        160 GB

# Disk:                                              \.\PHYSICALDRIVE2
# Disk Model:                                        WDC WD7501AALS-00J7B0
# Partition:                                         Disk #2, Partition #0
# Raw Size:                                          699 GB
# Drive Letter:                                      D:
# Volume Name:                                       Data
# Size:                                              699 GB
# Free Space:                                        385 GB

P.S。通过创建 [PsCustomObject],无需添加 [Ordered]