Windows有两个PowerShell命令,用于获取有关磁盘Get-PhysicalDisk
和Get-Disk
的信息。
我运行命令Get-PhysicalDisk
,然后尝试使用两个命令的输出中都存在的公用值,将命令的输出与Get-Disk
的结果一起追加,该值是{{1 }}。
我坚信这是一个简单的哈希表要求,我无法解决。
SerialNumber
我的预期结果将是以下各项的结合:
PS> Get-PhysicalDisk | Select OperationalStatus, HealthStatus, BusType, FriendlyName, Manufacturer, SerialNumber OperationalStatus : OK HealthStatus : Healthy BusType : SATA FriendlyName : KINGSTON SV310S37A960G Manufacturer : SerialNumber : 50026B724800A34E
这:
PS> Get-Disk | Where SerialNumber -eq "50026B724800A34E" | select PartitionStyle, ProvisioningType, NumberOfPartitions, Signature | Format-list PartitionStyle : MBR ProvisioningType : Fixed NumberOfPartitions : 2 Signature : 1364200743
给我这个:
OperationalStatus : OK HealthStatus : Healthy BusType : SATA FriendlyName : KINGSTON SV310S37A960G Manufacturer : SerialNumber : 50026B724800A34E PartitionStyle : MBR ProvisioningType : Fixed NumberOfPartitions: 2 Signature : 1364200743
答案 0 :(得分:2)
您可以像这样在循环中构建自定义对象:
foreach ($Disk in $PhysicalDisks) {
$info = Get-Disk |
Where-Object { $Disk.SerialNumber -eq $_.SerialNumber.Trim() }
New-Object -Type PSObject -Property @{
'OperationalStatus' = $Disk.OperationalStatus
'HealthStatus' = $Disk.HealthStatus
'BusType' = $Disk.BusType
'FriendlyName' = $Disk.FriendlyName
'Manufacturer' = $Disk.Manufacturer
'SerialNumber' = $Disk.SerialNumber
'PartitionStyle' = $info.PartitionStyle
'ProvisioningType' = $info.ProvisioningType
'NumberOfPartitions' = $info.NumberOfPartitions
'Signature' = $info.Signature
}
}
但是,创建一个将序列号映射到Get-Disk
数据的哈希表,然后将相关信息添加为计算所得的属性,在其中您可以通过序列号查找信息可能更简单。
$info = @{}
Get-Disk | ForEach-Object {
$info[$_.SerialNumber.Trim()] = $_
}
Get-PhysicalDisk |
Select-Object OperationalStatus, HealthStatus, BusType, FriendlyName,
Manufacturer, SerialNumber,
@{n='PartitionStyle';e={$info[$_.SerialNumber].PartitionStyle}},
@{n='ProvisioningType';e={$info[$_.SerialNumber].ProvisioningType}},
@{n='NumberOfPartitions';e={$info[$_.SerialNumber].NumberOfPartitions}},
@{n='Signature';e={$info[$_.SerialNumber].Signature}}