I have a sql job step that runs a powershell script using XP_cmdshell as follows
EXEC xp_cmdshell 'powershell.exe %Deployment_root%apps\Systems\sql\DR\ScriptSQLServerObjects.ps1 "gssqlbackup"'
However, if the script fails the job finishes successfully. I want the SQL Job to fail when the powershell script fails and also update the code below so that it outputs the error to a log file
The code in my script "ScriptSQLServerObjects.ps1" is as follows. The first part loads the SQLPS provider (obtained from MSDN) and the second part scripts every Job on the SQL Server for Backup purposes
# Pass the Backup Share Location into the script i.e ScriptsSQLServerObjects "$BackupShare" param ([string]$BackupShare) # Add the SQL Server Provider. $ErrorActionPreference = "Stop" $sqlpsreg="HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.SqlServer.Management.PowerShell.sqlps" if (Get-ChildItem $sqlpsreg -ErrorAction "SilentlyContinue") { throw "SQL Server Provider for Windows PowerShell is not installed." } else { $item = Get-ItemProperty $sqlpsreg $sqlpsPath = [System.IO.Path]::GetDirectoryName($item.Path) } # # Set mandatory variables for the SQL Server provider # Set-Variable -scope Global -name SqlServerMaximumChildItems -Value 0 Set-Variable -scope Global -name SqlServerConnectionTimeout -Value 30 Set-Variable -scope Global -name SqlServerIncludeSystemObjects -Value $false Set-Variable -scope Global -name SqlServerMaximumTabCompletion -Value 1000 # # Load the snapins, type data, format data # Push-Location cd $sqlpsPath Add-PSSnapin SqlServerCmdletSnapin100 Add-PSSnapin SqlServerProviderSnapin100 Update-TypeData -PrependPath SQLProvider.Types.ps1xml update-FormatData -prependpath SQLProvider.Format.ps1xml Pop-Location # Set the Server Name and Default starting location $SName = 'L3PSQL71' Set-Location C: # Load the Dot Net Assembly For SQL Server SMO. Out-Null indicates no output of the GAC and Version [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | Out-Null # Assign the Scripting Options to a variable $so = new-object Microsoft.SqlServer.Management.Smo.ScriptingOptions # Set the Input and Output Location Variables $JobsOutputLocation = '\\' + $BackupShare + '\' + $SName + 'Failover\OtherBits\' + $SName + '_SQLJob - ' $SQLServerJobsPath = 'SQLSERVER:\sql\' + $SName + '\default\JobServer\Jobs' # Script the Jobs $so.IncludeDatabaseContext = 1 gci -path $SQLServerJobsPath | %{$Job = $_.Name; $_.script($so) | out-file -filepath $JobsOutputLocation$Job.sql}
The $ErrorActionPreference is set to stop. So even though I can get it to fail, this failure does not cause the SQL job to fail. How can I trap the failure and also outut the failure details to a log file?
Thanks in advance
- Edited by Schwizla Thursday, September 09, 2010 10:20 AM wrong title