我有此查询扫描所有逻辑磁盘信息:
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
问题:
我想在这两个命令之间进行组合。我想在每个磁盘下面看到磁盘,和 - 它的逻辑磁盘信息:
>
其逻辑磁盘信息..... >
它的逻辑磁盘信息..... >
它的逻辑磁盘信息..... 如何在这两个查询之间进行组合?
答案 0 :(得分:23)
您需要查询多个WMI类以获取所需的所有信息。
Win32_DiskDrive
为您提供有关物理磁盘的信息。Win32_DiskPartition
为您提供有关物理磁盘上分区的信息。Win32_LogicalDisk
为您提供有关分区内文件系统的信息。可以使用Win32_DiskDriveToDiskPartition
类将分区映射到其磁盘,并且可以通过Win32_LogicalDiskToPartition
类将驱动器映射到其分区。
Get-WmiObject Win32_DiskDrive | % {
$disk = $_
$partitions = "ASSOCIATORS OF " +
"{Win32_DiskDrive.DeviceID='$($disk.DeviceID)'} " +
"WHERE AssocClass = Win32_DiskDriveToDiskPartition"
Get-WmiObject -Query $partitions | % {
$partition = $_
$drives = "ASSOCIATORS OF " +
"{Win32_DiskPartition.DeviceID='$($partition.DeviceID)'} " +
"WHERE AssocClass = Win32_LogicalDiskToPartition"
Get-WmiObject -Query $drives | % {
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
}
}
}
}
答案 1 :(得分:1)
这样怎么样...
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
答案 2 :(得分:0)
受 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")
答案 3 :(得分:-2)
我是这样做的:
[String]([Math] :: Round($ _。尺寸/ 1GB,2))+' GB'