简体   繁体   English

在远程会话中执行时,将PowerShell stdout重定向到文件

[英]Redirect PowerShell stdout to a file when executed in a remote session

I need to log everything printed to stdout to a file. 我需要将打印到标准输出的所有内容记录到文件中。 Start-Transcript / Stop-Transcript works great and does exactly this, but only for the local PowerShell sessions. Start-Transcript / Stop-Transcript可以很好地完成此工作,但仅适用于本地PowerShell会话。 The problem is that Transcripts are not supported when executing PowerShell scripts in a remote session (using Enter-PSSession cmd). 问题在于,在远程会话中(使用Enter-PSSession cmd)执行PowerShell脚本时,不支持脚本。

Is there another (preferably simple) way to redirect all output to a file, even if the .ps1 script is executed in a remote session? 即使.ps1脚本在远程会话中执行,还有另一种(最好是简单的)方法将所有输出重定向到文件吗?
I'd prefer not to have to add >> or Tee or out-file etc. to every line in the script that has output. 我希望不必在已输出脚本的每一行中添加>>Teeout-file等。

I also do not want to Start-Transcript before entering the remote session. 我也不想在进入远程会话之前先进行Start-Transcript I'm remoting to many destinations in a loop and I need each remote session's stdout logged to a separate file (local to the endpoint), not all bundled into a single file. 我要在一个循环中远程到达许多目的地,并且我需要将每个远程会话的标准输出记录到一个单独的文件(对于端点而言是本地的)中,而不是全部捆绑到一个文件中。

I suggest using the Start-Transcript before entering the session, and using the -Path option of that cmdlet to point to a separate file for each session. 我建议在进入会话之前使用Start-Transcript ,并使用该cmdlet的-Path选项指向每个会话的单独文件。 Perhaps something like 也许像

Start-Transcript -Path C:\Logs\$MachineName-$($(Get-Date).ToString("yyyyMMddhhmm")).log

Or something similar. 或类似的东西。 My personal preference is a date-time appendage for historical purposes. 我个人的喜好是出于历史目的的日期时间附件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM