PowerShell - Capturing STDERR Output for Git Comma

2019-05-29 14:28发布

Problem

I am trying to capture the STDERR outputs into a log file; however, I have been encountering some serious inconsistent issues that are making my task harder.

First off, I have my ErrorActionPreference to 'Stop', which is probably part of the problems I am enduring.

Second, I have tried two variants for EACH git command in my code displayed below:

Variant 01

& git add $path2File 2>&1 | Out-File "$LogPath\$tmpLogName" -Append
& git commit -m "Some logical update message" 2>&1 | Out-File "$LogPath\$tmpLogName" -Append
& git status 2>&1 | Out-File "$LogPath\$tmpLogName" -Append
& git push 2>&1 | Out-File "$LogPath\$tmpLogName" -Append

Variant 02

& git add $path2File 2>&1 >> "$LogPath\$tmpLogName" -Append
& git commit -m "Some Logical update message" 2>&1 >> "$LogPath\$tmpLogName" -Append
& git status 2>&1 >> "$LogPath\$tmpLogName" -Append
& git push 2>&1 >> "$LogPath\$tmpLogName" -Append

There were several other variants in which I mixed up which command used which variant depending upon common issues I had encountered.

The primary issues I have been encountering are:

1) whenever an STDERR output is encountered, it is treated as a terminating error, regardless of the message.

2) My attempts to create custom Try/Catch statements have failed due to a number of reasons. Exit Codes were not always the same for successful STDERR messages. Couldn't use plain text matches for anything beyond warnings due to messages not always using the same or similar messages.

NOTE: I am trying to do this without changing the ErrorActionPreference unless absolutely necessary.

Question

Does anyone know how to handle logging the STDERR messages for git commands so that it always logs the responses without throwing a terminating error except for true critical errors?

2条回答
霸刀☆藐视天下
2楼-- · 2019-05-29 14:56

You can try using -q to suppress the git output. It should still error, if there is an actual git error.

I think you may also be able to run it in just Powershell, not the ISE and it should work properly

查看更多
Juvenile、少年°
3楼-- · 2019-05-29 14:59

Looks like I get to answer my own question this time around.

Using git-scm.com/docs I was able to learn that some of the git commands had a --porcelain option which allows me to convert the outputs into a machine readable format and pipe them to the output. Then, using a combination of >> and *>> depending on the command, I came up with the following:

& git checkout --track $branch >> $logFilePath

# some set of actions

& git add . *>> $logFilePath
& git commit -m "some automated update message" *>> $logFilePath
& git status --porcelain >> $logFilePath
& git push --porcelain >> $logFilePath

This allowed me to get the information I wanted to record without the false terminating errors, and If I needed to mask anything (IP Address, PI, etc), I could do so before it ever gets logged or displayed.

If there is no need to do so, using Tee-Object has also been extremely helpful to achieve the desired results while still triggering any errors appropriately rather than just generating a terminating error simply because it came from STDERR

查看更多
登录 后发表回答