2024-11-08 12:38:20 +01:00
|
|
|
The *check-smart-devices.ps1* Script
|
|
|
|
===========================
|
2022-11-17 20:02:26 +01:00
|
|
|
|
|
|
|
check-smart-devices.ps1
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Parameters
|
|
|
|
----------
|
2021-11-08 21:36:42 +01:00
|
|
|
```powershell
|
|
|
|
|
|
|
|
|
|
|
|
[<CommonParameters>]
|
|
|
|
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
|
|
|
|
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
|
|
|
|
```
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Script Content
|
|
|
|
--------------
|
2022-11-17 20:05:34 +01:00
|
|
|
```powershell
|
2022-11-17 20:02:26 +01:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
2023-05-26 12:20:18 +02:00
|
|
|
Checks the SMART device status
|
2022-11-17 20:02:26 +01:00
|
|
|
.DESCRIPTION
|
2023-05-26 12:20:18 +02:00
|
|
|
This PowerShell script queries the status of the SSD/HDD devices (supporting S.M.A.R.T.) and prints it.
|
2022-11-17 20:02:26 +01:00
|
|
|
.EXAMPLE
|
2023-08-06 21:36:33 +02:00
|
|
|
PS> ./check-smart-devices.ps1
|
2024-11-08 12:35:11 +01:00
|
|
|
✅ 1TB Samsung SSD 970 EVO via NVMe (37°C, 2388 hours, 289x on/off, v2B2QEXE7, test passed)
|
2022-11-17 20:02:26 +01:00
|
|
|
.LINK
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
.NOTES
|
|
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
#>
|
|
|
|
|
2024-05-19 10:25:56 +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-11-17 20:02:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2024-08-15 09:51:46 +02:00
|
|
|
#Write-Progress "(1/3) Searching for smartmontools..."
|
2024-05-19 10:25:56 +02:00
|
|
|
$result = (smartctl --version)
|
2022-11-17 20:02:26 +01:00
|
|
|
if ($lastExitCode -ne "0") { throw "Can't execute 'smartctl' - make sure smartmontools are installed" }
|
|
|
|
|
2024-08-15 09:51:46 +02:00
|
|
|
#Write-Progress "(2/3) Scanning S.M.A.R.T devices..."
|
2022-11-17 20:02:26 +01:00
|
|
|
if ($IsLinux) {
|
2024-05-19 10:25:56 +02:00
|
|
|
$devices = $(sudo smartctl --scan-open)
|
2022-11-17 20:02:26 +01:00
|
|
|
} else {
|
2024-05-19 10:25:56 +02:00
|
|
|
$devices = $(smartctl --scan-open)
|
2022-11-17 20:02:26 +01:00
|
|
|
}
|
2023-05-26 12:20:18 +02:00
|
|
|
|
2024-08-15 09:51:46 +02:00
|
|
|
#Write-Progress "Querying S.M.A.R.T devices..."
|
2024-05-19 10:25:56 +02:00
|
|
|
foreach($device in $devices) {
|
|
|
|
$array = $device.split(" ")
|
|
|
|
$dev = $array[0]
|
|
|
|
if ("$dev" -eq "#") {
|
2022-11-17 20:02:26 +01:00
|
|
|
continue
|
|
|
|
} elseif ($IsLinux) {
|
2024-05-19 10:25:56 +02:00
|
|
|
$details = (sudo smartctl --all --json $dev) | ConvertFrom-Json
|
|
|
|
$null = (sudo smartctl --test=conveyance $dev)
|
2022-11-17 20:02:26 +01:00
|
|
|
} else {
|
2024-05-19 10:25:56 +02:00
|
|
|
$details = (smartctl --all --json $dev) | ConvertFrom-Json
|
|
|
|
$null = (smartctl --test=conveyance $dev)
|
2022-11-17 20:02:26 +01:00
|
|
|
}
|
2024-05-19 10:25:56 +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-11-17 20:02:26 +01:00
|
|
|
} else {
|
2024-05-19 10:25:56 +02:00
|
|
|
$capacity = ""
|
2022-11-17 20:02:26 +01:00
|
|
|
}
|
2024-05-19 10:25:56 +02:00
|
|
|
if ($details.temperature.current -gt 50) {
|
2024-11-08 12:35:11 +01:00
|
|
|
$temp = "$($details.temperature.current)°C TOO HOT"
|
|
|
|
$status = "⚠️"
|
|
|
|
} elseif ($details.temperature.current -lt 0) {
|
|
|
|
$temp = "$($details.temperature.current)°C TOO COLD"
|
2024-05-19 10:25:56 +02:00
|
|
|
$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) {
|
2024-11-08 12:35:11 +01:00
|
|
|
$selftest = "test passed"
|
2024-05-19 10:25:56 +02:00
|
|
|
} else {
|
2024-11-08 12:35:11 +01:00
|
|
|
$selftest = "test FAILED"
|
2024-05-19 10:25:56 +02:00
|
|
|
$status = "⚠️"
|
|
|
|
}
|
|
|
|
$firmwareVersion = $details.firmware_version
|
2024-11-08 12:35:11 +01:00
|
|
|
Write-Host "$status $capacity$modelName via $protocol ($temp, $hours, $powerOn, v$firmwareVersion, $selftest)"
|
2022-11-17 20:02:26 +01:00
|
|
|
}
|
2024-08-15 09:51:46 +02:00
|
|
|
#Write-Progress -completed "Done."
|
2022-11-17 20:02:26 +01:00
|
|
|
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
|
|
|
|
2024-11-20 11:52:20 +01:00
|
|
|
*(generated by convert-ps2md.ps1 as of 11/20/2024 11:51:52)*
|