PowerShell/scripts/check-smart-devices.ps1

98 lines
2.9 KiB
PowerShell
Raw Normal View History

2023-10-31 12:05:32 +01:00
<#
2021-10-03 21:32:17 +02:00
.SYNOPSIS
2023-04-11 10:48:59 +02:00
Checks the SMART device status
2021-10-03 21:32:17 +02:00
.DESCRIPTION
2023-04-11 10:48:59 +02:00
This PowerShell script queries the status of the SSD/HDD devices (supporting S.M.A.R.T.) and prints it.
2021-10-03 21:32:17 +02:00
.EXAMPLE
2023-08-06 21:35:36 +02:00
PS> ./check-smart-devices.ps1
2024-04-28 15:29:23 +02:00
1TB Samsung SSD 970 EVO via NVMe (37°C, 2388 hours, 289x on/off, v2B2QEXE7, selftest passed)
2021-10-03 21:32:17 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-01-29 12:47:46 +01:00
.NOTES
2022-09-06 21:42:04 +02:00
Author: Markus Fleschutz | License: CC0
2021-10-03 21:32:17 +02:00
#>
2024-04-28 15:29:23 +02:00
function Bytes2String([int64]$bytes) {
if ($bytes -lt 1000) { return "$bytes bytes" }
$bytes /= 1000
if ($bytes -lt 1000) { return "$($bytes)KB" }
$bytes /= 1000
if ($bytes -lt 1000) { return "$($bytes)MB" }
$bytes /= 1000
if ($bytes -lt 1000) { return "$($bytes)GB" }
$bytes /= 1000
if ($bytes -lt 1000) { return "$($bytes)TB" }
$bytes /= 1000
if ($bytes -lt 1000) { return "$($bytes)PB" }
$bytes /= 1000
if ($bytes -lt 1000) { return "$($bytes)EB" }
2022-10-27 16:55:32 +02:00
}
2021-10-03 21:32:17 +02:00
try {
Write-Progress "(1/3) Searching for smartmontools..."
2024-04-28 15:29:23 +02:00
$result = (smartctl --version)
2021-10-03 21:32:17 +02:00
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..."
2022-10-16 10:39:31 +02:00
if ($IsLinux) {
2024-04-28 15:29:23 +02:00
$devices = $(sudo smartctl --scan-open)
2022-10-16 10:39:31 +02:00
} else {
2024-04-28 15:29:23 +02:00
$devices = $(smartctl --scan-open)
2022-10-16 10:39:31 +02:00
}
2024-04-28 15:29:23 +02:00
Write-Progress "(3/3) Querying S.M.A.R.T devices..."
foreach($device in $devices) {
$array = $device.split(" ")
$dev = $array[0]
if ("$dev" -eq "#") {
2022-10-17 09:15:12 +02:00
continue
} elseif ($IsLinux) {
2024-04-28 15:29:23 +02:00
$details = (sudo smartctl --all --json $dev) | ConvertFrom-Json
2024-04-28 15:56:17 +02:00
$null = (sudo smartctl --test=conveyance $dev)
2022-10-16 10:39:31 +02:00
} else {
2024-04-28 15:29:23 +02:00
$details = (smartctl --all --json $dev) | ConvertFrom-Json
2024-04-28 15:56:17 +02:00
$null = (smartctl --test=conveyance $dev)
2022-10-16 10:39:31 +02:00
}
2024-04-28 15:29:23 +02:00
$status = ""
$modelName = $details.model_name
$protocol = $details.device.protocol
[int64]$bytes = $details.user_capacity.bytes
if ($bytes -gt 0) {
$capacity = "$(Bytes2String $bytes) "
2022-10-21 16:55:41 +02:00
} else {
2024-04-28 15:29:23 +02:00
$capacity = ""
2022-10-21 16:55:41 +02:00
}
2024-04-28 15:29:23 +02:00
if ($details.temperature.current -gt 50) {
$temp = "$($details.temperature.current)°C (!)"
$status = "⚠️"
} else {
$temp = "$($details.temperature.current)°C"
}
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)"
2021-10-03 21:32:17 +02:00
}
2024-04-28 15:29:23 +02:00
Write-Progress -completed " "
2021-10-03 21:32:17 +02:00
exit 0 # success
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-10-03 21:32:17 +02:00
exit 1
2022-11-01 16:43:00 +01:00
}