Wahid's Blog

a technology blog.

Sep-19-09

Free SharePoint Backups Email Log

Posted by Wahid Saleemi

In a very recent post, I showed you a script written in Visual BASIC (vbs) to have your log file emailed to you on a schedule.

This script didn’t work for me on my Windows Server 2008 machines. I got the error “Reportlog.vbs:(52,2). CDO.Message.1: The transport failed to connect to the server.”

I later confirmed this was due to me specifying the wrong server name. But, I went on a small tangent and decided to try PowerShell. The PowerShell version of this script seems to run faster and is easier to understand.

Step 1
Install PowerShell. You can do this on Windows Server 2008 by using the following command: ServerManagerCmd -install PowerShell

Step 2
You’ll need to enable execution of .ps1 files (powershell scripts) first by typing the following into the PowerShell prompt: Set-ExecutionPolicy RemoteSigned

Step 3
Save this script and run it. You can type Drive:\Path\scriptname.ps1 in PowerShell but I like to save this in a batch file and run it using: powershell.exe -noexit &’D:\BackupBatch\reportlog.ps1′

# ##############################################################
# Send Email with Attachment Script
# Usage: $smtp to see settings, or $smtp | gm to see options
# Written by Wahid Saleemi (http://wahidsaleemi.com)
# Enable execution by: Set-ExecutionPolicy RemoteSigned
# ##############################################################
$file = "D:\Backups\Farm\backupdaily.log"
$smtpServer = "mailserver1"

$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($file)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = "sharepoint@domain.com"
$msg.To.Add("wahid.saleemi@domain.com")
$msg.Subject = "Your SharePoint Backup Report"
$msg.Body = "See attachment"

$msg.Attachments.Add($att)
$smtp.Send($msg)

$att.Dispose()

# ##############################################################
# Here's some extra things we could use
# ##############################################################
# $Credentials = new-object System.Net.networkCredential
# $Credentials.domain = "NETBIOS"
# $Credentials.UserName = "xxxx"
# $Credentials.Password = "xxxx"
# $smtp.Credentials = $Credentials
#
# $msg.Sender = "portal.sharepoint@domain.com"
# $msg.IsBodyHtml = $true

Hope you find this useful. Check out Michael Blumenthal’s blog for more useful PowerShell scripts for SharePoint.

SPC09 3 3 Free SharePoint Backups Email Log
SEO Powered by Platinum SEO from Techblissonline

Wahid's Blog is Digg proof thanks to caching by WP Super Cache