powershell - 在Powershell中安装NServicebus服务时捕获错误

标签 powershell error-handling windows-services nservicebus nservicebus4

因此,我有一个Powershell脚本可以将NServiceBus服务安装为Windows服务。

Invoke-Expression "$fullNsbHostPath $arguments" 

为了完整起见,我尝试了Invoke-ExpressionStart-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/

相关文章:

windows - 如何在 Windows 上使用 PowerShell 脚本连接和断开蓝牙设备?

vba - 从 Powershell 中调用 Microsoft Word VBA 代码

javascript - Web API : error integer codes or error strings?中返回错误

C# 使用当前用户权限处理安装而不提示

c# - 如何在用 C# 编写的 Windows 服务中获取 ServiceName?

c# - 每天在特定时间C#(Windows服务)执行方法的代码失败

string - 如何将多个字符串参数传递给 PowerShell 脚本?

PowerShell 搜索文本并添加一行

sql - 与联合所有SQL查询获取错误 'Column specified multiple times'

json - 如何在JSON响应中产生错误消息