gpt4 book ai didi

powershell - 在PowerShell中结合 `Get-Disk`信息和 `LogicalDisk`信息(但具有格式化输出)

转载 作者:行者123 更新时间:2023-12-03 01:25:49 24 4
gpt4 key购买 nike

这是关于此Combine `Get-Disk` info and `LogicalDisk` info in PowerShell?答案的问题
这是我尝试更改的答案,以使输出格式化为所需的格式:
https://stackoverflow.com/a/31092004/8262102
它仅适用于所需格式的多个驱动器,如下面的代码。
这是代码,其中包含我尝试执行的所有详细信息:

$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 + '$1:$2 ')

$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

最佳答案

要输出您显然需要的信息,我们需要知道最大行长(在您的示例中为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>')
输出示例:
#磁盘:\\。\ PHYSICALDRIVE1
#磁盘型号:三星SSD 750 EVO 250GB
#分区:磁盘#1,分区#0
#原始大小:232 GB
#驱动器号:C:
#卷名:System
#大小:232 GB
#可用空间:160 GB

#磁盘:\\。\ PHYSICALDRIVE2
#磁盘型号:WDC WD7501AALS-00J7B0
#分区:磁盘#2,分区#0
#原始大小:699 GB
#驱动器号:D:
#卷名:数据
#大小:699 GB
#可用空间:385 GB

附言通过创建 [PsCustomObject],无需添加 [Ordered]

关于powershell - 在PowerShell中结合 `Get-Disk`信息和 `LogicalDisk`信息(但具有格式化输出),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63219836/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com