Disk space is one of those things that frequently runs out of space no matter how much you bump it up irrespective of the service you are running in the server. I know storage is cheap but who wouldn’t want to keep an eye on what’s cooking especially when it has the potential to bring things to halt?
I believe that if you run a Windows Service, or if anything Windows is your job, then implementing PowerShell will make your job a lot easier and lot more fun. This PowerShell script calculates free disk spaces in multiple servers (from a text file) and emails the report in a HTML format. The script can be scheduled using Windows Scheduler or SQL Agent Job to run at a certain time or interval. This is designed to report only servers with less than 20% free space but you can customize for your needs
Prerequisite:
If you have never used PowerShell on your system before, chances are that your PowerShell “Execution Policy” is set to restrict execution of scripts on your machine, and you’ll have trouble running this script. To allow your scripts to execute, you need to set your Execution Policy to RemoteSigned. Here is the procedure to, first of all, check what yours is set to, and then, if necessary, set it to RemoteSigned.
- Run PowerShell as Administrator on your PC/Server
- Enter in and run the Get-ExecutionPolicy cmdlet – this will output the current setting. If it is not alreadyRemoteSigned, or Unrestricted, then use the following cmdlet to set it to allow your scripts to run:Set-ExecutionPolicy RemoteSigned
- You should now be asked to confirm whether you are sure. Cick Yes to confirm as shown below
Now that your environment is ready to run the cmdlets and scripts, lets take a look at the basic rundown of the script’s processes:
- Iterate through a list of servers you specify in a text file, checking disk space.
- Check each free disk space percentage figure against a pre-defined percent threshold figure.
- If the disk in question is below this threshold, then add the details to the report, if not, skip past it.
- Assemble an e-mail and send it off to the specified recipient(s) if any of the drives were below the free disk space threshold.
The Script
######################################################### # # SQLSERVERZEST: Server Disk Space monitoring Report # ######################################################### #### Provide Below email and SMTP details #### $fromemail ="abc@email.com" $users="recipients@email.com" $Server= "smptserver.DomainName.Com" $computers = get-content -Path "//ServerName/../Servers.txt" # Specify servers' list path # Set free disk space threshold below in percent (default at 20%) [decimal]$thresholdspace = 20 #### Main Sctipt Block #### $tableFragment= Get-CimInstance -ComputerName $computers cim_LogicalDisk -erroraction 'silentlycontinue' ` | select SystemName, 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 #### HTML for our body of the email report #### $HTMLmessage = @" <font color=""Red"" face=""Segoe UI Light, Segoe UI Light"" size=""8""> <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> <style type=""text/css"">body{font: .8em ""Segoe UI Light"", Segoe UI Light, Segoe UI Light, Segoe UI Light, Segoe UI Light;} 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 } # End of Script
Here is the sample email report
This is just a quick report that I developed but as with any scripting language, PowerShell will give you plenty of customization to modify the look and feel of your report as desired.
[…] Link3 […]
Nice Script..How to Get Disk Utilization Report for all the drives. rather then less than 20%
$computers = get-content -Path “//ServerName/../Servers.txt” # Specify servers’ list path
=====> what’s about i can change?
//mail/servers.txt
//192.168.x.x/servers.txt
Can not effect.