如何在从 SQL Server 2008 R2 作业调用的 PowerShell 步骤中显示错误? [英] How to show error in PowerShell step called from SQL Server 2008 R2 job?

查看:34
本文介绍了如何在从 SQL Server 2008 R2 作业调用的 PowerShell 步骤中显示错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在网上四处寻找,但没有找到太多.事实上,stackoverflow 上的信息最多 - 显示通过 CMD 提示符执行 Powershell 脚本的 SQL 2005 作业失败,如果 PS 脚本失败 但这是 SQL Server 2005,我希望事情在 2008 年有所改善.>

无论如何,如果 Powershell 脚本失败,我希望能够让 SQL Server 代理作业失败.在 SQL Server 中,我使用的是 powershell 步骤,仅此而已:

write-output "这是一个测试错误"退出 -1

我认为这会导致 SQL Server 作业出错,但它没有发生,它显示成功.是否有必要使用 cmdexec 步骤,然后 shell 到 powershell,以便在发生错误时能够得到错误?

谢谢,西尔维娅

解决方案

如果我希望 Powershell 作业步骤返回错误,我将一行添加到 Write-Error 并将 ErrorAction 设置为停止.这只是一行.

写错误作业失败"-EA 停止

I've been looking around online, but haven't found very much. As a matter of fact, the most info has been here on stackoverflow - Show failure for SQL 2005 Job that executes Powershell script through CMD prompt, IF PS script fails but that's with SQL Server 2005, I'm hoping things have improved in 2008.

Anyway, I want to be able to have the SQL Server Agent job FAIL if the the Powershell script fails. In SQL Server I'm using a powershell step, with just this:

write-output "this is a test error"
exit -1

I thought this would cause the SQL Server job to error, but it's not happening, it shows successful. Is it necessary to use a cmdexec step, and then shell out to powershell, in order to be able to get errors when they occur?

thanks, Sylvia

解决方案

I add a line to Write-Error with ErrorAction set to stop if I want a Powershell job step to return an error. It's just the one line.

Write-Error "Job Failure" -EA Stop

这篇关于如何在从 SQL Server 2008 R2 作业调用的 PowerShell 步骤中显示错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆