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?
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
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: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