Power shell script to get the report of free drive space and run the scheduler task for daily alert.

Create power shell script to get the server drive space report that server drive space threshold value is less then 10%. you will get the alert of low space drive type. Create the schedule task of the alert of low space drive of server.

First of all we will create power shell script for the low space in server drive. For get the alert of low disk space you will require From address, To address and SMTP address.

follow this step to get the report of low disk space.

Step 1 : Open PowerShell.exe

Step 2 : Create folder which is located at C:\diskspacereport.

Step 3 : Create power shell file and keep the details which is marked as red color text.

You will need a PowerShell script editor to edit this script and customize it for your use.

$users = “Type here To address “,” for multiple user TO address” # List of users to email your report to (separate by comma)
$fromemail = “Diskspacealert@Domain.com
$server = “smtp.Domain.com” #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 C:\diskspacereport\list.txt #grab the names of the servers/computers to check from the list.txt file. Here you to mention path of list.txt file.
# 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, Century Gothic”” size=””10″”>
<font face=”Century Gothic” size=”5″ color=”Blue”><u><b> Disk Space Storage Report </b></u> </font>
<br>
<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.We are request you to remove unwanted files from respective server drives.
<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
</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
}

Step 4 : Create list.txt file of servername.

disk-space-1

Step 5 : Create start.bat file.

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

Step 6 : Now create daily Task Scheduler for alert notification of free disk space report.

Disk Space 2.JPG

 

New Triggers 

disk-space-3

New Action  

disk-space-4

Condition and Setting tab are same.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: