PowerShell/Scripts/check-smart-devices.ps1

57 lines
1.7 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-16 10:39:31 +02:00
This PowerShell script queries and prints your S.M.A.R.T. HDD/SSD devices.
2021-10-03 21:32:17 +02:00
.EXAMPLE
PS> ./check-smart-devices
2022-10-25 19:06:35 +02:00
Device HFM256GD3JX016N via NVMe, 238GB, 126x on, 71h, 29°C, 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
#>
try {
2022-10-25 19:06:35 +02:00
Write-Progress "⏳ Step 1/3 - 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" }
2022-10-16 10:39:31 +02:00
Write-Progress "⏳ Step 2/3 - Scanning S.M.A.R.T devices..."
if ($IsLinux) {
$Devices = $(sudo smartctl --scan-open)
} else {
$Devices = $(smartctl --scan-open)
}
2021-10-03 21:32:17 +02:00
foreach($Device in $Devices) {
2022-10-16 10:39:31 +02:00
Write-Progress "⏳ Step 3/3 - Querying S.M.A.R.T devices..."
2021-10-03 21:32:17 +02:00
$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
} else {
$Details = (smartctl --all --json $Device) | ConvertFrom-Json
}
$ModelName = $Details.model_name
2022-10-21 16:55:41 +02:00
$Protocol = $Details.device.protocol
[int]$GBytes = $Details.user_capacity.bytes / 1GB
if ($GBytes -eq 0) {
$Capacity = ""
} else {
2022-10-25 19:23:15 +02:00
$Capacity = "($GBytes GB) "
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
if ($Details.smart_status.passed) { $Status = "passed" } else { $Status = "NOT PASSED" }
2022-10-25 19:23:15 +02:00
"✅ Device $ModelName $($Capacity)via $Protocol, v$($Firmware), $($PowerOn)x on, $($Hours) hours, $($Temp)°C, $Status."
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
}