Parsing nested JSON data, needing help with foreach and nested if statement

by May 21, 2019

I am new to powershell, having mainly used one liners and simple scripts. I am attempting to generate a csv file based on JSON data I am collecting from over 1000 drives. I generate my json data with another tool and then I want to parse it to give me the following

 Model Number, Serial Number, FirmwareVersion, The attribute value of smart ID 202

the first three values I have no problem obtaining, its when I need to get the attribute value of smart I 202 that I am running into issues.

So far my code looks like this:
$mydata = Get-Content -Raw .mytest.json |convertfrom-json
$mysmartID = $mydata.smartdata.attributeID
$mysmartValue = $mydata.smartdata.attributeValue
$mySerialNum = $mydata.serialnumber
$myModelNum = $mydata.modelnumber
$myFWVersion = $mydata.FirmwareVersion

I think I need to write a foreach statement that has a nested IF statement to look for AttributeID = 202, then write the attributeValue of that object.  Am I on the right track in my thinking.

My attempts so far have resulted in writing all of the attributeValue for each drive, or not writing anything at all –  when I need the attributevalue of just the 202 nested data.

ForEach ($ssd in $mySmartID)
{If($mySmartID -eq '202')
{write-host $_.attributeValue}
ELSE {Write-Host "Not Found"}
}

This is the dataset I am working with.

[{
"deviceName": "mraid0:0",
"deviceStatus": "Drive is in good health",
"serialNumber": "163513ED2E11",
"modelNumber": "Micron_1100_MTFDDAK256TBN",
"firmwareVersion": "M0MA031",
"driveType": "SATA",
"driveDensityGB": "150.00GB",
"bootDrive": "false",
"isMicron" : "true",
"driverInfo" : {
"hostName": "WIN-T8STVD9OAE3",
"osVersion":"Windows Server 2016 Standard Evaluation (build 14393), 64-bit",
"driverName": "PercSas3i.sys",
"driverVersion": "6.603.06.00"
},
"smartData" : [{
"attributeID":"1",
"attributeDescription":"Raw Read Error Rate",
"attributeValue":"100",
"attributeRawData":"0",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"5",
"attributeDescription":"Reallocated NAND Block Count",
"attributeValue":"100",
"attributeRawData":"0",
"attributeWorst":"100",
"attributeThreshold":"10"
},
{
"attributeID":"9",
"attributeDescription":"Power On Hours Count",
"attributeValue":"100",
"attributeRawData":"9499",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"12",
"attributeDescription":"Power Cycle Count",
"attributeValue":"100",
"attributeRawData":"40",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"171",
"attributeDescription":"Program Fail Count",
"attributeValue":"100",
"attributeRawData":"0",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"172",
"attributeDescription":"Erase Fail Count",
"attributeValue":"100",
"attributeRawData":"0",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"173",
"attributeDescription":"Block Wear-Leveling Count",
"attributeValue":"99",
"attributeRawData":"23",
"attributeWorst":"99",
"attributeThreshold":"0"
},
{
"attributeID":"174",
"attributeDescription":"Unexpected Power Loss Count",
"attributeValue":"100",
"attributeRawData":"29",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"180",
"attributeDescription":"Unused Reserved Block Count",
"attributeValue":"0",
"attributeRawData":"2034",
"attributeWorst":"0",
"attributeThreshold":"0"
},
{
"attributeID":"183",
"attributeDescription":"SATA Interface Downshift",
"attributeValue":"100",
"attributeRawData":"0",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"184",
"attributeDescription":"Error Correction Count",
"attributeValue":"100",
"attributeRawData":"0",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"187",
"attributeDescription":"Reported Uncorrectable Errors",
"attributeValue":"100",
"attributeRawData":"0",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"194",
"attributeDescription":"Enclosure Temperature",
"attributeValue":"71",
"attributeRawData":"188979478557",
"attributeWorst":"56",
"attributeThreshold":"0"
},
{
"attributeID":"196",
"attributeDescription":"Reallocation Event Count",
"attributeValue":"100",
"attributeRawData":"0",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"197",
"attributeDescription":"Current Pending ECC Count",
"attributeValue":"100",
"attributeRawData":"0",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"198",
"attributeDescription":"SMART Off-line Scan Uncorrectable Errors",
"attributeValue":"100",
"attributeRawData":"0",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"199",
"attributeDescription":"Ultra-DMA CRC Error Count",
"attributeValue":"100",
"attributeRawData":"0",
"attributeWorst":"100",
"attributeThreshold":"1"
},
{
"attributeID":"202",
"attributeDescription":"Percentage Lifetime Remaining",
"attributeValue":"99",
"attributeRawData":"1",
"attributeWorst":"99",
"attributeThreshold":"0"
},
{
"attributeID":"206",
"attributeDescription":"Write Error Rate",
"attributeValue":"100",
"attributeRawData":"0",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"210",
"attributeDescription":"RAIN Successful Recovery Page Count",
"attributeValue":"100",
"attributeRawData":"0",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"246",
"attributeDescription":"Cumulative Host Sectors Written",
"attributeValue":"100",
"attributeRawData":"5866734062",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"247",
"attributeDescription":"Host Program Page Count",
"attributeValue":"100",
"attributeRawData":"183683520",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"248",
"attributeDescription":"FTL Program Page Count",
"attributeValue":"100",
"attributeRawData":"105642473",
"attributeWorst":"100",
"attributeThreshold":"0"
}
]
},
{
"deviceName": "mraid0:1",
"deviceStatus": "Drive is in good health",
"serialNumber": "171216BE1427",
"modelNumber": "MTFDDAK480TCB",
"firmwareVersion": "D0MU058",
"driveType": "SATA",
"driveDensityGB": "480.00GB",
"bootDrive": "false",
"isMicron" : "true",
"driverInfo" : {
"hostName": "WIN-T8STVD9OAE3",
"osVersion":"Windows Server 2016 Standard Evaluation (build 14393), 64-bit",
"driverName": "PercSas3i.sys",
"driverVersion": "6.603.06.00"
},
"smartData" : [{
"attributeID":"1",
"attributeDescription":"Raw Read Error Rate",
"attributeValue":"100",
"attributeRawData":"0",
"attributeWorst":"100",
"attributeThreshold":"50"
},
{
"attributeID":"5",
"attributeDescription":"Reallocated NAND Block Count",
"attributeValue":"100",
"attributeRawData":"0",
"attributeWorst":"100",
"attributeThreshold":"1"
},
{
"attributeID":"9",
"attributeDescription":"Power On Hours Count",
"attributeValue":"100",
"attributeRawData":"10266",
"attributeWorst":"100",
"attributeThreshold":"1"
},
{
"attributeID":"12",
"attributeDescription":"Power Cycle Count",
"attributeValue":"100",
"attributeRawData":"28",
"attributeWorst":"100",
"attributeThreshold":"1"
},
{
"attributeID":"170",
"attributeDescription":"Reserved Block Count",
"attributeValue":"100",
"attributeRawData":"0",
"attributeWorst":"100",
"attributeThreshold":"10"
},
{
"attributeID":"171",
"attributeDescription":"Program Fail Count",
"attributeValue":"100",
"attributeRawData":"0",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"172",
"attributeDescription":"Erase Fail Count",
"attributeValue":"100",
"attributeRawData":"0",
"attributeWorst":"100",
"attributeThreshold":"1"
},
{
"attributeID":"173",
"attributeDescription":"Block Wear-Leveling Count",
"attributeValue":"100",
"attributeRawData":"22",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"174",
"attributeDescription":"Unexpected Power Loss Count",
"attributeValue":"100",
"attributeRawData":"26",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"180",
"attributeDescription":"Unused Reserved Block Count",
"attributeValue":"0",
"attributeRawData":"7765",
"attributeWorst":"0",
"attributeThreshold":"0"
},
{
"attributeID":"183",
"attributeDescription":"SATA Interface Downshift",
"attributeValue":"100",
"attributeRawData":"0",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"184",
"attributeDescription":"Error Correction Count",
"attributeValue":"100",
"attributeRawData":"0",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"187",
"attributeDescription":"Reported Uncorrectable Errors",
"attributeValue":"100",
"attributeRawData":"0",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"188",
"attributeDescription":"Command Time Out Count",
"attributeValue":"100",
"attributeRawData":"14",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"194",
"attributeDescription":"Enclosure Temperature",
"attributeValue":"72",
"attributeRawData":"150325100572",
"attributeWorst":"65",
"attributeThreshold":"0"
},
{
"attributeID":"195",
"attributeDescription":"Cumulative Corrected ECC",
"attributeValue":"100",
"attributeRawData":"0",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"196",
"attributeDescription":"Reallocation Event Count",
"attributeValue":"100",
"attributeRawData":"0",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"197",
"attributeDescription":"Current Pending ECC Count",
"attributeValue":"100",
"attributeRawData":"0",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"198",
"attributeDescription":"SMART Off-line Scan Uncorrectable Errors",
"attributeValue":"100",
"attributeRawData":"0",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"199",
"attributeDescription":"Ultra-DMA CRC Error Count",
"attributeValue":"100",
"attributeRawData":"0",
"attributeWorst":"100",
"attributeThreshold":"1"
},
{
"attributeID":"202",
"attributeDescription":"Percentage Lifetime Remaining",
"attributeValue":"100",
"attributeRawData":"0",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"206",
"attributeDescription":"Write Error Rate",
"attributeValue":"100",
"attributeRawData":"0",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"210",
"attributeDescription":"RAIN Success Recover Page Count",
"attributeValue":"100",
"attributeRawData":"0",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"246",
"attributeDescription":"Cumulative Host Sectors Written",
"attributeValue":"100",
"attributeRawData":"17587248172",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"247",
"attributeDescription":"Host Program Page Count",
"attributeValue":"100",
"attributeRawData":"549603006",
"attributeWorst":"100",
"attributeThreshold":"0"
},
{
"attributeID":"248",
"attributeDescription":"FTL Program Page Count",
"attributeValue":"100",
"attributeRawData":"442450651",
"attributeWorst":"100",
"attributeThreshold":"0"
}
]