PowerShell在使用tee-object时删除控制台消息颜色

Det*_*ric 12 powershell redirect tee powershell-2.0

有没有办法在使用tee-object时阻止PowerShell删除控制台消息颜色?

当我没有tee-object运行时,我得到了很好的错误和详细的powershell消息颜色,如下所示:

powershell.exe -noprofile -file $project_root/test_main.ps1
Run Code Online (Sandbox Code Playgroud)

用颜色http://i32.tinypic.com/250779w.jpg

但是,当我使用tee-object(b/c我想要记录到控制台和文件)时,控制台上没有显示消息颜色(我知道文件不会显示它),如下所示:

powershell.exe -noprofile -file $project_root/test_main.ps1 | tee-object -FilePath $log
Run Code Online (Sandbox Code Playgroud)

没有颜色http://i29.tinypic.com/bzpc2.jpg

如果powershell只是使用tee-object将输出拆分为除控制台之外的文件,为什么我会丢失控制台格式?

Kei*_*ill 5

试试这个:

powershell.exe -noprofile -command { $path\test_main.ps1 | tee-object $log }
Run Code Online (Sandbox Code Playgroud)

发生这种情况是因为首先执行此部分:

powershell.exe -noprofile -file $project_root/test_main.ps1 
Run Code Online (Sandbox Code Playgroud)

这样tee-object看到的是原生EXE的输出.而AFAICT,PowerShell不会从本机EXE输出错误记录(或突出显示)stderr输出(除非您重定向错误流,例如2>err.log.