Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm trying to setup a script in Jenkins to backup my SharePoint server. The problem I'm encountering is that it Backup-SPFarm is not correctly returning an error. So my $ErrorActionPreference is being ignored.

My script looks like:

Add-PsSnapin Microsoft.Sharepoint.Powershell
$ErrorActionPreference = "Stop"
# $backUpDir=New-Item -Type Directory -Path d:\backups\SharePoint\$Env:BUILD_ID
# Backup-SPFarm -BackupMethod Full -Directory $backUpDir -Verbose
# Get-Content foo
Backup-SPFarm -BackupMethod Full -Directory d:\backups\bogus -Verbose

The result I'm getting looks like this:

Building in workspace D:\Alfred\jobs\Maintenance-Backup-SharePoint\workspace
[workspace] $ powershell.exe "& 'D:\Tomcat 7.0\temp\hudson3082553469596846563.ps1'"
VERBOSE: Leaving BeginProcessing Method of Backup-SPFarm.
VERBOSE: Performing operation "Backup-SPFarm" on Target "VA01DJNKCIS001".
Backup-SPFarm : Directory d:\backups\unknown does not exist or the SQL Server s
ervice account and the NT SERVICE\Tomcat7 service account do not have permissio
n to read or write to the backup folder. Specify a different directory or ensur
e that the SharePoint Timer service and Microsoft SQL Server service accounts h
ave Full Control permission on both the file share and the underlying folder.
At D:\Tomcat 7.0\temp\hudson3082553469596846563.ps1:5 char:14
+ Backup-SPFarm <<<<  -BackupMethod Full -Directory d:\backups\unknown -Verbose
    + CategoryInfo          : InvalidData: (Microsoft.Share...mdletBackupFarm: 
   SPCmdletBackupFarm) [Backup-SPFarm], DirectoryNotFoundException
    + FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletBackupFa 
   rm

VERBOSE: Leaving ProcessRecord Method of Backup-SPFarm.
VERBOSE: Leaving EndProcessing Method of Backup-SPFarm.
Finished: SUCCESS

When I was expecting to see something like this:

Building in workspace D:\Alfred\jobs\Maintenance-Backup-SharePoint\workspace
[workspace] $ powershell.exe "& 'D:\Tomcat 7.0\temp\hudson9060264468839525852.ps1'"
Get-Content : Cannot find path 'D:\Alfred\jobs\Maintenance-Backup-SharePoint\wo
rkspace\foo' because it does not exist.
At D:\Tomcat 7.0\temp\hudson9060264468839525852.ps1:3 char:12
+ Get-Content <<<<  foo
    + CategoryInfo          : ObjectNotFound: (D:\Alfred\jobs\...t\workspace\f 
   oo:String) [Get-Content], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetCo 
   ntentCommand

Build step 'Windows PowerShell' marked build as failure
Finished: FAILURE

when given:

Add-PsSnapin Microsoft.Sharepoint.Powershell
$ErrorActionPreference = "Stop"
# $backUpDir=New-Item -Type Directory -Path d:\backups\SharePoint\$Env:BUILD_ID
# Backup-SPFarm -BackupMethod Full -Directory $backUpDir -Verbose
Get-Content foo
# Backup-SPFarm -BackupMethod Full -Directory d:\backups\bogus -Verbose

Is there a return value from Backup-SPFarm that I can check? How do I check it?

Update: $LastExitCode seems to be blank when I add:

Write-Host $LastExitCode
share|improve this question
add comment

1 Answer

Have you tried removing the -Verbose parameter? I have found that -Verbose seems to override $ErrorActionPreference for some reason (possibly a bug). This is not just specific to Sharepoint commands.

You can also combine -Verbose -ErrorAction "Stop".

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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