Powershell 从调用.exe的SQL代理作业捕获错误

Powershell 从调用.exe的SQL代理作业捕获错误,powershell,exception,invoke-command,sql-agent-job,sql-agent,Powershell,Exception,Invoke Command,Sql Agent Job,Sql Agent,我想从SQL Server代理作业运行.exe。 .exe托管在与我的SQL Server代理不同的位置。 我在MSSMS的步骤属性中将我的作业定义为PowerShell类型,并定义了以下命令: Invoke-Command -ComputerName "myserver" -ScriptBlock { try { C:\...\......\myexe.exe 2>&1 -ErrorAction Stop }

我想从SQL Server代理作业运行.exe。 .exe托管在与我的SQL Server代理不同的位置。 我在MSSMS的步骤属性中将我的作业定义为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代理作业中? 我希望sql代理作业出错,并在输出中写入抛出的exeption消息

非常感谢你的帮助

试着这样做:

$FilePath = "C:\...\.exe"
$ArgumentList = "your args if you need"
$consoleOutput = (cmd /s /c "$FilePath $ArgumentList")
不要使用“-ErrorAction Stop”