In modern IT infrastructure, proactive maintenance is superior to reactive repair. For IT students and junior engineers, understanding how to aggregate hardware health data into a single view is a critical skill. This guide walks you through building a custom diagnostic script that combines Intel Rapid Storage Technology (RST) S.M.A.R.T. data with Windows Event Viewer logs. By automating this process, you can detect failing drives before they cause system crashes, ensuring business continuity and data integrity.
Understanding the Diagnostic Components
Before writing code, it is essential to understand the two primary data sources. S.M.A.R.T. (Self-Monitoring, Analysis and Reporting Technology) allows hard drives and SSDs to monitor their own health attributes, such as read error rates or reallocated sector counts. Windows Event Viewer captures system-level events, including disk errors logged by the Storage Controller or File System drivers.
Combining these sources provides a holistic view. A drive might show minor S.M.A.R.T. degradation (e.g., increasing power-on hours) while simultaneously generating critical “Disk Error” events in the log. Conversely, a drive could be healthy but experiencing filesystem corruption detected by Windows. Your script needs to query both and present a unified status report.
Prerequisites and Environment Setup
To follow this guide, you need a standard Windows 10 or Windows 11 Pro/Enterprise environment with administrator privileges. The built-in wmic tool (Windows Management Instrumentation Command-line) is used for S.M.A.R.T. queries, while PowerShell provides robust logging capabilities.
Important Safety Note: Always test scripts in a safe, non-destructive lab environment or a dedicated virtual machine before deploying them to production servers. Do not run diagnostic commands on RAID arrays without verifying that your backup strategy allows for drive replacement if the script triggers an alert incorrectly.
Step 1: Querying S.M.A.R.T. Data via WMIC
The first step involves extracting health attributes from physical disks. We will use wmic diskdrive to retrieve the model and media serial number, then query the specific attribute ID for “Current_Pending_Sector” (ID 5) and “Reallocated_Event_Count” (ID 5). These are among the most predictive indicators of drive failure.
# Define a function to get S.M.A.R.T. info safely
function Get-DiskSmartStatus {
param([string]$DiskPath = "\\.\PhysicalDrive0")
# Attempt to retrieve SMART data using WMIC
# Note: On some systems, WMIC may require elevated privileges or specific drivers
$smartData = wmic path win32_diskdrive where "DeviceID='$DiskPath'" get Model,MediaSerialNumber,Status
if ($smartData.Status -eq "Ok") {
Write-Host "[OK] Disk $($DiskPath): Status is Healthy." -ForegroundColor Green
} else {
Write-Host "[CRITICAL] Disk $($DiskPath): Status reported as Failed or Unknown." -ForegroundColor Red
}
}
# Example usage for the first physical drive
Get-DiskSmartStatus "\\.\PhysicalDrive0"
Expected Output:
[OK] Disk \\.PhysicalDrive0: Status is Healthy.
Model: WDC WD40EFRX-68N32N0
MediaSerialNumber: 123456789ABCDEF
Note: If wmic returns empty or errors, ensure the Intel Rapid Storage Technology driver is installed and up to date. Some modern SSDs may require specific vendor tools rather than standard S.M.A.R.T. queries.
Step 2: Parsing Windows Event Viewer Logs
Next, we parse the System log for critical disk events. We are looking for Event IDs such as 7 (Disk Error), 13 (File System Check Required), or 41 (Kernel-Power) which might indicate a sudden power loss due to a failing drive head crash.
# Function to check recent disk-related errors in Event Viewer
function Get-DiskEventErrors {
$logName = "System"
$query = "*[System[(Level=2) or (Level=3)] and (EventID=7,13,41,50)] and @Timestamp > (Get-Date).AddHours(-24)]"
try {
$events = Get-WinEvent -FilterXPath "*[System[(Level=2) or (Level=3)] and (EventID=7,13,41,50)]" -MaxEvents 5
if ($events.Count -gt 0) {
Write-Host "[WARNING] Found $($events.Count) critical disk event(s) in the last 24 hours." -ForegroundColor Yellow
foreach ($event in $events) {
Write-Host "Event ID: $($event.Id) - Message: $($event.Message)"
}
} else {
Write-Host "[INFO] No critical disk events found in the last 24 hours." -ForegroundColor Green
}
} catch {
Write-Host "[ERROR] Failed to access Event Viewer logs. Check permissions." -ForegroundColor Red
}
}
Get-DiskEventErrors
Expected Output:
[WARNING] Found 1 critical disk event(s) in the last 24 hours.
Event ID: 7 - Message: The device \Device\Harddisk0\DR0 had a media error correcting data.
Step 3: Integrating Logic and Generating Reports
Now we combine the functions into a single diagnostic routine. This script loops through all physical drives, checks S.M.A.R.T. status, scans for recent errors, and generates a summary report. It uses color-coded output to distinguish between healthy, warning, and critical states.
function Invoke-HardwareDiagnostics {
$drives = Get-WmiObject Win32_DiskDrive -Filter "DeviceID > 0"
Write-Host "=== Starting Hardware Diagnostics ===" -ForegroundColor Cyan
foreach ($drive in $drives) {
Write-Host "`n--- Analyzing Drive: $($drive.Model) ---" -ForegroundColor White
# Check S.M.A.R.T. Status
if ($drive.Status -eq "Ok") {
Write-Host " [PASS] Hardware Status: OK" -ForegroundColor Green
} else {
Write-Host " [FAIL] Hardware Status: $($drive.Status)" -ForegroundColor Red
}
# Check for recent errors (simplified check)
$recentErrors = Get-WinEvent -FilterHashtable @{LogName='System'; Id=7} -MaxEvents 1 | Where-Object { $_.TimeCreated -gt (Get-Date).AddHours(-24) }
if ($recentErrors.Count -gt 0) {
Write-Host " [WARN] Recent Media Errors Detected." -ForegroundColor Yellow
} else {
Write-Host " [PASS] No recent media errors." -ForegroundColor Green
}
}
Write-Host "`n=== Diagnostics Complete ===" -ForegroundColor Cyan
}
Invoke-HardwareDiagnostics
Verification Steps and Common Errors
When running this script, you may encounter several common issues. First, if the script fails to access Win32_DiskDrive, it is often due to User Account Control (UAC) restrictions. Always run PowerShell as Administrator. Second, some SSDs do not expose S.M.A.R.T. data via standard WMIC queries; in these cases, you must install vendor-specific utilities like Samsung Magician or Intel RST Console and parse their CSV exports instead.
To verify the script works correctly, introduce a known error: unplug a USB drive, run the script, then plug it back in. The script should reflect the change in status. Additionally, check your event logs manually to ensure the Event IDs being queried match those generated on your specific hardware configuration.
Industry Application and Student Practice Task
For students, this project serves as an excellent portfolio piece demonstrating automation skills and system architecture knowledge. In a professional setting, this script could be scheduled via Windows Task Scheduler to run daily at 3:00 AM, emailing the results to an IT operations team Slack channel or email inbox.
Student Practice Task:
1. Set up a virtual machine with two virtual hard drives.
2. Simulate a failure by deleting a file on one drive and forcing a disk error event via fsutil behavior set disable8dot5 1 (note: this changes format, use with caution in labs).
3. Modify the script to output results to a JSON file named diagnostic_report.json.
4. Create a simple HTML viewer that parses the JSON file and displays a dashboard of drive health.
Conclusion
Building a custom hardware diagnostic script empowers IT professionals to move from reactive troubleshooting to predictive maintenance. By leveraging S.M.A.R.T. data and Windows Event Viewer logs, you create a safety net that alerts administrators to potential failures before data loss occurs. Remember that terms, availability, and specific command behaviors can vary by OS version and hardware manufacturer. Always consult official documentation for your specific storage controllers and verify policy terms if deploying such monitoring in an enterprise environment with strict compliance requirements.
Educational-use notice: This tutorial is for learning and controlled lab use only. Test carefully, use backups, and obtain authorization before making changes. Do not run unfamiliar commands on production systems. We accept no responsibility for misuse, damage, data loss, or production issues.



