1

NServicebus服务时,所以,我有一个PowerShell脚本安装一个NServiceBus服务作为Windows服务。捕获错误安装在PowerShell中

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 

我对此感到满意。我想预计的例外。然而,这个过程本身并没有表明它失败了。的确,Powershell脚本本身成功完成。

我可以解析为一个错误代码或者一些“例外”文本输出文本,但这似乎可悲的不尽人意。

我不认为这是一个PowerShell的问题。当我通过命令行运行它并检查%errorlevel%时,我得到0。此外,其他几个执行相同操作的示例联机脚本也会忽略任何错误传播。

有什么建议吗?

+0

NServiceBus.Host.exe包含捕获所有的异常,然后异常详细信息写入控制台(标准输出,而不是标准错误),不设置退出代码,这么短解析所有输出的代码,我想你”重新卡住了。 –

回答

1

要阐述我到OP评论,当您使用NServiceBus.Host.exe安装一个Windows服务,它最终从NServiceBus.Hosting.Windows.Installers命名空间调用WindowsInstaller.RunInstall来执行安装:

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),所以您唯一的选择是解析应用程序的标准输出。

+0

这是垃圾!也许提出一个错误!哪个版本的NServicebus?我有兴趣看看他们是否已经在5或更高版本中解决了这个问题。 –

+1

这至少适用于NServiceBus 3.3 - 5.0。 –

+2

我怀疑这是一个错误。我在这里提出了一个问题https://github.com/Particular/NServiceBus.Host/issues/112,对于任何牦牛刮这造成了你的歉意cc @JamesWiseman – Simon

1

的要求我会运行在错误日志

快速和肮脏......编辑检查。

$logfile = 'logfilelocation' 
$Complete = 'no' 
$Counter = 1 
$max = 6 

    DO { 

    $Check = SELECT-STRING -pattern 'status: 0.' -path $logfile -quiet 
    write-host $Check 
    If (($Check) -eq $True) { 
    Set-Variable -name Complete -Value "yes" 
     } 
      Else {Set-Variable -name Complete -Value 'no'} 
       Write-host $Counter 
       Start-Sleep 20 
       $Counter++ 
          } 

      while ($Complete -eq 'no') 

       If (($Counter) -eq $max) { 

    Throw 'Installation failed, check the error log' 

     } 
+0

谢谢。我有类似的解决方案,但希望避免这种情况。从Andy的回答看来,我所追求的是不可能的。 –

+0

我不知道涉及的产品。但是你可以尝试用退出代码封装启动进程。 如 (开始处理-FilePath “$ MSI” -ArgumentList “/ S/V/QN” -Wait -passThru).ExitCode 编辑 - 刚刚看了起来,是啊看起来是无用也.. ..! –