1

So, I have a powershell script to install an NServiceBus service as a Windows Service.

Invoke-Expression "$fullNsbHostPath $arguments" 

For completeness, I've tried both Invoke-Expression and Start-Process:

Start-Process -Wait -NoNewWindow -FilePath $fullNsbHostPath -ArgumentList $arguments -RedirectStandardOutput $tempFileName -ErrorVariable $errvar

It fires up the installer just fine, which under certain circumstances reports an exception For example:

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

I'm happy with this. I expect the exception. However, the process itself gives no indication that it's failed. Indeed, the Powershell script itself completes successfully.

I could parse the output text for an error code or maybe some 'exception' text, but this seems lamentably unsatisfactory.

I don't think this is a Powershell issue. When I run it simply through the command line and examine %errorlevel% I get 0. Moreover, several other example online scripts that do the same also omit any error propagation.

Any suggestions?

James Wiseman
  • 29,946
  • 17
  • 95
  • 158
  • NServiceBus.Host.exe contains code that catches all exceptions then writes the exception detail to the console (stdout rather than stderr) and doesn't set an exit code, so short of parsing all of the output, I think you're stuck. – Andy Lamb Nov 18 '16 at 10:43

2 Answers2

1

I'd run a check on the error log

Quick and dirty... Edit as required.

$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'

        } 
Richard Dakin
  • 413
  • 4
  • 16
  • Thanks. I have a solution analogous to this, however was hoping to avoid this. It looks like from Andy's answer that what I'm after isn't possible. – James Wiseman Nov 18 '16 at 11:05
  • I don't know the product involved. But you could try wrapping the start-process with a exit code out. Such as (Start-Process -FilePath "$MSI" -ArgumentList "/S /v/qn" -Wait -Passthru).ExitCode EDIT - Just read up, yeah looks like that is useless also....! – Richard Dakin Nov 18 '16 at 11:21
1

To elaborate on my comment to the OP, when you use NServiceBus.Host.exe to install a Windows service, it eventually calls WindowsInstaller.RunInstall from the NServiceBus.Hosting.Windows.Installers namespace to perform the installation:

private static void RunInstall()
{
    Console.WriteLine("Executing the NServiceBus installers");
    try
    {
        WindowsInstaller.host.Install();
    }
    catch (Exception ex)
    {
        Console.WriteLine("Failed to execute installers: " + ex);
    }
}

As you can see, this method catches all exceptions from the installation process and writes them to the console standard output stream. It doesn't write to the error output stream (Console.Error) or set the exit code (Environment.ExitCode), so your only option is to parse the standard output from the application.

Andy Lamb
  • 2,151
  • 20
  • 22
  • That's rubbish! Maybe raise a bug! Which version of NServicebus? I would be interested to see if they've addressed this in 5 or later. – James Wiseman Nov 18 '16 at 11:04
  • 1
    This applies to NServiceBus 3.3 - 5.0 at least. – Andy Lamb Nov 18 '16 at 12:07
  • 2
    I suspect this is a bug. i raised an issue here https://github.com/Particular/NServiceBus.Host/issues/112 and sorry for any yak shaving this has caused you cc @JamesWiseman – Simon Nov 19 '16 at 00:33