从SQL Agent Job调用.exe时捕获错误

问题描述 投票:0回答:1

我想从SQL Server代理作业中运行.exe。.exe托管在与SQL Server代理不同的位置。我在MSSMS的Step属性中将作业定义为PowerShell类型,并定义了以下命令:

Invoke-Command -ComputerName "myserver"   -ScriptBlock {
      try
       {
         C:\...\......\myexe.exe  2>&1  -ErrorAction Stop 
        } 
    catch
       {
        Exit 1
        }
    } 

其中myexe.exe是远程服务器驱动器中托管的可执行文件。当我这样做时,出现以下错误:

Message
Unhandled exception. System.Exception: A Forced Exception for testing purpose
At line:3 char:13
+       $res= & C:\........... ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Unhandled excep...testing purpose:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

.....

那是在我的.exe中引发的错误,但实际上我希望作业成功后会出错。

所以问题是,如何处理抛出到myexe.exe文件中的异常并将其捕获到SQL Agent Job中?我希望sql代理作业有误,并且在输出中写入我抛出的异常消息。

非常感谢您的帮助

powershell exception invoke-command sql-agent-job sql-agent
1个回答
0
投票

尝试执行此操作:

$FilePath = "C:\...\.exe"
$ArgumentList = "your args if you need"
$consoleOutput = (cmd /s /c "$FilePath $ArgumentList")

请勿使用“ -ErrorAction Stop”

© www.soinside.com 2019 - 2024. All rights reserved.