PowerShell/scripts/smart-data2csv.ps1

99 lines
2.7 KiB
PowerShell
Raw Normal View History

2024-10-01 13:37:53 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2021-10-04 21:29:23 +02:00
Converts the S.M.A.R.T. JSON files in a folder to a CSV table for analysis
2021-07-13 21:10:02 +02:00
.DESCRIPTION
2022-01-30 10:49:30 +01:00
This PowerShell script converts the S.M.A.R.T. JSON files in the current/given directory
to a CSV table for analysis (use query-smart-data.ps1 to generate those JSON files).
2021-10-16 16:50:10 +02:00
.PARAMETER Directory
Specifies the path to the directory
2021-07-13 21:10:02 +02:00
.EXAMPLE
2021-09-24 17:19:49 +02:00
PS> ./smart-data2csv
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-01-30 10:49:30 +01:00
.NOTES
2022-09-06 21:42:04 +02:00
Author: Markus Fleschutz | License: CC0
2021-02-04 20:44:13 +01:00
#>
2021-07-15 15:51:22 +02:00
param([string]$Directory = "")
2021-02-04 20:44:13 +01:00
function WriteCsvHeader { param([PSCustomObject]$File)
2021-02-06 18:04:42 +01:00
foreach($Entry in $File.ata_smart_attributes.table) {
[int]$ID = $Entry.id
$Name = $Entry.name
write-host -nonewline "$Name ($ID);"
2021-02-04 20:44:13 +01:00
}
write-host ""
}
2021-02-06 18:04:42 +01:00
function WriteCsvDataRow { param([PSCustomObject]$File)
foreach($Entry in $File.ata_smart_attributes.table) {
[int]$ID = $Entry.id
switch ($ID) {
1 { write-host -nonewline "$($Entry.raw.value);" }
4 { write-host -nonewline "$($Entry.raw.value);" }
7 { write-host -nonewline "$($Entry.raw.value);" }
9 { write-host -nonewline "$($Entry.raw.value);" }
12 { write-host -nonewline "$($Entry.raw.value);" }
190 { write-host -nonewline "$($Entry.raw.string);" }
191 { write-host -nonewline "$($Entry.raw.value);" }
192 { write-host -nonewline "$($Entry.raw.value);" }
193 { write-host -nonewline "$($Entry.raw.value);" }
195 { write-host -nonewline "$($Entry.raw.value);" }
240 { write-host -nonewline "$($Entry.raw.string);" }
241 { write-host -nonewline "$($Entry.raw.value);" }
242 { write-host -nonewline "$($Entry.raw.value);" }
default { write-host -nonewline "$($Entry.value);" }
}
2021-02-04 20:44:13 +01:00
}
write-host ""
}
try {
2021-02-07 15:01:08 +01:00
if ($Directory -eq "" ) {
$Directory = "$PWD"
2021-02-04 20:44:13 +01:00
}
2021-02-07 15:01:08 +01:00
$Filenames = get-childitem -path "$Directory/SMART*.json"
2021-02-04 20:44:13 +01:00
$ModelFamily = $ModelName = $SerialNumber = ""
2021-02-06 18:04:42 +01:00
[int]$Row = 1
2021-02-04 20:44:13 +01:00
foreach($Filename in $Filenames) {
$File = get-content $Filename | ConvertFrom-Json
if ($File.model_family -ne $ModelFamily) {
if ($ModelFamily -eq "") {
$ModelFamily = $File.model_family
} else {
write-error "Different model families: $ModelFamily vs. $($File.model_family)"
exit 1
}
}
if ($File.model_name -ne $ModelName) {
if ($ModelName -eq "") {
$ModelName = $File.model_name
} else {
write-error "Different model names: $ModelName vs. $($File.model_name)"
exit 1
}
}
if ($File.serial_number -ne $SerialNumber) {
if ($SerialNumber -eq "") {
$SerialNumber = $File.serial_number
} else {
write-error "Different serial numbbers: $SerialNumber vs. $($File.serial_number)"
exit 1
}
}
2021-02-06 18:04:42 +01:00
if ($Row -eq 1) {
WriteCsvHeader $File
}
WriteCsvDataRow $File
$Row++
2021-02-04 20:44:13 +01:00
}
2021-09-27 10:09:45 +02:00
exit 0 # success
2021-02-04 20:44:13 +01:00
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-02-04 20:44:13 +01:00
exit 1
}