4

Using this script: https://github.com/byterogues/powershell-bittrex-api which I call from another script.

e.g.

$order = .\bittrex-api.ps1 -action buylimit -market BTC-TX -quantity 1 -rate 0.00011300

bittrex-api.ps1 catches an error and shows this on screen

BITTREX ERROR: API Query returned an error. Error Message: MIN_TRADE_REQUIREMENT_NOT_MET

How can I capture the output from bittrex-api.ps1 into a variable so I can use this variable in my base script?

mklement0
  • 382,024
  • 64
  • 607
  • 775
Mister Iks
  • 165
  • 1
  • 4
  • 13

2 Answers2

6

To complement Frode F.'s helpful answer, which sensibly recommends modifying the script to use Write-Error for error reporting:

If modifying the code is not an option and you're using PSv5+, you can capture Write-Host output, because since PSv5 Write-Host writes to the newly introduced information output stream (to which primarily Write-Information is designed to write):

PowerShell's output streams are numbered, and the information stream has number 6 so that output-redirection expression 6>&1 redirects the information stream into the success output stream, whose number is 1, allowing regular capturing in a variable, as the following example shows:

# PSv5+

$captured = Write-Host 'write-host output' 6>&1

$captured # output what was captured -> 'write-host output'

To learn more about PowerShell's output streams and redirection, run Get-Help about_Redirection

Note:

  • Write-Host output captured via 6>&1 consists of one or more System.Management.Automation.InformationRecord instances, which print as if they were strings, namely by their .MessageData.Message property value, which is the string content of the argument(s) passed to Write-Host.

  • Therefore, any coloring that stems from the use of the -ForegroundColor and -BackgroundColor parameters is not (directly) passed through:

    • However, the information is preserved, namely in the .MessageData.ForegroundColor and .MessageData.BackgroundColor properties, along with the information about whether -NoNewLine was passed to Write-Host, in Boolean property .MessageData.NoNewLine

    • This answer shows how to recreate the original coloring from the captured objects.

  • By contrast, coloring via ANSI / VT escape sequences embedded in the original string argument(s) is preserved.


Note: To capture the output and also pass it through (to the success output stream), you have two options:

  • Simply enclose the statement in (...), but note that output will only show after all output has been collected.

    ($captured = Write-Host 'write-host output' 6>&1)
    
  • For streaming pass-through, use Tee-Object with the -Variable parameter:

    Write-Host 'write-host output' 6>&1 | Tee-Object -Variable captured
    

The above techniques generally work for capturing and passing success-stream output through, irrespective of whether redirections are involved.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • How would you get both the output displayed **and** results put into the variable? – not2qubit Dec 09 '22 at 20:37
  • @not2qubit, please see my update; the required techniques aren't specific to dealing with `Write-Host` output. – mklement0 Dec 09 '22 at 21:04
  • Hi I tried that, but coloring is lost in the output. `Write-Host -ForegroundColor Red "[ERROR]: Package id: ""jaja"" not found!" 6>&1 | Tee-Object -Variable zz`. Posted a separate question about this [here](https://stackoverflow.com/questions/74748384/how-to-get-both-the-output-and-results-to-a-varibale-using-write-host-in-powersh). – not2qubit Dec 09 '22 at 21:22
4

You can't. The script uses Write-Host to output the error. Write-Host only writes text to the console, it doesn't return any objects which means there's nothing to capture.

I would recommend modifying the script to use other cmdlets like Write-Error, Write-Output or any other Write-* which outputs to a stream (which you can redirect to the stdout-stream and save).

See http://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful/

Frode F.
  • 52,376
  • 9
  • 98
  • 114