1

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.

2
  • What output are you expecting? Commented Aug 3, 2017 at 8:18
  • @Jelphy: I added the expected output to the post - see above. Commented Aug 3, 2017 at 8:25

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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 stackoverflow.com/questions/8761888/…. Is that what you meant?
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
OK. I tried running the script proposed by Rainer in this post: stackoverflow.com/questions/8761888/…. 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 If you go on the machine itself does Rainer's code get you what you need in $p.StandardOutput.ReadToEnd()?
@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).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.