PowerShell Disk Space Monitoring and Email Alert

Overview

This PowerShell script monitors disk space on a list of servers and sends an email alert if the free space on any drive falls below a specified threshold. It retrieves disk space information from remote servers using WMI (Windows Management Instrumentation) and sends an email notification with details of the disk space.

Features

  • Checks disk space on multiple servers.
  • Sends an email alert when free space falls below a specified percentage threshold.
  • Customizable SMTP server settings for sending email notifications.
  • HTML-formatted email with drive details and a clear alert message.

Pre-requisites

  • PowerShell 5.1 or higher (for compatibility with Send-MailMessage and Get-WmiObject cmdlets).
  • Access to the remote servers for querying disk space via WMI.
  • An SMTP email server for sending notifications (e.g., Gmail, SMTP server).
  • A list of server names saved in a text file (one per line).

Script Configuration

1. Server List Configuration

The script reads a list of server names from a text file (serverlist.txt). Each line of the file should contain one server name.

Example of serverlist.txt:

Server1

Server2

Server3

Modify the following line in the script to specify the correct path to your server list file:

powershell

$servers = Get-Content -Path "G:\Healthcheck-report\serverlist.txt"

2. Threshold Configuration

You can set the threshold for disk space as a percentage. When the free space on any drive falls below this threshold, an alert email will be sent.

Modify the following line to set the desired threshold (default is 10%):

powershell

$thresholdPercentage = 20

3. SMTP Server Configuration

The script uses an SMTP server to send email alerts. Modify the following lines with your SMTP server details and credentials.

  • SMTP Server: The address of your SMTP server (e.g., smtp.gmail.com for Gmail).
  • SMTP User: The email address used to authenticate with the SMTP server.
  • SMTP Password: The app password for the SMTP account (for Gmail, use an app-specific password if 2FA is enabled).

powershell

$smtpServer = "smtp.gmail.com"

$smtpFrom = "emailid"

$smtpTo = "emailid"

$smtpUser = "emailid"

$smtpPassword = "your-app-password"

Script Logic and Functionality

1. Reading the Server List

The script starts by reading a list of server names from a text file (serverlist.txt), which contains one server name per line. The Get-Content cmdlet is used to read the content into the $servers variable.

2. Checking Disk Space

For each server in the list:

  • The script uses the Get-WmiObject cmdlet to retrieve disk space information (Win32_LogicalDisk class) for all local drives (DriveType = 3).
  • The script calculates the free space in GB and the total disk space in GB.
  • It also calculates the free space as a percentage.

3. Sending Email Alerts

If any drive has free space below the configured threshold, an email alert is generated and sent with the following information:

  • Server Name
  • Drive Letter/ID
  • Free Space (in GB)
  • Free Space Percentage
  • Total Disk Space (in GB)

The email is formatted in HTML to highlight the alert in red and bold, making it easy to spot.

4. Email Content

The email content includes:

  • A bold and red-colored message indicating the alert.
  • Information on the free space and total space of the drive.
  • A note at the bottom indicating that this is an auto-generated message.

Example alert message:

html

<html>

    <body>

        <p style="font-weight: bold; color: red;">Alert: Disk space on <strong>Server1</strong> - Drive <strong>C:</strong> is below the threshold!</p>

        <p>Free Space: <strong>5.0 GB</strong> (5%) out of <strong>100 GB</strong>.</p>

        <p><br>NOTE: This is auto-generated mail</p>

        <p>Thank you,</p>

        <p>DBA Reports</p>

    </body>

</html>

5. Handling Errors

  • The script catches errors in case it fails to connect to a server and displays a message (Failed to connect to $serverName).
  • If a server name is invalid (null or empty), the script prints an error message (Invalid server name: $serverName) and skips that entry.

6. Running the Script

The script loops through each server in the list and checks the disk space for each one. If the space is below the threshold, the script sends an alert email.

How to Run the Script

  1. Save the PowerShell script as Check-DiskSpace.ps1.
  2. Ensure that the serverlist.txt file is in the correct location and contains valid server names.
  3. Modify the SMTP server and authentication settings in the script.
  4. Open a PowerShell window with administrator privileges.
  5. Run the script by executing:

powershell

.\Check-DiskSpace.ps1

Additional Considerations

  • Security: It’s important to keep your email password (or app password) secure. If possible, store it securely using a password manager or secure string methods.
  • Error Handling: If you're running this script for a large number of servers, ensure proper error handling is in place to avoid unnecessary email alerts for servers that may be temporarily unreachable.
  • Scheduling: You can schedule this script to run periodically using Task Scheduler in Windows to automate the disk space checks.

Troubleshooting

  • Failed to connect to server: Check that the server is reachable and that you have the necessary permissions to query disk information using WMI.
  • Invalid server name: Ensure that the server names in the serverlist.txt file are correct and formatted properly (no extra spaces or empty lines).
  • Email not sent: Verify that the SMTP server settings are correct and that the account has the necessary permissions (e.g., app password for Gmail if 2FA is enabled).

This documentation template explains the purpose, configuration, and usage of the script, along with key sections for troubleshooting and additional considerations. It can be expanded further with specific company policies or additional customization options.

 

No comments:

Post a Comment

Popular Posts