PowerShell script to monitor server disk space and send out email alerts

When you’re managing a large SharePoint installation, storage issues are going to be a major concern.   Whether it’s ULS logs, IIS logs,  SQL databases and transaction logs – whatever it is, when left to its own devices, SharePoint can consume a lot of space very quickly, and before you know it, your disks will be running out of space.

I found a cool PowerShell script by Colin Smith called Disk Space Monitor (see it here:  http://powershell.com/cs/media/p/1617.aspx).  For whatever reasons, I had problems getting the email component to work properly, and I also wanted to tweak the email message format.   One thing led to another, and I ended up re-writing a few other sections of Colin’s script to better suit my needs.  The final product appears below  – it reads a list of servers, checks the free space on each server, and sends out an email summary to a list of users you specify. 

How to use this script:

Step 1.  Create a simple text file with a list of your servers to audit, single column, single-spaced, like this:

server1

server2

server3

server4

Let’s call the list “list.txt”.  Save the file.

Step 2. Create a batch file (let’s call it “start.bat”) and enter the following code in it.  Make sure to enter your own path for the server list as well as the output log file.


REM  Usage:  powershell %~dp0DiskSpaceMonitor.ps1  <computer list file path>   <output log path>
powershell  %~dp0DiskSpaceMonitor.ps1 C:\Scripts\DiskSpaceMonitor\list.txt   C:\Scripts\DiskSpaceMonitor\output.txt

 

Step 3. Create a PowerShell script file in the same directory as “start.bat”,  let’s call it “DiskSpaceMonitor.ps1”, and enter the following code in it.  Make sure to specify your own list of users to email and your SMTP server name or IP address.  Save the file, and execute “start.bat”.

# This script performs the following actions:

#  1) Read a list of servers
#
#  2) For each server on the list, get disk drive information - drive letter, drive size, free space, percent free
#
#  3) Email the report to users specified by the $users variable
#
$users = "user1 @ domain.com", "user2 @ domain.com " , "user3 @ domain.com"

$server = "SMTP server name or IP address"

$port = 25

$list = $args[0]

$output = $args[1]

$computers = get-content $list

echo "SharePoint Storage Report" > $output
echo " " >> $output
echo "Note: Free space below 30% is labeled with *** " >> $output
echo " " >> $output
echo " " >> $output
echo "ServerName    Drive Letter Drive Size Free Space Percent Free" >> $output
echo "----------    ------------ ---------- ---------- ------------" >> $output
foreach ($line in $computers)
{
 $computer = $line 
 
 $drives = Get-WmiObject -ComputerName $computer Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3}
 foreach($drive in $drives)
 {

 $id = $drive.DeviceID
 $size = [math]::round($drive.Size / 1073741824, 2)
 $free = [math]::round($drive.FreeSpace  / 1073741824, 2)
 $pct = [math]::round($free / $size, 2) * 100
 
 if ($pct -lt 30) { $pct = $pct.ToString() + "% *** " }

 else {  $pct = $pct.ToString() + " %" }

echo "$computer   $id  $size  $free  $pct"  >> $output

$pct = 0 

 }

}
foreach ($user in $users)
{

$to      = $user

$from    = "<a href="mailto:diskspacemonitor@domain.com">diskspacemonitor@domain.com</a>"

$subject = "Connect Storage Report"

foreach ($line in Get-Content $output)

{

$body += “$line `n”

}

# Create mail message

$message = New-Object system.net.mail.MailMessage $from, $to, $subject, $body

#Create SMTP client

$client = New-Object system.Net.Mail.SmtpClient $server, $port

# Credentials are necessary if the server requires the client # to authenticate before it will send e-mail on the client's behalf.

$client.Credentials = [system.Net.CredentialCache]::DefaultNetworkCredentials

# Try to send the message

try {      

$client.Send($message)      

"Message sent successfully"

# reset variables

$body = ""

}

# Catch an error

catch {

"Exception caught in CreateTestMessage1(): "

}

}

# End of Script

 
Advertisements

11 Responses to “PowerShell script to monitor server disk space and send out email alerts”


  1. 1 Toan Pham January 2, 2011 at 6:36 pm

    Thanks for the script. I had issue getting the part sending out email to work. I can see the output file with disk space, but it did not send email out…

    • 2 andrei338 January 2, 2011 at 7:54 pm

      I’d first check to make sure that you can successfully send email from your server. Try sending a test email message from a Telnet window, such as described here: http://www.yuki-onna.co.uk/email/smtp.html. Also, if the Powershell script generated an exception during the email send, it should get displayed in the command window – it might give you some clues as to what’s going on.

      • 3 Shah February 11, 2011 at 1:33 pm

        Hi,

        I’m following everything per the instructions, however, I’m not getting anywhere. It doesn’t even get me the output file nor do I get any error/exception etc. I’m trying to make it work for Windows 03 servers with no success. Any idea what I might be doing wrong?
        1. Got the list file
        2. Got the bat file with powershell parameters as mention
        3. Powershell code file as step 3.

        Thanks

  2. 4 andrei338 February 13, 2011 at 9:12 pm

    Make sure that you’re running Powershell V2 – I haven’t tested this script under Powershell v1.0.

  3. 5 Shah February 14, 2011 at 7:25 am

    Thanks mate. That was it :-)..Cheers

  4. 6 Kirill August 25, 2011 at 8:30 am

    Thank you a lot guy! Good scripts, works all right. )

  5. 7 Martin September 17, 2012 at 9:49 am

    doesn’t work

    Unexpected token ‘mailto:it@ebpgroup.com”>mailto:it@sadf.com“‘ in expre
    ssion or statement.
    At C:\Users\a\DSM\DiskSpaceMonitor.ps1:57 char:73
    + $from = “mailto:it@sadf.com” <<<<

    + CategoryInfo : ParserError: (mailto:it@sadf.com“:
    String) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken

  6. 8 Alexy Thomas July 8, 2014 at 11:45 am

    Exception calling “.ctor” with “4” argument(s): “The specified string is not in the form required for an e
    -mail address.”

    I get this error on the parameters passed for $users. Could you please help me with this?
    I have it set as :
    $user=”someone@gmail.com”

    • 9 andrei338 July 9, 2014 at 8:58 am

      Check the quotation marks – sometimes they get garbled during copy & paste. I would try deleting the quotation marks and re-typing them manually.

  7. 10 vivek mehar December 29, 2016 at 3:46 am

    i am getting the below error

    Unexpected token ‘mailto:it@ebpgroup.com”>mailto:it@sadf.com“‘ in expre
    ssion or statement.
    At C:\Users\a\DSM\DiskSpaceMonitor.ps1:57 char:73

    Please help me

    • 11 andrei338 December 29, 2016 at 10:59 pm

      I would check your script to see if something went wrong during the copy & paste. Sometimes certain characters don’t copy over correctly. Quotation marks “” especially are often a problem. Try erasing quotation marks in that particular line of script and re-typing them manually.


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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s




RSS Information Week Headlines

  • An error has occurred; the feed is probably down. Try again later.

RSS SharePoint Team Blog

  • An error has occurred; the feed is probably down. Try again later.

RSS InfoPath Team Blog

  • An error has occurred; the feed is probably down. Try again later.

RSS Joel Oleson Blog

  • An error has occurred; the feed is probably down. Try again later.

RSS Susan Hanley’s KM Blog

  • An error has occurred; the feed is probably down. Try again later.

Blog Stats

  • 356,734 hits

%d bloggers like this: