I have the following script:
$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
This currently returns the information if there is only 1 'Physical Disk'.
{
"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
}
However when I have 2 or more Physical Disks like a USB Stick, I get the following error:
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
I know this is because the PSCustomObject only returns the first value, how do I get it to return multiple devices like this:
{
"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
}
Thanks in advance, Kyle
CodePudding user response:
Pipe the output from Get-PhysicalDisk
to 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
CodePudding user response:
you need to use foreach.
I would suggest something like this bellow (not a tested, you may need to adjust !!!)
$DISKPhysicals = Get-PhysicalDisk
$DiskResults = foreach ($DISKPhysical in $DISKPhysicals )
{
$DiskErrors = Get-Disk -FriendlyName $DISKPhysical.FriendlyName | Get-StorageReliabilityCounter
[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