Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke-Expression, not all output returned to variable

I'm using some GIT commands in my PowerShell scripts. Most of the time I'm calling the GIT commands via Invoke-Expression so that I, e.g.

  • can parse the output, or/and
  • forward the out to a logging method.

At some GIT commands I recognized that not all output is returned via Invoke-Expression though the documentation states:

Outputs

PSObject

Returns the output that is generated by the invoked command (the value of the Command parameter).

Here is an example:

> $x = iex "git fetch --all"
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 3), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), done.

Content of $x:

> $x
Fetching origin
Fetching upstream

So the main information is not returned to $x. I can't imagine that git fetch --all is returning the main information via stderr (wouldn't make sense ...).

I also found this PowerShell question, which is unanswered and the used PowerShell version is 2.

Used PowerShell version:

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.2.0
PSEdition                      Core
GitCommitId                    6.2.0
OS                             Microsoft Windows 10.0.18362
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

How can I force Invoke-Expression to return the whole output?

Thx

like image 284
Moerwald Avatar asked Oct 23 '25 14:10

Moerwald


1 Answers

As I mentioned in "PowerShell Capture Git Output", with Git 2.16 (Q1 2018), you can try and set first:

set GIT_REDIRECT_STDERR=2>&1

Then in your Powershell script, you should get both stdout and stderr outputs,

See also dahlbyk/posh-git issue 109 for a more Powershell-like example:

$env:GIT_REDIRECT_STDERR = '2>&1'
like image 194
VonC Avatar answered Oct 25 '25 04:10

VonC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!