Skip to content

SharePoint Farm Backup Checks Revisited with XML File Node Traversing

by on April 15, 2015

I have this running on sql server and throwing errors if it does not run as well as error checking. I have it running separately from the backup job to make sure it really works.

Not 100% sure why the sql service account could not run the powershell command send-mailmessage though.

I chose to include the whole content of the xml file, but if that is too messy, you have a few options. Note that it includes nodes like the backup method and start and endtime, as well as error counts.

If you only want to include certain parts of the backup XML file

You can pull it out like this:

[xml]$xml = get-content \\server\filesharepath\farm*\*.xml ;

$spbackupmethod = $xml.spbackuprestorehistory.sphistoryobject.spbackupmethod

$spbackupmethod

image

Or iterate through the xml nodes:

$xml.spbackuprestorehistory.sphistoryobject.spbackupmethod.childnodes | %{ $_.localname $_.innerxml}

Or if you just want that set of xml try:

%{$_.outerxml}

The Script

$ErrorActionPreference = “Stop”
$emailserver = “sqlserver.mydomain.com”
#change to this for send-mailmessage
#$emailserver = “mail.mydomain.com”
$emailfrom = “sqlserver@mydomain.com”
$erroremailfrom = “alerts@mydomain.com”
$emailto = “ebruce@mydomain.com”
$checkpath = “\\backupserver\backupshare\farm*\*.xml”

if (get-childitem $checkpath)
{
###################success
$emailbody =  (get-content $checkpath | out-string)
$emailsubject = “Backup Check Sql – SharePoint Backup SQL Job”
#does not work in sql server 2008 job – works in task scheduler or manual
#send-mailmessage -to $emailto -from $emailfrom -subject $emailsubject -body $emailbody -smtpserver $emailserver

#works in sql server 2008 job – send-mailmessage does not
$smtp = New-Object Net.Mail.SmtpClient($emailserver)
$objMailMessage = New-Object System.Net.Mail.MailMessage
$objMailMessage.From = $emailfrom
$objMailMessage.To.Add($emailto)
$objMailMessage.Subject = $emailsubject
$objMailMessage.Body = $emailbody
$smtp.send($objMailMessage)

}else{
##################failure
$emailsubject = “SharePoint Farm Backup Failed”
$emailbody = “SP Farm Backup Failed – Failure, the backup location is empty, could not find $checkpath. Please check SP Farm backup job in Central Admin and space on server.”
#does not work in sql server 2008 job – works in task scheduler or manual
#send-mailmessage -to $emailto -from $emailfrom -subject $emailsubject -body $emailbody -smtpserver $emailserver
#works in sql server 2008 – send-mailmessage does not
$smtp = New-Object Net.Mail.SmtpClient($emailserver)
$objMailMessage = New-Object System.Net.Mail.MailMessage
$objMailMessage.From = $erroremailfrom
$objMailMessage.To.Add($emailto)
$objMailMessage.Subject = $emailsubject
$objMailMessage.Body = $emailbody
$smtp.send($objMailMessage)

throw “SPFarm Backup Failure, the backup location is empty. Please check SP Farm backup job in Central Admin and space on server.”
}
if (get-childitem $checkpath)
{
###################success
$emailbody =  (get-content $checkpath | out-string)
$emailsubject = “Backup Check Sql6 – SharePoint 2007 – SQL Job”
#send-mailmessage -to $emailto -from $emailfrom -subject $emailsubject -body $emailbody -smtpserver $emailserver

#works in sql server 2008 – send-mailmessage does not
$smtp = New-Object Net.Mail.SmtpClient($emailserver)
$objMailMessage = New-Object System.Net.Mail.MailMessage
$objMailMessage.From = $emailfrom
$objMailMessage.To.Add($emailto)
$objMailMessage.Subject = $emailsubject
$objMailMessage.Body = $emailbody
$smtp.send($objMailMessage)

}else{
##################failure
$emailsubject = “SharePoint Farm Backup Failed”
$emailbody = “SP Farm Backup Failed – Failure, the backup location is empty, could not find $checkpath. Please check SP Farm backup job in Central Admin and space on server.”
#does not work in sql
#send-mailmessage -to $emailto -from $emailfrom -subject $emailsubject -body $emailbody -smtpserver $emailserver
#works in sql server 2008 – send-mailmessage does not
$smtp = New-Object Net.Mail.SmtpClient($emailserver)
$objMailMessage = New-Object System.Net.Mail.MailMessage
$objMailMessage.From = “sql6@mydomain.com”
$objMailMessage.To.Add(“whatsup@mydomain.com”)
$objMailMessage.Subject = “sql6 SP Farm Backup Failed”
$objMailMessage.Body = “SP Farm Backup Failed – Failure, the backup location H:\spbackup\farm\ is empty. Please check SP Farm backup job in Central Admin and space on server.”
$smtp.send($objMailMessage)

throw “SPFarm Backup Failure, the backup location is empty. Please check SP Farm backup job in Central Admin and space on server.”
}

Cheers!

Advertisements
Leave a Comment

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

%d bloggers like this: