2
votes

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. The problem is that Transcripts are not supported when executing PowerShell scripts in a remote session (using Enter-PSSession cmd).

Is there another (preferably simple) way to redirect all output to a file, even if the .ps1 script is executed in a remote session?
I'd prefer not to have to add >> or Tee or out-file etc. to every line in the script that has output.

I also do not want to Start-Transcript before entering the remote session. 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.

1

1 Answers

0
votes

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. 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.