且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PowerShell - 为Git命令捕获STDERR输出

更新时间:2023-02-22 14:44:51

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