微软文档说*这两行是等价的:
"test1" | Out-File -FilePath temp-log.txt
"test1" > temp-log
但是以下极其简单的脚本(仅在细节上有所不同)导致在 temp-transcript.txt
中捕获的输出完全不同。
这个使用详细 | Out-File
的版本按预期工作(test2
出现在成绩单中):
Start-Transcript -Path temp-transcript.txt > $null
"test1" | Out-File -FilePath temp-log.txt -Append
echo "test2"
Stop-Transcript > $null
虽然这个使用 >
运算符的不起作用(成绩单为空)
Start-Transcript -Path temp-transcript.txt > $null
"test1" > temp-log.txt
echo "test2"
Stop-Transcript > $null
我在这里错过了什么?或者它是一个 powershell 错误?
*: “而且由于重定向操作符(> 和 >>)实际上是 Out-File 的别名” -- https://www.google.com/url?q=https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_redirection&sa=U&ved=2ahUKEwjnj_rv653xAhXvgf0HHUw6DfIQFjAAegQIBxAB&usg=AOvVaw0f5Rs7UoanapqG5xfjRy8A
P.S. #1,当我写这个问题时,我开始意识到这看起来太像一个错误。事实上,我无法在 powershell core 7 中重现它。我只在 PowerShell 5.1 中体验过它。这是我的 $PSVersionTable.PSVersion:
Major Minor Build Revision
----- ----- ----- --------
5 1 19041 1023
P.S. #2,另一种“修复”脚本的方法是在附加到 temp-log.txt 的命令之前移动命令 echo "test2"
。 这对我来说也没有任何意义。
P.S. #3,我想你不会惊讶地发现我用这个愚蠢的错误调试一个更大的脚本已经浪费了一个多小时!啊!