因此,我有一个Powershell脚本可以将NServiceBus服务安装为Windows服务。
Invoke-Expression "$fullNsbHostPath $arguments"
为了完整起见,我尝试了
Invoke-Expression
和Start-Process
:Start-Process -Wait -NoNewWindow -FilePath $fullNsbHostPath -ArgumentList $arguments -RedirectStandardOutput $tempFileName -ErrorVariable $errvar
它可以正常启动安装程序,在某些情况下会报告异常,例如:
Failed to execute installers: System.Data.SqlClient.SqlException
(0x80131904): A connection was successfully established with the
server, but then an error occurred during the login process.
(provider: Shared Memory Provider, error: 0 - No process is on the
other end of the pipe.) ---> System.ComponentModel.Win32Exception
(0x80004005): No process is on the other end of the pipe ... Error
....
Number:233,State:0,Class:20
我对此很满意。我希望异常(exception)。但是,该过程本身并不表示已失败。实际上,Powershell脚本本身已成功完成。
我可以解析输出文本以获取错误代码或某些“异常(exception)”文本,但这似乎令人遗憾地不能令人满意。
我认为这不是Powershell的问题。当我仅通过命令行运行它并检查
%errorlevel%
时,我得到0
。此外,执行此操作的其他几个示例在线脚本也忽略了任何错误传播。有什么建议?
最佳答案
要详细说明我对OP的评论,当您使用NServiceBus.Host.exe安装Windows服务时,它最终会从WindowsInstaller.RunInstall
命名空间调用NServiceBus.Hosting.Windows.Installers
来执行安装:
private static void RunInstall()
{
Console.WriteLine("Executing the NServiceBus installers");
try
{
WindowsInstaller.host.Install();
}
catch (Exception ex)
{
Console.WriteLine("Failed to execute installers: " + ex);
}
}
如您所见,此方法捕获安装过程中的所有异常,并将它们写入控制台标准输出流。它不会写入错误输出流(
Console.Error
)或设置退出代码(Environment.ExitCode
),因此您唯一的选择是解析应用程序的标准输出。
关于powershell - 在Powershell中安装NServicebus服务时捕获错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40674465/