我想使用PowerShell同时将脚本的数据导出到2个文本文件中

我有一个继续运行的Power Shell脚本,并且已经将该脚本的输出导出到记事本文件中,并且我总是将其追加以保持自身更新。

现在,我希望输出到2个纯文本文件。 1个将附加我的输出,2个仅包含最新数据。

如何将同一输出同时导出到2个文本文件? 以下是我的脚本的片段。

$(
write-host 'Checking Status.........'
GC .\file.txt | %{.\Status.ps1 $_}
) *>&1 > WINRMStatus.txt -Append
评论
  • 笙歌i
    笙歌i 回复

    You can combine an appending redirection (>>) with Tee-Object:

    & {
      write-host 'Checking Status.........'
      GC .\file.txt | %{.\Status.ps1 $_}
    } *>&1 | Tee-Object OtherFile.txt >> WINRMStatus.txt
    

    Also note that I've wrapped your statements in a script block ({ ... }) invoked with the call operator (&), which - unlike $(...), the subexpression operator - preserves the streaming behavior of the enclosed commands (sends their output to the pipeline one by one instead of collecting everything in memory first).