将所有输出重定向到 PowerShell 中的文件

Dar*_*te1 4 powershell stderr

我想将所有输出重定向到一个文件,如下所示:

Write-Error 'bla' *>>  '\\SERVER\s$\Test\test.log'
Run Code Online (Sandbox Code Playgroud)

问题是当我想使用 a 时LiteralPath,它会失败,因为它试图将路径解析为 a wildcard path。所以这失败了:

Write-Error 'bla' *>>  '\\SERVER\s$\Test\test[0].log'
Run Code Online (Sandbox Code Playgroud)

尝试时,Write-Error 'bla' | Out-File -LiteralPath它不会将错误写入文件,而只会写入控制台。

当文件名中使用特殊字符时,有没有办法将所有输出都保存在文件中?

Chr*_*ris 8

您可以使用Start-TranscriptStop-Transcript cmdlet:

Start-Transcript -Path '\\SERVER\s$\Test\test[0].log'
# Your code
Stop-Transcript
Run Code Online (Sandbox Code Playgroud)


Dar*_*te1 6

我想我已经找到了:

Write-Error 'bla' 2>&1 | Out-File -LiteralPath '\\SERVER\s$\Test\test[0].log' -Append
Run Code Online (Sandbox Code Playgroud)