Share via


Capturing Output from Start-Process to PowerShell Console Host

Question

Thursday, November 12, 2015 9:56 PM

I am using a PowerShell script as part of my build process in TeamCity and I need to figure out how to get Start-Process to output the results back to the Console or Host so that TeamCity can display any error messages in the build log.  I cannot write to a separate output text file because I need this information added to the TeamCity build log rather than a separate file.

I have looked through numerous articles and forum postings but none of them so far have proved fruitful.

This is the code I am using:

Start-Process $SignToolExe  $SignToolArgs -Verb runas -Verbose

I have tried piping the results to Out-String, Out-Host and Out-Null, but none of them seem to push the output of SignTool executed by Start-Process back to the PowerShell console when I run the script.

Please advise.

Thanks.

All replies (8)

Friday, November 13, 2015 2:55 AM ✅Answered

Don't think you can get what you want from the Start-Process cmdlet, but I believe you can do what you want with PowerShell and .Net  Check out this example: (Modified from this post: http://stackoverflow.com/questions/8925323/redirection-of-standard-and-error-output-appending-to-the-same-log-file)

cls
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = "nslookup.exe"
$pinfo.Arguments = "google.com", "8.8.8.8"
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$p.WaitForExit()
$output = $p.StandardOutput.ReadToEnd()
$output += $p.StandardError.ReadToEnd()
$output

Results in the PowerShell Console

Server:  google-public-dns-a.google.com
Address:  8.8.8.8

Name:    google.com
Addresses:  2607:f8b0:4000:80b::200e
      74.125.227.165
      74.125.227.169
      74.125.227.168
      74.125.227.163
      74.125.227.164
      74.125.227.160
      74.125.227.162
      74.125.227.174
      74.125.227.167
      74.125.227.161
      74.125.227.166

Non-authoritative answer:

Friday, November 13, 2015 9:55 AM ✅Answered

Start-Process is not required.  Just call the program. The output will go to the console by default.

Start-Process does make arguments easier to state but may open in a new window so just twll itnot to.

Start-Process$SignToolExe  $SignToolArgs -NoNewWindow

Do not use RunAs.

\(ツ)_/


Thursday, November 12, 2015 10:03 PM

Just run the program at the prompt.  All output will go to the console by default.

\(ツ)_/


Friday, November 13, 2015 12:44 AM

As I stated above, it is being run through my Continuous Integration Build Server (Jetbrains TeamCity), so there is no possibility of "running the program at the prompt".


Friday, November 13, 2015 4:10 AM

Hi, try to use invoke-command instead of start-process.

Like this:

$out = Invoke-Command -ScriptBlock {cmd /c ipconfig /all}

Friday, November 13, 2015 9:31 PM

I am attempting to run the following script using Start-Process to sign my executables using SignTool:

Start-Process -FilePath $SignToolExe  $SignToolArgs -RedirectStandardOutput $OutputFile -RedirectStandardError $ErrorFile -Verbose -Credential $PSCredential

The problem I seem to be having is that I get an Access Denied error message when I attempt to run SignTool using this command.

I have tried adding -Verb RunAs to the end of this command, however, when I then run the command, I get the following error message instead:

"Parameter set cannot be resolved using the specified named parameters."

I found this article on the forums which seems to address a similar issue: https://social.technet.microsoft.com/Forums/windows/en-US/132e170f-e3e8-4178-9454-e37bfccd39ea/startprocess-verb-runas-credential

However, the article is about 4 years old, so I am not sure if it is still relevant today with PowerShell v. 4.0.

I was wondering if there are any better solutions to run Start-Process with elevated credentials (or even using the & Call operator in elevated mode).

Any guidance on how to solve this would be greatly appreciated.

Thanks.


Friday, November 13, 2015 10:08 PM

I gave you the anseer in this thread https://social.technet.microsoft.com/Forums/windowsserver/en-US/38d1f65d-b5a4-4f6f-a267-cef50b758098/capturing-output-from-startprocess-to-powershell-console-host#38d1f65d-b5a4-4f6f-a267-cef50b758098

If you need Admin elevation start PowerShell as an administrator.

\(ツ)_/


Friday, November 13, 2015 10:12 PM

You cannot use -Verb without ShellExecute.

See the parameter sets: https://technet.microsoft.com/en-us/library/hh849848.aspx?f=255&MSPPError=-2147217396

\(ツ)_/