PowerShell/Docs/check-smart-devices.md

99 lines
2.8 KiB
Markdown
Raw Normal View History

2023-07-29 10:34:04 +02:00
*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
PS> ./check-smart-devices
2023-05-26 12:20:18 +02:00
✅ 1TB Samsung SSD 970 EVO via NVMe (2388 hours, 289x on, v2B2QEXE7, 37°C, selftest passed)
2022-11-17 20:02:26 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
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" }
}
try {
2023-05-26 12:20:18 +02:00
Write-Progress "⏳ (1/3) Searching for smartmontools..."
2022-11-17 20:02:26 +01:00
$Result = (smartctl --version)
if ($lastExitCode -ne "0") { throw "Can't execute 'smartctl' - make sure smartmontools are installed" }
2023-05-26 12:20:18 +02:00
Write-Progress "⏳ (2/3) Scanning S.M.A.R.T devices..."
2022-11-17 20:02:26 +01:00
if ($IsLinux) {
$Devices = $(sudo smartctl --scan-open)
} else {
$Devices = $(smartctl --scan-open)
}
2023-05-26 12:20:18 +02:00
2022-11-17 20:02:26 +01:00
foreach($Device in $Devices) {
2023-05-26 12:20:18 +02:00
Write-Progress "⏳ (3/3) Querying S.M.A.R.T devices..."
2022-11-17 20:02:26 +01:00
$Array = $Device.split(" ")
$Device = $Array[0]
if ("$Device" -eq "#") {
continue
} elseif ($IsLinux) {
$Details = (sudo smartctl --all --json $Device) | ConvertFrom-Json
$null = (sudo smartctl --test=short $Device)
} else {
$Details = (smartctl --all --json $Device) | ConvertFrom-Json
$null = (smartctl --test=short $Device)
}
$ModelName = $Details.model_name
$Protocol = $Details.device.protocol
[int64]$GBytes = $Details.user_capacity.bytes
if ($GBytes -gt 0) {
$Capacity = "$(Bytes2String $GBytes) "
} else {
$Capacity = ""
}
$Temp = $Details.temperature.current
$Firmware = $Details.firmware_version
$PowerOn = $Details.power_cycle_count
$Hours = $Details.power_on_time.hours
if ($Details.smart_status.passed) { $Status = "passed" } else { $Status = "FAILED" }
2023-05-26 12:20:18 +02:00
Write-Progress -completed " "
Write-Host "✅ $($Capacity)$ModelName via $Protocol ($Hours hours, $($PowerOn)x on, v$($Firmware), $($Temp)°C, selftest $Status)"
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
2023-08-06 11:42:46 +02:00
*(generated by convert-ps2md.ps1 using the comment-based help of check-smart-devices.ps1 as of 08/06/2023 11:42:26)*