0

I have the following (part of a) script, which is executed from TeamCity.

try
{
    $result = Invoke-Command -Session $session –ScriptBlock {
        Param
        (
            [String]                            
            $serviceName
        )                       
        Start-Process "C:\<some_path>\$serviceName\NServiceBus.Host.exe" "/install /serviceName:$serviceName /displayName:$serviceName" -NoNewWindow -Wait
        } -ArgumentList $service                
    }
    catch
    {
        $errorMessage = $_.Exception.Message                    
        Write-Error "ERROR: NServiceBus.Host service installation failed with exception '$errorMessage'"
    }

The problem is that no output is being written to the console, and thus, TeamCity will pass the build step regardless of whether or not the script block of the Invoke-Command cmdlet succeded.

When I run the Start-Process cmdlet locally on one of the target servers, I get the following output (which is what I would expect be returned when invoking the cmdlet using Invoke-Command):

Running a transacted installation.

Beginning the Install phase of the installation. Installing service ... Service has been successfully installed. Creating EventLog source in log Application...

The Install phase completed successfully, and the Commit phase is beginning.

The Commit phase completed successfully.

The transacted install has completed.

One workaround is to use the -Redirectxx <some_log_file> switch on Start-Process, read the log file and act upon whatever its contents may be. However, I would really like to avoid this if it is possible.

I am using Powershell version 5.

nils1k
  • 467
  • 5
  • 20

1 Answers1

1

You can use Start-Process -PassThru parameter

https://learn.microsoft.com/fr-fr/powershell/module/Microsoft.PowerShell.Management/Start-Process?view=powershell-5.1

-PassThru : Returns a process object for each process that the cmdlet started. By default, Start-Process does not generate any output.

Manu
  • 1,685
  • 11
  • 27
  • The `PassThru` parameter only gets me a process object, it does not get me any output - as you state above. Of course, I could use the process object to get the output using e.g. the approach mentioned in https://stackoverflow.com/questions/8761888/capturing-standard-out-and-error-with-start-process. Is that what you meant? – nils1k Aug 03 '17 at 09:17
  • Yes I did not detail how to get output from the cmdlet but only the prerequisite to retrieve "any" informations from `Start-Process` because as indicated in M$ docs, `Start-Process` does not generate output without `Passthru` – Manu Aug 03 '17 at 09:21
  • OK. I tried running the script proposed by Rainer in this post: https://stackoverflow.com/questions/8761888/capturing-standard-out-and-error-with-start-process. However, I am still unable to get any output. Only difference between that solution and my code, is that I am running it inside of an `Invoke-Command` cmdlet. Thus, I guess the problem here is that whatever output produced by `NServiceBus.Host.exe` need to be passed to the `Start-Process` cmdlet (seemingly not happening), and in turn `Invoke-Command` should take care of "piping" any output from the former to the console. – nils1k Aug 03 '17 at 09:30
  • @nils1k If you go on the machine itself does Rainer's code get you what you need in `$p.StandardOutput.ReadToEnd()`? – Sambardo Aug 04 '17 at 03:31
  • @Sambardo: Yes, running Rainer's code locally on the server works, i.e. all output is printed to the console. If I try to wrap the code inside a scriptblock using the `Invoke-Command` cmdlet, however, I get no output at all when running it locally on the server (`ComputerName` parameter set to localhost). – nils1k Aug 04 '17 at 06:09
  • @nils1k, that's odd. I put that code into a scriptblock and tried it on both the localhost and a connected server and it worked fine. Have you tried his code as written (with ping) or only with your own exe? – Sambardo Aug 04 '17 at 14:07
  • @Sambardo: Sorry, I should have been more precise in my comment. My attempts were all using `NServiceBus.Host.exe`. I also tried using just the `ping.exe` command, and that works as it should, i.e. according to what you describe. – nils1k Aug 04 '17 at 15:17
  • Sounds like its an issue with the difference between how `NServiceBus.Host.exe` and `ping.exe` do their output. – Sambardo Aug 04 '17 at 16:19