【问题标题】:Append Text File In PowerShell [duplicate]在 PowerShell 中附加文本文件 [重复]
【发布时间】:2019-10-09 20:33:17
【问题描述】:

所以我正在尝试编写一个快速脚本来帮助我儿子学习他的拼字游戏。我几乎所有东西都在工作,但我想跟踪他的结果,所以我正在尝试写入输出到文本文件。我只关心最近的结果,所以在他开始测试时我删除了现有的结果文件。

我正在为每个人的拼写列表做一个,在每个人的末尾,我都有:

write-host "Word $counter - You spelled $wordcorrectly!!" | Out-File $outputpath -Append

write-host "Word $counter - Sorry.  The correct spelling is $word." | Out-File $outputpath -Append

最后,在遍历所有列表之后,我有:

write-host "You answered $counter out of $WordCount correctly" | Out-File $outputpath -Append

但是当我转到 $outputpath 文件时,它完全是空白的......你猜猜我在这里犯了什么简单的错误?

【问题讨论】:

  • 使用Write-Output 而不是Write-Host

标签: powershell append output


【解决方案1】:

您将Write-Host ... 的结果通过管道传输到Out-File,但Write-Host 没有将任何内容传递给管道(它将内容发送到标准输出),因此Out-File 什么都不做(除了覆盖文件)。因此,您可以将字符串本身通过管道传递给Out-File,它应该可以工作。如果您仍想写入主机字符串(以查看输出),您可以将消息存储在变量中:

$Message = "You answered $counter out of $WordCount correctly"
Write-Host $Message
$Message | Out-File $outputpath -Append

【讨论】:

  • 就是这样,谢谢!
猜你喜欢
  • 1970-01-01
  • 2012-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多