【问题标题】:Write stdout to screen and file [duplicate]将标准输出写入屏幕和文件[重复]
【发布时间】:2015-10-16 04:40:38
【问题描述】:

从 powershell 控制台运行 powershell 脚本时,我可以将输出重定向到如下文件:

powershell script.ps1 > file.txt

但是,我需要将输出重定向到文件并仍将其打印到屏幕上。我该怎么做?

【问题讨论】:

  • 查看 Tee-Object cmdlet,看看它是否能让您朝着正确的方向前进。

标签: powershell stdout io-redirection


【解决方案1】:

使用transcript。您可以通过以下两种方式之一进行操作,最简单的可能是在您的脚本中包含这些行:

在“Script.ps1”中

Start-Transcript "C:\file.txt"
# the rest of your script
Stop-Transcript

第二种方法是这样调用你的脚本:

Start-Transcript "C:\file.txt"
.\Script.ps1
Stop-Transcript

请记住,如果您尝试启动一个脚本,而之前的脚本没有停止,则会引发错误。因此,在您的脚本中,在 Start-Transcript 命令之前,可能只是为了确保脚本不再运行:

try{
Stop-Transcript
}catch{}
Start-Transcript "C:\file.txt"
# the rest of your script
Stop-Transcript

【讨论】:

  • 1) 我宁愿只有一个单行 2) 这会转录所有内容,而不仅仅是标准输出
  • 警告未来的居民:这不适用于 Python 3,并且可能适用于更多软件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多