Search code examples
powershellio-redirection

Powershell Script output to variable - capture Write-Host output


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?


Solution

  • 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/