PowerShell/Docs/smart-data2csv.md

139 lines
3.8 KiB
Markdown
Raw Normal View History

2022-12-04 10:40:18 +01:00
## The *smart-data2csv.ps1* Script
2021-11-08 21:36:42 +01:00
2022-02-10 09:01:07 +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-11-08 21:36:42 +01:00
## Parameters
```powershell
2023-05-26 12:20:18 +02:00
/home/mf/Repos/PowerShell/Scripts/smart-data2csv.ps1 [[-Directory] <String>] [<CommonParameters>]
2021-11-08 21:36:42 +01:00
-Directory <String>
Specifies the path to the directory
Required? false
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
## Example
```powershell
PS> ./smart-data2csv
```
## Notes
2022-11-17 19:46:02 +01:00
Author: Markus Fleschutz | License: CC0
2021-11-08 21:36:42 +01:00
## Related Links
https://github.com/fleschutz/PowerShell
2022-11-17 20:02:26 +01:00
## Source Code
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.SYNOPSIS
Converts the S.M.A.R.T. JSON files in a folder to a CSV table for analysis
.DESCRIPTION
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).
.PARAMETER Directory
Specifies the path to the directory
.EXAMPLE
PS> ./smart-data2csv
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$Directory = "")
function WriteCsvHeader { param([PSCustomObject]$File)
foreach($Entry in $File.ata_smart_attributes.table) {
[int]$ID = $Entry.id
$Name = $Entry.name
write-host -nonewline "$Name ($ID);"
}
write-host ""
}
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);" }
}
}
write-host ""
}
try {
if ($Directory -eq "" ) {
$Directory = "$PWD"
}
$Filenames = get-childitem -path "$Directory/SMART*.json"
$ModelFamily = $ModelName = $SerialNumber = ""
[int]$Row = 1
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
}
}
if ($Row -eq 1) {
WriteCsvHeader $File
}
WriteCsvDataRow $File
$Row++
}
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
2022-11-17 20:05:34 +01:00
```
2022-11-17 20:02:26 +01:00
2021-11-08 21:36:42 +01:00
*Generated by convert-ps2md.ps1 using the comment-based help of smart-data2csv.ps1*