Disk Space monitoring can be something crucial to your environment and knowing when you might need a clean up or more space is something every Administrator would want.

There a little script that can help you monitor diskspace and will send you an email. This can be scheduled and sent to you once a week or month.

—–

diskspace.ps1

—–

# Script Disk Space
$users = “user@example.com” # List of users to email your report to (separate by comma)
$fromemail = “user@example.com
$server = “smtpserver” #enter your own SMTP server DNS name / IP address here
$list = $args[0] #This accepts the argument you add to your scheduled task for the list of servers. i.e. list.txt
$computers = get-content $list #grab the names of the servers/computers to check from the list.txt file.
$mydate = get-date
# Set free disk space threshold below in percent (default at 10%)
[decimal]$thresholdspace = 10

#assemble together all of the free disk space data from the list of servers and only include it if the percentage free is below the threshold we set above.
$tableFragment= Get-WMIObject -ComputerName $computers Win32_LogicalDisk `
| select __SERVER, DriveType, VolumeName, Name, @{n=’Size (Gb)’ ;e={“{0:n2}” -f ($_.size/1gb)}},@{n=’FreeSpace (Gb)’;e={“{0:n2}” -f ($_.freespace/1gb)}}, @{n=’PercentFree’;e={“{0:n2}” -f ($_.freespace/$_.size*100)}} `
| Where-Object {$_.DriveType -eq 3 -and [decimal]$_.PercentFree -lt [decimal]$thresholdspace} `
| ConvertTo-HTML -fragment

# assemble the HTML for our body of the email report.
$HTMLmessage = @”
<font color=””black”” face=””Arial, Verdana”” size=””3″”>
<u><b>Disk Space Storage Report</b></u>
<br>This report was generated because the drive(s) listed below have less than $thresholdspace % free space. Drives above this threshold will not be listed.
<br>
<br>
<style type=””text/css””>body{font: .8em “”Lucida Grande””, Tahoma, Arial, Helvetica, sans-serif;}
ol{margin:0;padding: 0 1.5em;}
table{color:#FFF;background:#C00;border-collapse:collapse;width:647px;border:5px solid #900;}
thead{}
thead th{padding:1em 1em .5em;border-bottom:1px dotted #FFF;font-size:120%;text-align:left;}
thead tr{}
td{padding:.5em 1em;}
tfoot{}
tfoot td{padding-bottom:1.5em;}
tfoot tr{}
#middle{background-color:#900;}
</style>
<body BGCOLOR=””white””>
$tableFragment
<br>
Generated on $mydate
<br>
</body>
“@

# Set up a regex search and match to look for any <td> tags in our body. These would only be present if the script above found disks below the threshold of free space.
# We use this regex matching method to determine whether or not we should send the email and report.
$regexsubject = $HTMLmessage
$regex = [regex] ‘(?im)<td>’

# if there was any row at all, send the email
if ($regex.IsMatch($regexsubject)) {
send-mailmessage -from $fromemail -to $users -subject “Disk Space Monitoring Report” -BodyAsHTML -body $HTMLmessage -priority High -smtpServer $server

 

#####################################################
diskspace.bat
#####################################################

powershell.exe -command “& ‘C:\DiskSpace\diskspace.ps1’ ‘C:\DiskSpace\list.txt'”

 

#####################################################
Install Instructions
#####################################################

1. Edit the diskspace.ps1 PowerShell File
2. Edit the $users parameter and enter the emails the report should be sent to seperated by comma.
3. Specify the SMTP Server on the $server parameter.
4. Specify the Threshold for low diskspace warning – Default at 10%
5. Save and Close

6. Copy the DiskSpace to the C Drive of the Server
7. If you decide to change the location of the DiskSpace Folder, Edit the diskspace.bat file and specify the new location

8. Create the list.txt file and input all servers to be monitored, each on a line.

9. Scheduled the diskspace.bat file to be run month or weekly via Scheduled Task of Windows.