Improved check-smart-devices.ps1

This commit is contained in:
Markus Fleschutz 2024-04-28 15:29:23 +02:00
parent 5f573540a5
commit c9867174cd

View File

@ -5,70 +5,91 @@
This PowerShell script queries the status of the SSD/HDD devices (supporting S.M.A.R.T.) and prints it. This PowerShell script queries the status of the SSD/HDD devices (supporting S.M.A.R.T.) and prints it.
.EXAMPLE .EXAMPLE
PS> ./check-smart-devices.ps1 PS> ./check-smart-devices.ps1
1TB Samsung SSD 970 EVO via NVMe (2388 hours, 289x on, v2B2QEXE7, 37°C, selftest passed) 1TB Samsung SSD 970 EVO via NVMe (37°C, 2388 hours, 289x on/off, v2B2QEXE7, selftest passed)
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
.NOTES .NOTES
Author: Markus Fleschutz | License: CC0 Author: Markus Fleschutz | License: CC0
#> #>
function Bytes2String { param([int64]$Bytes) function Bytes2String([int64]$bytes) {
if ($Bytes -lt 1000) { return "$Bytes bytes" } if ($bytes -lt 1000) { return "$bytes bytes" }
$Bytes /= 1000 $bytes /= 1000
if ($Bytes -lt 1000) { return "$($Bytes)KB" } if ($bytes -lt 1000) { return "$($bytes)KB" }
$Bytes /= 1000 $bytes /= 1000
if ($Bytes -lt 1000) { return "$($Bytes)MB" } if ($bytes -lt 1000) { return "$($bytes)MB" }
$Bytes /= 1000 $bytes /= 1000
if ($Bytes -lt 1000) { return "$($Bytes)GB" } if ($bytes -lt 1000) { return "$($bytes)GB" }
$Bytes /= 1000 $bytes /= 1000
if ($Bytes -lt 1000) { return "$($Bytes)TB" } if ($bytes -lt 1000) { return "$($bytes)TB" }
$Bytes /= 1000 $bytes /= 1000
if ($Bytes -lt 1000) { return "$($Bytes)PB" } if ($bytes -lt 1000) { return "$($bytes)PB" }
$Bytes /= 1000 $bytes /= 1000
if ($Bytes -lt 1000) { return "$($Bytes)EB" } if ($bytes -lt 1000) { return "$($bytes)EB" }
} }
try { try {
Write-Progress "(1/3) Searching for smartmontools..." Write-Progress "(1/3) Searching for smartmontools..."
$Result = (smartctl --version) $result = (smartctl --version)
if ($lastExitCode -ne "0") { throw "Can't execute 'smartctl' - make sure smartmontools are installed" } if ($lastExitCode -ne "0") { throw "Can't execute 'smartctl' - make sure smartmontools are installed" }
Write-Progress "(2/3) Scanning S.M.A.R.T devices..." Write-Progress "(2/3) Scanning S.M.A.R.T devices..."
if ($IsLinux) { if ($IsLinux) {
$Devices = $(sudo smartctl --scan-open) $devices = $(sudo smartctl --scan-open)
} else { } else {
$Devices = $(smartctl --scan-open) $devices = $(smartctl --scan-open)
} }
foreach($Device in $Devices) { Write-Progress "(3/3) Querying S.M.A.R.T devices..."
Write-Progress "(3/3) Querying S.M.A.R.T devices..." foreach($device in $devices) {
$Array = $Device.split(" ") $array = $device.split(" ")
$Device = $Array[0] $dev = $array[0]
if ("$Device" -eq "#") { if ("$dev" -eq "#") {
continue continue
} elseif ($IsLinux) { } elseif ($IsLinux) {
$Details = (sudo smartctl --all --json $Device) | ConvertFrom-Json $details = (sudo smartctl --all --json $dev) | ConvertFrom-Json
$null = (sudo smartctl --test=short $Device) $null = (sudo smartctl --test=short $dev)
} else { } else {
$Details = (smartctl --all --json $Device) | ConvertFrom-Json $details = (smartctl --all --json $dev) | ConvertFrom-Json
$null = (smartctl --test=short $Device) $null = (smartctl --test=short $dev)
} }
$ModelName = $Details.model_name $status = ""
$Protocol = $Details.device.protocol $modelName = $details.model_name
[int64]$GBytes = $Details.user_capacity.bytes $protocol = $details.device.protocol
if ($GBytes -gt 0) { [int64]$bytes = $details.user_capacity.bytes
$Capacity = "$(Bytes2String $GBytes) " if ($bytes -gt 0) {
$capacity = "$(Bytes2String $bytes) "
} else { } else {
$Capacity = "" $capacity = ""
} }
$Temp = $Details.temperature.current if ($details.temperature.current -gt 50) {
$Firmware = $Details.firmware_version $temp = "$($details.temperature.current)°C (!)"
$PowerOn = $Details.power_cycle_count $status = "⚠️"
$Hours = $Details.power_on_time.hours } else {
if ($Details.smart_status.passed) { $Status = "passed" } else { $Status = "FAILED" } $temp = "$($details.temperature.current)°C"
Write-Progress -completed " " }
Write-Host "$($Capacity)$ModelName via $Protocol ($Hours hours, $($PowerOn)x on, v$($Firmware), $($Temp)°C, selftest $Status)" if ($details.power_on_time.hours -gt 87600) { # 10 years
$hours = "$($details.power_on_time.hours) hours (!)"
$status = "⚠️"
} else {
$hours = "$($details.power_on_time.hours) hours"
}
if ($details.power_cycle_count -gt 100000) {
$powerOn = "$($details.power_cycle_count)x on/off (!)"
$status = "⚠️"
} else {
$powerOn = "$($details.power_cycle_count)x on/off"
}
if ($details.smart_status.passed) {
$selftest = "selftest passed"
} else {
$selftest = "selftest FAILED"
$status = "⚠️"
}
$firmwareVersion = $details.firmware_version
Write-Host "$status $capacity$modelName via $protocol ($temp, $hours, $powerOn, $selftest, v$firmwareVersion)"
} }
Write-Progress -completed " "
exit 0 # success exit 0 # success
} catch { } catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"