SQL Talent SQLServer DiskSpaceMonitorPS

DiskSpaceMonitorPS

Windows Drive Space Monitoring Using PowerShell Script

Purpose:

To automate disk space monitoring on a Windows server and send email alerts when free space falls below a defined threshold. This helps ensure system stability and prevent outages due to storage issues.

Files and Configuration

Script: DiskSpaceMonitor.ps1

This script checks all fixed drives (DriveType = 3), calculates the free space percentage, and sends an HTML email if any drive falls below a specified threshold.

Key Configurable Parameters:

  • $smtpServer – SMTP server to relay email (e.g., mail.company.com).
  • $from – Sender email address.
  • $to – Recipient(s) email address.
  • $threshold – Minimum acceptable free space percentage (e.g., 20%).

Ensure this file is saved in a known path, e.g., C:\Monitoring\DiskSpaceMonitor.ps1.

Scripts:-

# DiskSpaceMonitor.ps1
# Configuration
$smtpServer = “*******.****.****”
$from = “*******@****.****”
$to = “*******@****.****”
$cc = “*******@****.****”
$subject = “Disk Space Alert on $env:COMPUTERNAME”
$threshold = 20 # percentage

# Get disk space info
$drives = Get-WmiObject Win32_LogicalDisk -Filter “DriveType = 3″
$alertDrives = @()

foreach ($drive in $drives) {
$freePercent = [math]::Round(($drive.FreeSpace / $drive.Size) * 100, 2)
if ($freePercent -lt $threshold) {
$alertDrives += @{
Drive = $drive.DeviceID
SizeGB = [math]::Round($drive.Size / 1GB, 2)
FreeGB = [math]::Round($drive.FreeSpace / 1GB, 2)
FreePercent = $freePercent
}
}
}

# Build HTML body if any drive is low
if ($alertDrives.Count -gt 0) {
$body = @”
<html>
<head>
<style>
table {border-collapse: collapse; width: 100%;}
th, td {border: 1px solid #ccc; padding: 8px; text-align: left;}
th {background-color: #f2f2f2;}

.low {background-color: #f28b82;} /* Red */
.warn {background-color: #fff3cd;} /* Orange */
</style>
</head>
<body>
<p>Dear Team,</p>
<p><b>Report As On:</b> $(Get-Date -Format “dd MMM yyyy HH:mm”)</p>
<p><b>Warning!</b> Low disk space detected on server <b>$env:COMPUTERNAME</b>:</p>
<table>
<tr><th>Drive</th><th>Total Size (GB)</th><th>Free Space (GB)</th><th>Free Space (%)</th></tr>
“@

foreach ($drive in $alertDrives) {
$rowClass = “”
if ($drive.FreePercent -lt 10) {
$rowClass = “low”
} elseif ($drive.FreePercent -le 20) {
$rowClass = “warn”
}

$body += “<tr class=’$rowClass’><td>$($drive.Drive)</td><td>$($drive.SizeGB)</td><td>$($drive.FreeGB)</td><td>$($drive.FreePercent)%</td></tr>`n”
}

$body += “</table><p>Regards,<br/>Automated Disk Monitor – By DBA Team</p></body></html>”

# Send the email
Send-MailMessage -From $from -To $to -Cc $cc -Subject $subject -Body $body -BodyAsHtml -SmtpServer $smtpServer
}

Prerequisites

  1. SMTP Relay Access

    • Ensure port 25 (SMTP) is open to the SMTP server ($smtpServer) from this server.
    • Confirm that the sending address ($from) is authorized to send through this relay.
    • Validate DNS resolution and firewall rules for the mail server.
  1. Execution Policy

    • Run the following in PowerShell (as Administrator) to allow the script to run:

      Set-ExecutionPolicy RemoteSigned
      
      

    3.Test Script Manually Before automating, test manually:

PowerShell.exe -File “C:\Monitoring\DiskSpaceMonitor.ps1”

Schedule the Script in Task Scheduler

  1. Open Task Scheduler
    Press Win + R, type taskschd.msc, and press Enter.

  2. Create a Basic Task

    • Name: Disk Space Monitor
    • Description: Alerts if disk space is low on the server.
  1. Trigger

    • Choose Daily or Repeat every X hours depending on your need.
  1. Action

    • Choose Start a program
    • Program/script:
PowerShell.exe
  • Add arguments:
-ExecutionPolicy Bypass -File “C:\Monitoring\DiskSpaceMonitor.ps1”

5.Finish and Save

Testing and Logs

  • After scheduling, right-click the task and click Run to verify execution.
  • Check for emails and review the Task Scheduler History for failures.
  • Optionally, add logging inside the script to output to a .log file.

Email Output Sample

Dear Team,

Report As On: 01 May 2025 13:12
Warning! Low disk space detected on server SRV01:

Drive | Total Size (GB) | Free Space (GB) | Free Space (%)
C:    | 100             | 15              | 15.00
D:    | 200             | 25              | 12.50

Regards,
Automated Disk Monitor – By IT Team

Maintenance Tips

  • Update recipient list as needed.
  • Re-test SMTP if mail stops working (relay or DNS may change).
  • Update threshold based on system growth trends.

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post