Search This Blog

Thursday, July 09, 2015

Logging in Powershell - transcript

I come from a strong batch file background, and so to create logs whilst running batch files was just a matter of redirecting IO to a file.

As i migrate to powershell I discovered this was not as easy, maybe I am missing something, but anyway the closest I could find was the transcript command.

By running this command it would direct any output to a file.

Start-Transcript -path $scriptPath\psOutput_name.log

Once this command is run, any output generated that would normally be directed to the powershell command window will be directed to the transcript log.

You must stop the transcript at the end of the process, otherwise you will get errors the next time it runs.

However I found issues if the script error-ed or was terminated with out the stop-transcript command having been run.

Therefore whilst I have a transcript command in each script, the first command is always a stop-transcript command. This may throw an error if a transcript is not running, but for me that is better than having no output logged.
Start-Transcript -path $scriptPath\psOutput_name.log Stop-Transcript


No comments:

Post a Comment