2024-01-25 13:37:12 +01:00
|
|
|
Script: *check-smart-devices.ps1*
|
|
|
|
========================
|
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-05-19 10:25:56 +02:00
|
|
|
✅ 1TB Samsung SSD 970 EVO via NVMe (37°C, 2388 hours, 289x on/off, v2B2QEXE7, selftest 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 {
|
2023-12-07 20:24:45 +01: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" }
|
|
|
|
|
2023-12-07 20:24:45 +01: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-05-19 10:25:56 +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-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) {
|
|
|
|
$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)"
|
2022-11-17 20:02:26 +01:00
|
|
|
}
|
2024-05-19 10:25:56 +02:00
|
|
|
Write-Progress -completed " "
|
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-05-19 10:25:56 +02:00
|
|
|
*(generated by convert-ps2md.ps1 using the comment-based help of check-smart-devices.ps1 as of 05/19/2024 10:25:18)*
|