在PowerShell中组合“Get Disk”信息和“LogicalDisk”信息(但带有格式化输出)
这是一个关于答案的问题 以下是我试图更改的答案,以使输出按我希望的方式格式化: 它需要在多个驱动器上工作,如下面的代码,仅在所需的格式 这是一段代码,详细说明了我的尝试:在PowerShell中组合“Get Disk”信息和“LogicalDisk”信息(但带有格式化输出),powershell,get-wmiobject,Powershell,Get Wmiobject,这是一个关于答案的问题 以下是我试图更改的答案,以使输出按我希望的方式格式化: 它需要在多个驱动器上工作,如下面的代码,仅在所需的格式 这是一段代码,详细说明了我的尝试: $info_diskdrive_basic = Get-WmiObject Win32_DiskDrive | ForEach-Object { $disk = $_ $partitions = "ASSOCIATORS OF " + "{Win32_DiskDrive.DeviceID
$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
,其中值位于右侧并沿左侧对齐
# 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
...
#>
($info_diskdrive_basic=Get WmiObject Win32_diskdrive)
ForEach对象{
$disk=$_
$partitions=“与”+
“{Win32_DiskDrive.DeviceID='$($disk.DeviceID)}”+
“其中AssocClass=Win32\u DiskDriveToDiskPartition”
获取WmiObject-查询$partitions |
ForEach对象{
$partition=$_
$drives=“与”+
“{Win32_DiskPartition.DeviceID='$($partition.DeviceID)}”+
“其中AssocClass=Win32_LogicalDiskToPartition”
获取WmiObject-查询$drives |
ForEach对象{
[PSCustomObject][Ordered]@{
Disk=“$($Disk.DeviceID)”
DiskModel=“$($disk.Model)”
Partition=“$($Partition.Name)”
RawSize=“$({0:d}GB'-f[int]($partition.Size/1GB))”
DriveLetter=“$($设备ID)”
VolumeName=“$($卷名)”
Size=“$('{0:d}GB'-f[int]($).Size/1GB))”
FreeSpace=“$('{0:d}GB'-f[int]($\自由空间/1GB))”
}
}
}
})
#结果
注意事项:
我使用PowerShell变量压缩将结果分配给变量,同时将结果输出到屏幕
更新
至于这个
“我想将属性和值的格式设置为:properties:>spaces$Spacer=(“`t”)*8
($info_diskdrive_basic=获取WmiObject Win32_diskdrive|
ForEach对象{
$disk=$_
$partitions=“与”+
“{Win32_DiskDrive.DeviceID='$($disk.DeviceID)}”+
“其中AssocClass=Win32\u DiskDriveToDiskPartition”
获取WmiObject-查询$partitions |
ForEach对象{
$partition=$_
$drives=“与”+
“{Win32_DiskPartition.DeviceID='$($partition.DeviceID)}”+
“其中AssocClass=Win32_LogicalDiskToPartition”
获取WmiObject-查询$drives |
ForEach对象{
[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))”
}
}
}
})
#结果
要输出您显然需要的信息,我们需要知道最大行长(在您的示例中为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>')
$maxLineLength=79#从示例中的最长行开始计算
$maxValueLength=0#用于跟踪字符中最大值长度的计数器
$info_diskdrive_basic=获取WmiObject Win32_diskdrive| ForEach对象{
$disk=$_
$partitions=“与“+”{Win32\U DiskDrive.DeviceID=”$($disk.DeviceID)}“}”的关联者+“其中,关联类=Win32\U DiskDrive ToDiskPartition”
获取wmioobject-查询$partitions | ForEach对象{
$partition=$_
$drives=“与“+”{Win32_DiskPartition.DeviceID=”$($partition.DeviceID)}“}”的关联器+“其中,关联类=Win32_LogicalDiskToPartition”
获取WmiObject-查询$drives | ForEach对象{
$obj=[PSCustomObject]@{
“Disk”=$Disk.DeviceID
“磁盘型号”=$Disk.Model
“Partition”=$Partition.Name
'Raw Size'='{0:d}GB'-f[int]($partition.Size/1GB)
“驱动器号”=$\设备ID
“卷名”=$\卷名
'Size'='{0:d}GB'-f[int]($\大小/1GB)
'可用空间'='{0:d}GB'-f[int]($\ 0.FreeSpace/1GB)
}
#获取所有值的最大长度
$len=($obj.PsObject.Properties.Value.ToString().Trim()|测量对象-属性长度-最大值)。最大值
$maxValueLength=[Math]::Max($maxValueLength,$len)
#在$info\u diskdrive\u basic中输出要收集的对象
$obj
}
}
}
#对DriveLetter属性上返回的对象数组进行排序并循环
$result=$info_diskdrive_basic |排序对象DriveLetter | ForEach对象{
#循环遍历所有属性并计算输出所需的填充
$\.PsObject.Properties | ForEach对象{
$label='#{0}:'-f$#.Name.Trim()
$padding=$maxLineLength-$maxValueLength-$label.Length
#输出格式化的行
“{0}{1,-$padding}{2}”-f$label,,$\u.Value.ToString().Trim()
}
#在磁盘之间添加分隔线
''
}
#在屏幕上输出结果
$result
#写入磁盘
$result |设置内容-路径“X:\theResult.txt”
#HTML邮件的格式:
“{0}'-f($result-join'
”)
示例输出:
#磁盘:\\.\PHYSICALDRIVE1
#磁盘型号:三星SSD 750 EVO 250GB
#分区:
$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
...
#>
$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