-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQL Drive Space Alerts.ps1
35 lines (28 loc) · 1.4 KB
/
SQL Drive Space Alerts.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
<# borrowed code from here:
https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Script-Sample-f7164554
https://sqlrus.com/2015/07/validating-cluster-instance-owner-with-powershell/
#>
$File = @("server1","server2", "server3", "server4")
$DiskReport = ForEach ($Servernames in ($File))
{Get-WmiObject win32_logicaldisk `
-ComputerName $servernames -Filter "Drivetype=3" `
-ErrorAction SilentlyContinue |
Where-Object { ($_.freespace/$_.size) -le '0.10'} #10% free space. Adjust accordingly
}
$body = $DiskReport |
Select-Object @{Label = "Server Name";Expression = {$_.SystemName}},
@{Label = "Drive Letter";Expression = {$_.DeviceID}},
@{Label = "Total Capacity (GB)";Expression = {"{0:N1}" -f( $_.Size / 1gb)}},
@{Label = "Free Space (GB)";Expression = {"{0:N1}" -f( $_.Freespace / 1gb ) }},
@{Label = 'Free Space (%)'; Expression = {"{0:P0}" -f ($_.freespace/$_.size)}} | convertto-html -head $style | out-string
$messageParameters = @{
Subject = "Low Disk Space Alert"
Body = $body
From = "[email protected]"
To = "[email protected]"
SmtpServer = "smtp_address_goes_here"
}
IF ($DiskReport.count -gt 0){
Send-MailMessage @messageParameters -BodyAsHtml
}