【问题标题】:PowerShell removing console message colors when using tee-objectPowerShell 在使用 tee-object 时删除控制台消息颜色
【发布时间】:2011-03-19 06:44:37
【问题描述】:

在使用 tee-object 时,有什么方法可以阻止 PowerShell 删除控制台消息颜色?

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

powershell.exe -noprofile -file $project_root/test_main.ps1

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

powershell.exe -noprofile -file $project_root/test_main.ps1 | tee-object -FilePath $log

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

【问题讨论】:

    标签: powershell redirect powershell-2.0 tee


    【解决方案1】:

    试试这个:

    powershell.exe -noprofile -command { $path\test_main.ps1 | tee-object $log }
    

    这是因为这部分是先执行的:

    powershell.exe -noprofile -file $project_root/test_main.ps1 
    

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

    【讨论】:

    • 我明白你在说什么;但是,我无法使该命令方法起作用。第一个错误是“您必须在 '/' 运算符的右侧提供值表达式。”我试图逃脱它没有运气。然后我尝试将“$path\test_main.ps1 | tee-object $log”设置为 $command var,所以我调用 powershell.exe -noprofile -command { $command } 但这甚至没有调用脚本或抛出错误我调用了一个不存在的脚本。正如您所看到的 Keith,我正在处理您的博客文章“有效的 PowerShell 项目 14:捕获脚本的所有输出”来捕获和记录所有输出:)
    • 如果您从 PowerShell.exe 运行该命令,它应该可以工作(至少它在我的测试脚本中对我有用)。如果您在 PowerShell 之外的其他地方运行它,请使用 ... -command "&{ $path\test_main.ps1 | tee-object $log }"
    • RE “捕获脚本的所有输出” - 当 PowerShell 2.0 发布时没有解决脚本日志记录问题时,我感到很失望。 :-(
    猜你喜欢
    • 1970-01-01
    • 2016-08-20
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    相关资源
    最近更新 更多