1

I have an python script which will invoke the nmake. I want to invoke this script in powershell as following.

python D:\build.py -f folder

It works fine and the output is as following:

Microsoft (R) Program Maintenance Utility Version 11.00.50727.1
Copyright (C) Microsoft Corporation.  All rights reserved.

But if I add the redirection to make all the stderr&stdout goes to build.log as following, it show red errors:

python D:\build.py -f folder 2>&1 | tee build.log

python :
At line:1 char:1
+ python D:\source_code\media\media\build_system\build.py -f xplatform_ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Microsoft (R) Program Maintenance Utility Version 11.00.50727.1
Copyright (C) Microsoft Corporation.  All rights reserved.

---- Edit 1----

I understand possible reason is the external nmake executable write to stderr, then Powershell show error.

But the first example, which without redirection, doesn't show error. And the second one, which have redirect stderr to stdout, actually show errors. So any reason?

Zijing
  • 11
  • 3

1 Answers1

0

I'd like to answere my question so it can be closed and helpful for others.

The reason it show error is because the ErrorRecord object are print in stdout.

And the 2>&1 redirect the object instead of text. So after add the redirection, the ErrorRecord are redirect to stdout.

More information can be found at: $LastExitCode=0 but $?=False in PowerShell. Redirecting stderr to stdout gives NativeCommandError

Community
  • 1
  • 1
Zijing
  • 11
  • 3