我有以下腳本:
$DISKPhysical = Get-PhysicalDisk
$DiskErrors = Get-Disk | Get-StorageReliabilityCounter
$DiskResults = [PSCustomObject]@{
'Device ID' = $DISKPhysical.DeviceID
'Model' = $DISKPhysical.Model
'Serial Number' = $DISKPhysical.SerialNumber
'Connector Type' = $DISKPhysical.BusType
'Disk Type' = $DISKPhysical.MediaType
'Health Status' = $DISKPhysical.HealthStatus
'Size' = '{0:n0} GB' -f ($DISKPhysical.size/1GB)
'PowerOnHours' = $DiskErrors.PowerOnHours
'Read Errors' = $DiskErrors.ReadErrorsUncorrected
'Wear' = $DiskErrors.Wear
'Temperature' = $DiskErrors.Temperature
}
$DiskResults | ConvertTo-JSON
如果只有 1 個“物理磁盤”,則當前返回信息。
{
"Device ID": "0",
"Model": "Micron_1100_MTFDDAK256TBN",
"Serial Number": "18301DD5EF41",
"Connector Type": "SATA",
"Disk Type": "SSD",
"Health Status": "Healthy",
"Size": "238 GB",
"PowerOnHours": 895,
"Read Errors": 0,
"Wear": 0,
"Temperature": 26
}
但是,當我有 2 個或更多物理磁盤(如 USB 記憶棒)時,出現以下錯誤:
Method invocation failed because [System.Object[]] does not contain a method named 'op_Division'.
At line:4 char:5
+ $DiskResults = [PSCustomObject]@{
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Division:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
我知道這是因為 PSCustomObject 只返回第一個值,我如何讓它返回多個設備,如下所示:
{
"Device ID": "0",
"Model": "Micron_1100_MTFDDAK256TBN",
"Serial Number": "123456789",
"Connector Type": "SATA",
"Disk Type": "SSD",
"Health Status": "Healthy",
"Size": "238 GB",
"PowerOnHours": 895,
"Read Errors": 0,
"Wear": 0,
"Temperature": 26
},
{
"Device ID": "1",
"Model": "Cruzer Edge",
"Serial Number": "123456789",
"Connector Type": "USB",
"Disk Type": "Unspecified",
"Health Status": "Healthy",
"Size": "30 GB",
"PowerOnHours": null,
"Read Errors": null,
"Wear": 0,
"Temperature": 0
}
提前致謝,凱爾
通過管道將輸出從Get-PhysicalDisk
到ForEach-Object
:
$DiskResults = Get-PhysicalDisk |ForEach-Object {
# Fetch operational details only for this disk
$DiskErrors = Get-Disk -UniqueId $_.UniqueId | Get-StorageReliabilityCounter
[PSCustomObject]@{
'Device ID' = $_.DeviceID
'Model' = $_.Model
'Serial Number' = $_.SerialNumber
'Connector Type' = $_.BusType
'Disk Type' = $_.MediaType
'Health Status' = $_.HealthStatus
'Size' = '{0:n0} GB' -f ($_.size/1GB)
'PowerOnHours' = $DiskErrors.PowerOnHours
'Read Errors' = $DiskErrors.ReadErrorsUncorrected
'Wear' = $DiskErrors.Wear
'Temperature' = $DiskErrors.Temperature
}
}
$DiskResults | ConvertTo-JSON
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句