Script logging best practices

I’m working on building a script to do VM creation/install OS/add to domain/configure OS, and one of the requirements is that the process be logged in some standardized format (to be determined).

What are recommended best practices for logging the execution of such a script?

Start-Transcript

Is this script going to be executed on a server or in a PE? On a running server you can enable scriptblock logging, windows logs to event viewer and non windows can log to syslog server. Check the following links for more information on this.

https://searchwindowsserver.techtarget.com/tutorial/Set-up-PowerShell-script-block-logging-for-added-security

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_logging_windows?view=powershell-7

 

If you’re needing the actual script to log you could log to a network share or syslog server. Below are some articles that can help with this as well.

https://spiderip.com/blog/2018/07/syslog

https://thwack.solarwinds.com/t5/NPM-Documents/Send-syslog-using-PowerShell/ta-p/521776

https://stackoverflow.com/questions/303045/connecting-to-a-network-folder-with-username-password-in-powershell

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-psdrive?view=powershell-7

 

Sam already pointed out start-transcript, which could be used for logging interactive or automated scripts. It provides lots of information about the host environment. You can also add the -IncludeInvocationHeader switch and get timestamps added to the log. Don’t forget Stop-Transcript

 

I hope this helps.