英文:
How to send a copy of all the output produced by a script to a file in Powershell?
问题
我打算在Azure CLI上运行一个长的Powershell脚本,该脚本将生成一些输出。我的要求是我需要在终端和文件中都能看到这些输出,这可行吗?
在Azure Shell中,终端输出的历史记录不太好。你不能向上滚动到你开始的地方。
我需要证明输出内容。请提供建议。
英文:
I meant to run a long Powershell script on Azure CLI which will produce some outputs. My requirement is that I need those outputs on terminals as well as in a file, Is this achievable?
In Azure Shell, the history of terminal output is not great. You can not scroll up to the point you started.
I need to have a proof of the output.
Please suggest.
答案1
得分: 1
通常情况下,如果您想创建PowerShell会话的_转录_,请使用Start-Transcript
和Stop-Transcript
cmdlet。
-
在_交互式_会话中,转录将包括用户提交的_输入命令_。
-
在脚本内部,只记录脚本的_输出_。
-
无论哪种方式,来自PowerShell的_所有_ 输出流的输出都记录在输出文件中,同时也打印到主机(显示器、控制台、终端)上。
如果您只需要记录_单个脚本调用的_输出到文件中,同时也将其打印到主机,您可以使用Tee-Object
cmdlet:
-
但是 - 与PowerShell管道中的任何命令一样 -
Tee-Object
仅对通过_成功_输出流接收的数据起作用。 -
为了通过管道传递_所有_输出流中的输出,需要进行以下重定向(
*
是_所有_流的占位符,&
表示重定向(>
)的目标流,1
表示_成功_输出流的目标):*>&1
因此,您可能正在寻找以下内容:
./somescript.ps1 *>&1 | Tee-Object -FilePath out.txt
关于捕获_带颜色_输出的说明:
-
上述讨论的两种方法都_不会_保留您在控制台中看到的常规着色,例如错误消息以红色显示,警告以黄色显示,使用
Write-Host
和-ForegroundColor
... -
捕获有颜色的输出的唯一方法是输出_带有嵌入的VT/ANSI转义序列的字符串_。
-
例如,输出以下字符串将在绿色中打印单词
green
,并在文件中捕获它(保留了ANSI/VT转义序列,需要_终端_来呈现它们:# 注意:在PowerShell 7+中,您可以将“$([char]27)”替换为“`e” # 或使用$PSStyle - 请参阅下文。 "下一个单词是$([char]27)[32mgreen$([char]27)[m。其余不是。"
-
在PowerShell(Core)7+中,
`e
可用作ESC字符的转义序列,作为$([char]27)
的便捷替代品。此外,自动变量$PSStyle
允许您使用_符号名称_来表示晦涩的ANSI/VT序列,例如:"下一个单词是$($PSStyle.Foreground.Green)green$($PSStyle.Reset)。其余不是。"
- 还请参阅:about_ANSI_Terminals。
-
英文:
Generally, if you want to create a transcript of a PowerShell session, use the Start-Transcript
and Stop-Transcript
cmdlets.
-
In interactive sessions, the transcript will include the input commands submitted by the user.
-
Inside a script, only the script's output is recorded.
-
Either way, output from all of PowerShell's output streams:
- is recorded in the output file.
- while also printing to the host (display, console, terminal).
If you only need to record a single script call's output in a file while also printing it to the host, you can use the Tee-Object
cmdlet:
-
However - as any command in a PowerShell pipeline -
Tee-Object
only acts on data received via the success output stream. -
In order to pass output from all output streams through the pipeline, the following redirection is needed (
*
is a placeholder for all streams,&
indicates the target stream of the redirection (>
), and1
targets the success output stream):*>&1
Therefore, you were probably looking for the following:
./somescript.ps1 *>&1 | Tee-Object -FilePath out.txt
A note on capturing colored output:
-
Both methods discussed above do not preserve the usual coloring you'd see in the console, such as error messages appearing in red, warnings in yellow, use of
Write-Host
with-ForegroundColor
, ... -
The only way to capture colored output is to output strings with embedded VT/ANSI escape sequences.
-
E.g., outputting the following string would both print the word
green
in green as well as capture it in a file as such (with the ANSI/VT escape sequences preserved, which requires a terminal to render them:# Note: In PowerShell 7+ you can replace "$([char]27)" with "`e" # or use $PSStyle - see below. "The next word is $([char]27)[32mgreen$([char]27)[m. The rest not."
-
In PowerShell (Core) 7+,
`e
is available as an escape sequence for the ESC character, as a convenient alternative to$([char]27)
. Moreover, the automatic$PSStyle
variable allows allows you to express the obscure ANSI/VT sequences using symbolic names, e.g.:"The next word is $($PSStyle.Foreground.Green)green$($PSStyle.Reset). The rest not."
- See also: about_ANSI_Terminals.
-
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论