Archive for October, 2010

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

 

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

  • 344,936 hits