This repository has been archived by the owner on Aug 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-SMARTReport.ps1
137 lines (106 loc) · 4.49 KB
/
Get-SMARTReport.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<#
.SYNOPSIS
Generates a report regarding the SMART status of physical drives on the system.
.DESCRIPTION
The report will include a warning when one or more physical drives return a SMART status other than 'OK'.
.EXAMPLE
Powershell "C:\scripts\Get-SMARTReport\Get-SMARTReport.ps1"
Runs the Get-SMARTReport.ps1 script in an instance of PowerShell.
.EXAMPLE
Get-SMARTReport >> "C:\logs\smart-report.log"
Runs the Get-SMARTReport module, appending the output to the specified log file.
.LINK
https://github.com/joeltimothyoh/Get-SMARTReport
#>
########################## Email Settings ###########################
# SMTP Server
$smtp_server = 'smtp.server.com'
# SMTP port (usually 465 or 587)
$smtp_port = '587'
# SMTP email address
$smtp_email = '[email protected]'
# SMTP email password
$smtp_password = 'Password'
# Source email address (usually matching SMTP email address)
$email_from = '[email protected]'
# Destination email address
$email_to = '[email protected]'
# Email title prefix
$email_title_prefix = '[MachineName]'
#########################################################################
function Get-SMARTReport {
[CmdletBinding()]
Param()
try {
# Get info of each physical drive on system
$Physical_Drives_Info = Get-WmiObject -Class Win32_DiskDrive | Sort-Object DeviceID
# Count the number of physical drives with a SMART status other than 'OK'
$faulty_drives = $Physical_Drives_Info | Where-Object { $_.Status -ne 'OK'}
# Generate a table containing each physical drive with their respective capacities and SMART statuses
$smart_status_table = $Physical_Drives_Info | Format-Table `
DeviceID, # DeviceID for physical drive unique identifier
Model, # Model for physical drive model name
# MediaType, # MediaType for physical drive type
# SerialNumber, # SerialNumber for physical drive serial number
@{ Name = "Size (GB)"; Expression = { [math]::Round($_.Size/1GB,1) } }, # Size for physical drive storage capacity
@{ Name = "SMART Status"; Expression = { $_.Status }; Alignment="right" } # Status for physical drive SMART status
# Trim newlines in the formatted table
$smart_status_table = ($smart_status_table | Out-String).Trim()
# Module name to appear in title
$module_name = "[Get-SMARTReport]"
# Format title of report
$title = "$module_name "
if ($faulty_drives.Count -gt 0) {
$title += "$($faulty_drives.Count) Drive(s) may be faulty/failing."
} else {
$title += "No problems found."
}
# Format body of report
$smart_status_report = @(
"Smart Status Check as of: $(Get-Date)"
$smart_status_table
)
# Print report to stdout
Write-Output $title
Write-Output $smart_status_report
Write-Output "-"
# Format title of email report
$email_title_prefix = $email_title_prefix.Trim()
if ($email_title_prefix -ne "") {
$email_title = "$email_title_prefix $title"
} else {
$email_title = $title
}
# Format body of email report
$email_body = @(
"<html><pre style='font-family: Courier New; font-size: 11px;'>"
$smart_status_report
"</pre></html>"
)
# Secure credential
$encrypted_password = $smtp_password | ConvertTo-SecureString -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential( $smtp_email, $encrypted_password )
# Define Send-MailMessage parameters
$emailprm = @{
SmtpServer = $smtp_server
Port = $smtp_port
UseSsl = $true
Credential = $credential
From = $email_from
To = $email_to
Subject = $email_title
Body = ($email_body | Out-String)
BodyAsHtml = $true
}
# Email the report
try {
Send-MailMessage @emailprm -ErrorAction Stop
} catch {
Write-Output "Failed to send email. Reason: $($_.Exception.Message)"
}
} catch {
throw
}
}
# Call main function
Get-SMARTReport