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
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…
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.
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
Make sure that you’re running Powershell V2 – I haven’t tested this script under Powershell v1.0.
Thanks mate. That was it
..Cheers
Thank you a lot guy! Good scripts, works all right. )