PowerShell/Scripts/check-smart-devices.ps1

77 lines
2.4 KiB
PowerShell
Raw Normal View History

2021-10-03 21:32:17 +02:00
<#
.SYNOPSIS
2022-10-25 19:06:35 +02:00
Checks SMART devices
2021-10-03 21:32:17 +02:00
.DESCRIPTION
2022-10-28 09:45:24 +02:00
This PowerShell script queries S.M.A.R.T. HDD/SSD device details and prints it.
2021-10-03 21:32:17 +02:00
.EXAMPLE
PS> ./check-smart-devices
2023-01-01 19:22:35 +01:00
1TB Samsung SSD 970 EVO via NVMe (2388 hours, 289x on, v2B2QEXE7, 37°C, 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
#>
2022-10-27 16:55:32 +02:00
function Bytes2String { param([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" }
}
2021-10-03 21:32:17 +02:00
try {
2023-01-01 19:22:35 +01:00
Write-Progress "⏳ Searching for smartctl executable..."
2022-10-16 10:39:31 +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" }
2023-01-01 19:22:35 +01:00
Write-Progress "⏳ Scanning S.M.A.R.T devices..."
2022-10-16 10:39:31 +02:00
if ($IsLinux) {
$Devices = $(sudo smartctl --scan-open)
} else {
$Devices = $(smartctl --scan-open)
}
Write-Progress "⏳ Querying S.M.A.R.T devices..."
2021-10-03 21:32:17 +02:00
foreach($Device in $Devices) {
$Array = $Device.split(" ")
$Device = $Array[0]
2022-10-17 09:15:12 +02:00
if ("$Device" -eq "#") {
continue
} elseif ($IsLinux) {
2022-10-16 10:39:31 +02:00
$Details = (sudo smartctl --all --json $Device) | ConvertFrom-Json
2022-10-28 09:45:24 +02:00
$null = (sudo smartctl --test=short $Device)
2022-10-16 10:39:31 +02:00
} else {
$Details = (smartctl --all --json $Device) | ConvertFrom-Json
2022-10-28 09:45:24 +02:00
$null = (smartctl --test=short $Device)
2022-10-16 10:39:31 +02:00
}
$ModelName = $Details.model_name
2022-10-21 16:55:41 +02:00
$Protocol = $Details.device.protocol
2022-10-27 16:55:32 +02:00
[int64]$GBytes = $Details.user_capacity.bytes
if ($GBytes -gt 0) {
$Capacity = "$(Bytes2String $GBytes) "
2022-10-21 16:55:41 +02:00
} else {
2022-10-27 16:55:32 +02:00
$Capacity = ""
2022-10-21 16:55:41 +02:00
}
2022-10-16 10:39:31 +02:00
$Temp = $Details.temperature.current
2022-10-21 16:55:41 +02:00
$Firmware = $Details.firmware_version
2022-10-16 10:39:31 +02:00
$PowerOn = $Details.power_cycle_count
$Hours = $Details.power_on_time.hours
2022-10-25 19:45:02 +02:00
if ($Details.smart_status.passed) { $Status = "passed" } else { $Status = "FAILED" }
2023-01-01 19:22:35 +01:00
Write-Host "$($Capacity)$ModelName via $Protocol ($Hours hours, $($PowerOn)x on, v$($Firmware), $($Temp)°C, selftest $Status)"
2021-10-03 21:32:17 +02:00
}
Write-Progress -completed "Querying S.M.A.R.T devices finished."
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
}