0

im trying to set up a Powershell script, which backs up a specific SiteCollection and sends an email afterwards, stating if the backup was successfully or not. I'm stuck at the point where I need to check if there was an exception or not. This is my code so far:

Add-PsSnapin Microsoft.SharePoint.Powershell –ErrorAction SilentlyContinue
try
{
$name = "xxxx"
$today = (Get-Date -Format dd-MM-yyyy)
[IO.Directory]::CreateDirectory("E:\Backup\SiteCollections\$name\$today")

Backup-SPsite -Identity http://xxxxxxx.xxxxx.de/sites/xxxxxx -Path     E:\Backup\SiteCollections\$name\$today\$name.bak –Force
}
catch
{
 #check for errors
}
#send mail success if no error
#send mail error if exception count is >0

At the end of the day my script should have checked whether an exception has occured or not and based on this alter the mail subject / body.

Any ideas on that?

1

Unless you change the ErrorVariable of PowerShell cmdlet they'll store error information in $Error

So you can just check $Error.Count to see if there is any errors

|improve this answer|||||
  • Sometimes I am just plain stupid. Thank you Per :> Works like a charm. – Marco Oct 30 '12 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.