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" } ]
First what you have posted is not properly formatted JSON. It is missing closing braces and brackets. I corrected in this response and from taht, you can just use the Jason cmdlet to parse the info.
# Get parameters, examples, full and Online help for a cmdlet or function
# Get a list of all ModulesGet-Module -ListAvailable | Out-GridView -PassThru -Title 'Available modules'
# Get a list of all functionsGet-Command -CommandType Function | Out-GridView -PassThru -Title 'Available functions'
# Get a list of all commandletsGet-Command -CommandType Cmdlet | Out-GridView -PassThru -Title 'Available cmdlets'
# Get a list of all functions for the specified nameGet-Command -Name '*json*' -CommandType Function | Out-GridView -PassThru -Title 'Available named functions'
# Get a list of all commandlets for the specified nameGet-Command -Name '*json**' -CommandType Cmdlet | Out-GridView -PassThru -Title 'Available named cmdlet'
# Results
CommandType Name Version Source----------- ---- ------- ------Cmdlet ConvertFrom-Json 3.1.0.0 Microsoft.PowerShell.UtilityCmdlet ConvertTo-Json 3.1.0.0 Microsoft.PowerShell.Utility
# get function / cmdlet detailsGet-Command -Name ConvertFrom-Json -Syntax(Get-Command -Name ConvertFrom-Json).Parameters.KeysGet-help -Name ConvertFrom-Json -FullGet-help -Name ConvertFrom-Json -Online
Get-help -Name ConvertFrom-Json -Examples
Get-Date | Select-Object -Property * | ConvertTo-Json | ConvertFrom-Json
$j = Invoke-WebRequest -Uri http://search.twitter.com/search.json?q=PowerShell | ConvertFrom-Json
(Get-Content JsonFile.JSON) -join "`n" | ConvertFrom-Json
$JSONData = @'[ { "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" } ] }]'@ | ConvertFrom-Json
$JSONData | Out-GridView -Title 'Review JSON data' -PassThru
($JSONData | Select-Object -Property *)[0] | Get-Member
TypeName: Selected.System.Management.Automation.PSCustomObject
Name MemberType Definition ---- ---------- ---------- Equals Method bool Equals(System.Object obj) GetHashCode Method int GetHashCode() GetType Method type GetType() ToString Method string ToString() bootDrive NoteProperty string bootDrive=false deviceName NoteProperty string deviceName=mraid0:0 deviceStatus NoteProperty string deviceStatus=Drive is in good health driveDensityGB NoteProperty string driveDensityGB=150.00GB driverInfo NoteProperty System.Management.Auto...driveType NoteProperty string driveType=SATA firmwareVersion NoteProperty string firmwareVersion=M0MA031 isMicron NoteProperty string isMicron=true modelNumber NoteProperty string modelNumber=Micron_1100_MTFDDAK256TBN serialNumber NoteProperty string serialNumber=163513ED2E11 smartData NoteProperty Object[] smartData=System.Object[]
$JSONData | Select-Object -Property *
So, you smartData is a collection, so, you need calculated property, leveraging Select-String, Sub-String, RegEx match to extact only the attribure you want.
deviceName : mraid0:0deviceStatus : Drive is in good healthserialNumber : 163513ED2E11modelNumber : Micron_1100_MTFDDAK256TBNfirmwareVersion : M0MA031driveType : SATAdriveDensityGB : 150.00GBbootDrive : falseisMicron : truedriverInfo : @{hostName=WIN-T8STVD9OAE3; osVersion=Wind...
deviceName : mraid0:1deviceStatus : Drive is in good healthserialNumber : 171216BE1427modelNumber : MTFDDAK480TCBfirmwareVersion : D0MU058driveType : SATAdriveDensityGB : 480.00GBbootDrive : falseisMicron : truedriverInfo : @{hostName=WIN-T8STVD9OAE3; osVersion=Wind...smartData : {@{attributeID=1; attributeDescription=Raw Rea...
ForEach-Object { $JSONData | Select-Object -ExpandProperty smartData} | Out-GridView -Title 'Showing smartData attributes'
attributeID : 1attributeDescription : Raw Read Error RateattributeValue : 100attributeRawData : 0attributeWorst : 100attributeThreshold : 0
attributeID : 5attributeDescription : Reallocated NAND Block CountattributeValue : 100attributeRawData : 0attributeWorst : 100attributeThreshold : 10
attributeID : 9attributeDescription : Power On Hours CountattributeValue : 100attributeRawData : 9499attributeWorst : 100attributeThreshold : 0...
# Getting stuff from a collectionForEach-Object { $JSONData | Select-Object -ExpandProperty smartData | Where attributeID -eq '202'} | Out-GridView -Title 'Showing smartData attributes matching 202'
202 Percentage Lifetime Remaining 99 1 99 0 202 Percentage Lifetime Remaining 100 0 100 0
# Select and using a calulated property for smartData informationForEach-Object { $JSONData | Select-Object -Property deviceName, deviceStatus, serialNumber, modelNumber, firmwareVersion, driveType, driveDensityGB, bootDrive, isMicron, @{Name = 'SmartDataAttributeID';Expression = {$PSItem.smartData.attributeID -eq '202'}}}
# or if this IF filter is needed, then this
ForEach-Object {
If ($JSONData.smartData.attributeID -Match '202' ) { $JSONData | Select-Object -Property deviceName, deviceStatus, serialNumber, modelNumber, firmwareVersion, driveType, driveDensityGB, bootDrive, isMicron, @{Name = 'SmartDataAttributeID';Expression = {$PSItem.smartData.attributeID -eq '202'}} }}
# Results are the same for both options
deviceName : mraid0:0deviceStatus : Drive is in good healthserialNumber : 163513ED2E11modelNumber : Micron_1100_MTFDDAK256TBNfirmwareVersion : M0MA031driveType : SATAdriveDensityGB : 150.00GBbootDrive : falseisMicron : trueSmartDataAttributeID : 202
deviceName : mraid0:1deviceStatus : Drive is in good healthserialNumber : 171216BE1427modelNumber : MTFDDAK480TCBfirmwareVersion : D0MU058driveType : SATAdriveDensityGB : 480.00GBbootDrive : falseisMicron : trueSmartDataAttributeID : 202
@postanote thank you for the reply.
You are correct about the missing brackets on the JSON data, I did not want to post the entire fire, so I used one element (drive) from the file. I appreciate your detailed response and will get to implementing it. Thank you again for your help!
Now worries. Glad it provided some edification on what can be done. Hope all things work out. Walking thru JSON and XML can be really a straight forward thing, but there are times, you just end up pulling your hair out to get it where you want it. I have very little to no hair left... ;-} So, there's hat, from my time with it all.