在PowerShell中合并“获取磁盘”信息和“逻辑磁盘”信息?
我有一个扫描所有逻辑磁盘信息的查询:在PowerShell中合并“获取磁盘”信息和“逻辑磁盘”信息?,powershell,filesystems,drive,Powershell,Filesystems,Drive,我有一个扫描所有逻辑磁盘信息的查询: 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,SerialN
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
:
但是-我需要物理磁盘信息及其分区/卷信息:
所以-对于物理磁盘,我有以下命令:
获取磁盘
:
问题:
我想在这两个命令之间进行组合。我想查看每个磁盘下方的磁盘,和——其逻辑磁盘信息:
- 磁盘号1:…(信息)
其逻辑磁盘信息 - 磁盘号2:…(信息)
这是逻辑磁盘信息 - 磁盘编号3:…(信息)
这是逻辑磁盘信息 - 等等
如何在这两个查询之间进行组合 您需要查询多个WMI类以获取所需的所有信息
- 提供有关物理磁盘的信息
- 提供有关物理磁盘上分区的信息
- 提供有关分区内文件系统的信息
获取WmiObject Win32_DiskDrive | ForEach对象{
$disk=$_
$partitions=“的关联者”+
“{Win32_DiskDrive.DeviceID='$($disk.DeviceID)}”+
“其中AssocClass=Win32\u DiskDriveToDiskPartition”
获取wmioobject-查询$partitions | ForEach对象{
$partition=$_
$drives=“的关联者”+
“{Win32_DiskPartition.DeviceID='$($partition.DeviceID)}”+
“其中AssocClass=Win32_LogicalDiskToPartition”
获取WmiObject-查询$drives | ForEach对象{
新对象-类型PSCustomObject-属性@{
Disk=$Disk.DeviceID
DiskSize=$disk.Size
DiskModel=$disk.Model
Partition=$Partition.Name
RawSize=$partition.Size
DriveLetter=$\设备ID
VolumeName=$\卷名
大小=$\大小
自由空间=$\自由空间
}
}
}
}
像这样怎么样
Get-CimInstance Win32_Diskdrive -PipelineVariable disk |
Get-CimAssociatedInstance -ResultClassName Win32_DiskPartition -PipelineVariable 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
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")
启发了js2010的答案,并进行了一些改进: 例如,
Manufacturer
字段在从Win32\u DiskDrive
实例检索时似乎有一些占位符值,但在使用Get Disk
命令时有适当的值
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:' }
输出:
Disk : \\.\PHYSICALDRIVE0
DiskSize : 128034708480
DiskModel : SAMSUNG MZ7PC128HAFU-000L5
Partition : Disk #0, Partition #0
RawSize : 128034595328
DriveLetter : C:
VolumeName : DISK
Size : 128034594816
FreeSpace : 7023042560
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")
不久前我有一个类似的问题,不知道这个答案是否有用:@Kev谢谢,但对我帮助不大。驱动器号是分区的产物。我不想从驱动器号转到它的属性。我想从物理磁盘的------>它们的容量开始。正是我需要的。谢谢。最后一个问题:我已经设法用GB显示了信息。所以这个数字很好。但是我想添加文本后缀(“
(GB)
”),比如123.45(GB)
。但我可能遗漏了什么。我应该换什么?看这里:如果你想要一个显示为xgb
的数字,你必须将它转换成字符串,例如'{0:d}GB'-f[int]($\u.Size/1GB)
是否可以通过系统提示符下的wmic命令执行此操作?解决了!如果要对项目列表进行排序,请使用“[PSCustomObject][ORDERED]@{”而不是“New Object-Type PSCustomObject-Property@{”