在 PowerShell 中合并“Get-Disk”信息和“LogicalDisk”信息?
Combine `Get-Disk` info and `LogicalDisk` info in PowerShell?
我有这个查询扫描所有 逻辑磁盘 信息:
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
但是 - 我正在寻找 物理磁盘 信息及其分区/卷信息:
所以 - 对于物理磁盘,我有这个命令:
Get-Disk
Result :
问题:
我想结合这两个命令。我想在每个磁盘下面看到磁盘,和 - 它的逻辑磁盘信息:
- 磁盘编号 1:....(信息)
>
其逻辑磁盘信息.....
- 2 号磁盘:....(信息)
>
这是逻辑磁盘信息.....
- 3 号磁盘:....(信息)
>
这是逻辑磁盘信息.....
- 等...
我如何结合这两个查询?
您需要查询多个 WMI classes 才能获得您想要的所有信息。
Win32_DiskDrive
为您提供有关物理磁盘的信息。
Win32_DiskPartition
为您提供有关物理磁盘上分区的信息。
Win32_LogicalDisk
为您提供有关分区内文件系统的信息。
可以使用 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
}
}
}
}
这样怎么样...
Get-CimInstance Win32_Diskdrive -PipelineVariable disk |
Get-CimAssociatedInstance -ResultClassName Win32_DiskPartition -pv partition |
Get-CimAssociatedInstance -ResultClassName Win32_LogicalDisk |
Select-Object @{n='Disk';e={$disk.deviceid}},
@{n='DiskSize';e={$disk.size}},
@{n='DiskModel';e={$disk.model}},
@{n='Partition';e={$partition.name}},
@{n='RawSize';e={$partition.size}},
@{n='DriveLetter';e={$_.DeviceID}},
VolumeName,Size,FreeSpace
输出:
Disk : \.\PHYSICALDRIVE0
DiskSize : 128034708480
DiskModel : SAMSUNG MZ7PC128HAFU-000L5
Partition : Disk #0, Partition #0
RawSize : 128034595328
DriveLetter : C:
VolumeName : DISK
Size : 128034594816
FreeSpace : 7023042560
不幸的是,这不适用于多个驱动器。 get-cimassociatedinstance 和sort-object 一样块,只设置最新的管道变量。这是一个解决方法:
Get-CimInstance Win32_Diskdrive -PipelineVariable disk |
% { Get-CimAssociatedInstance $_ -ResultClass Win32_DiskPartition -pv partition}|
% { Get-CimAssociatedInstance $_ -ResultClassName Win32_LogicalDisk } |
Select-Object @{n='Disk';e={$disk.deviceid}},
@{n='DiskSize';e={$disk.size}},
@{n='DiskModel';e={$disk.model}},
@{n='Partition';e={$partition.name}},
@{n='RawSize';e={$partition.size}},
@{n='DriveLetter';e={$_.DeviceID}},
VolumeName,Size,FreeSpace
Disk : \.\PHYSICALDRIVE0
DiskSize : 128034708480
DiskModel : SAMSUNG MZ7PC128HAFU-000L5
Partition : Disk #0, Partition #0
RawSize : 128034595328
DriveLetter : C:
VolumeName : DISK
Size : 128034594816
FreeSpace : 4226514944
Disk : \.\PHYSICALDRIVE1
DiskSize : 7797565440
DiskModel : USB Flash Memory USB Device
Partition : Disk #1, Partition #0
RawSize : 7801405440
DriveLetter : E:
VolumeName : WINPE
Size : 7784628224
FreeSpace : 7222669312
启发了 js2010 的答案,并进行了一些改进:
例如,Manufacturer
字段在从 Win32_DiskDrive
实例中检索时似乎有一些占位符值,但在使用 Get-Disk
commandlet 时具有正确的值。
function Get-Drive {
foreach($disk in Get-CimInstance Win32_Diskdrive) {
$diskMetadata = Get-Disk | Where-Object { $_.Number -eq $disk.Index } | Select-Object -First 1
$partitions = Get-CimAssociatedInstance -ResultClassName Win32_DiskPartition -InputObject $disk
foreach($partition in $partitions) {
$drives = Get-CimAssociatedInstance -ResultClassName Win32_LogicalDisk -InputObject $partition
foreach($drive in $drives) {
$totalSpace = [math]::Round($drive.Size / 1GB, 3)
$freeSpace = [math]::Round($drive.FreeSpace / 1GB, 3)
$usedSpace = [math]::Round($totalSpace - $freeSpace, 3)
$volume = Get-Volume |
Where-Object { $_.DriveLetter -eq $drive.DeviceID.Trim(":") } |
Select-Object -First 1
[PSCustomObject] @{
DriveLetter = $drive.DeviceID
Number = $disk.Index
Label = $volume.FileSystemLabel
Manufacturer = $diskMetadata.Manufacturer
Model = $diskMetadata.Model
SerialNumber = $diskMetadata.SerialNumber.Trim()
Name = $disk.Caption
FileSystem = $volume.FileSystem
PartitionKind = $diskMetadata.PartitionStyle
TotalSpace = $totalSpace
FreeSpace = $freeSpace
UsedSpace = $usedSpace
Drive = $drive
Partition = $partition
Disk = $disk
}
}
}
}
}
用法示例:
Get-Drive | Format-List
Get-Drive | Where-Object { $_.DriveLetter -eq 'G:' }
输出:
DriveLetter : G:
Number : 5
Label : SE-LXY2-298GB
Manufacturer : Seagate
Model : FreeAgent Go
SerialNumber : 2GE45CK1
Name : Seagate FreeAgent Go USB Device
FileSystem : NTFS
PartitionKind : MBR
TotalSpace : 298.089
FreeSpace : 297.865
UsedSpace : 0.224
Drive : Win32_LogicalDisk: G: (DeviceID = "G:")
Partition : Win32_DiskPartition: Disk #5, Partition #0 (DeviceID = "Disk #5, Partition #0")
Disk : Win32_DiskDrive: Seagate FreeAgent Go USB Device (DeviceID = "\.\PHYSICALDRIVE5")
我有这个查询扫描所有 逻辑磁盘 信息:
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
但是 - 我正在寻找 物理磁盘 信息及其分区/卷信息:
所以 - 对于物理磁盘,我有这个命令:
Get-Disk
Result :
问题:
我想结合这两个命令。我想在每个磁盘下面看到磁盘,和 - 它的逻辑磁盘信息:
- 磁盘编号 1:....(信息)
>
其逻辑磁盘信息..... - 2 号磁盘:....(信息)
>
这是逻辑磁盘信息..... - 3 号磁盘:....(信息)
>
这是逻辑磁盘信息..... - 等...
我如何结合这两个查询?
您需要查询多个 WMI classes 才能获得您想要的所有信息。
Win32_DiskDrive
为您提供有关物理磁盘的信息。Win32_DiskPartition
为您提供有关物理磁盘上分区的信息。Win32_LogicalDisk
为您提供有关分区内文件系统的信息。
可以使用 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
}
}
}
}
这样怎么样...
Get-CimInstance Win32_Diskdrive -PipelineVariable disk |
Get-CimAssociatedInstance -ResultClassName Win32_DiskPartition -pv partition |
Get-CimAssociatedInstance -ResultClassName Win32_LogicalDisk |
Select-Object @{n='Disk';e={$disk.deviceid}},
@{n='DiskSize';e={$disk.size}},
@{n='DiskModel';e={$disk.model}},
@{n='Partition';e={$partition.name}},
@{n='RawSize';e={$partition.size}},
@{n='DriveLetter';e={$_.DeviceID}},
VolumeName,Size,FreeSpace
输出:
Disk : \.\PHYSICALDRIVE0
DiskSize : 128034708480
DiskModel : SAMSUNG MZ7PC128HAFU-000L5
Partition : Disk #0, Partition #0
RawSize : 128034595328
DriveLetter : C:
VolumeName : DISK
Size : 128034594816
FreeSpace : 7023042560
不幸的是,这不适用于多个驱动器。 get-cimassociatedinstance 和sort-object 一样块,只设置最新的管道变量。这是一个解决方法:
Get-CimInstance Win32_Diskdrive -PipelineVariable disk |
% { Get-CimAssociatedInstance $_ -ResultClass Win32_DiskPartition -pv partition}|
% { Get-CimAssociatedInstance $_ -ResultClassName Win32_LogicalDisk } |
Select-Object @{n='Disk';e={$disk.deviceid}},
@{n='DiskSize';e={$disk.size}},
@{n='DiskModel';e={$disk.model}},
@{n='Partition';e={$partition.name}},
@{n='RawSize';e={$partition.size}},
@{n='DriveLetter';e={$_.DeviceID}},
VolumeName,Size,FreeSpace
Disk : \.\PHYSICALDRIVE0
DiskSize : 128034708480
DiskModel : SAMSUNG MZ7PC128HAFU-000L5
Partition : Disk #0, Partition #0
RawSize : 128034595328
DriveLetter : C:
VolumeName : DISK
Size : 128034594816
FreeSpace : 4226514944
Disk : \.\PHYSICALDRIVE1
DiskSize : 7797565440
DiskModel : USB Flash Memory USB Device
Partition : Disk #1, Partition #0
RawSize : 7801405440
DriveLetter : E:
VolumeName : WINPE
Size : 7784628224
FreeSpace : 7222669312
启发了 js2010 的答案,并进行了一些改进:
例如,Manufacturer
字段在从 Win32_DiskDrive
实例中检索时似乎有一些占位符值,但在使用 Get-Disk
commandlet 时具有正确的值。
function Get-Drive {
foreach($disk in Get-CimInstance Win32_Diskdrive) {
$diskMetadata = Get-Disk | Where-Object { $_.Number -eq $disk.Index } | Select-Object -First 1
$partitions = Get-CimAssociatedInstance -ResultClassName Win32_DiskPartition -InputObject $disk
foreach($partition in $partitions) {
$drives = Get-CimAssociatedInstance -ResultClassName Win32_LogicalDisk -InputObject $partition
foreach($drive in $drives) {
$totalSpace = [math]::Round($drive.Size / 1GB, 3)
$freeSpace = [math]::Round($drive.FreeSpace / 1GB, 3)
$usedSpace = [math]::Round($totalSpace - $freeSpace, 3)
$volume = Get-Volume |
Where-Object { $_.DriveLetter -eq $drive.DeviceID.Trim(":") } |
Select-Object -First 1
[PSCustomObject] @{
DriveLetter = $drive.DeviceID
Number = $disk.Index
Label = $volume.FileSystemLabel
Manufacturer = $diskMetadata.Manufacturer
Model = $diskMetadata.Model
SerialNumber = $diskMetadata.SerialNumber.Trim()
Name = $disk.Caption
FileSystem = $volume.FileSystem
PartitionKind = $diskMetadata.PartitionStyle
TotalSpace = $totalSpace
FreeSpace = $freeSpace
UsedSpace = $usedSpace
Drive = $drive
Partition = $partition
Disk = $disk
}
}
}
}
}
用法示例:
Get-Drive | Format-List
Get-Drive | Where-Object { $_.DriveLetter -eq 'G:' }
输出:
DriveLetter : G:
Number : 5
Label : SE-LXY2-298GB
Manufacturer : Seagate
Model : FreeAgent Go
SerialNumber : 2GE45CK1
Name : Seagate FreeAgent Go USB Device
FileSystem : NTFS
PartitionKind : MBR
TotalSpace : 298.089
FreeSpace : 297.865
UsedSpace : 0.224
Drive : Win32_LogicalDisk: G: (DeviceID = "G:")
Partition : Win32_DiskPartition: Disk #5, Partition #0 (DeviceID = "Disk #5, Partition #0")
Disk : Win32_DiskDrive: Seagate FreeAgent Go USB Device (DeviceID = "\.\PHYSICALDRIVE5")