I've seen some examples out there, but they all involve just sending manually entered text to a log file. What I'd like is something that can accept pipeline input and have that appear in the log. (Ex: Get-Service | Write-Log). Or have the entire contents from the Start/Stop-Transcript piped to a log file.
What have you come across that you've found to work well? Any recommendations?
Here's what I use: http://gallery.technet.microsoft.com/Enhanced-Script-Logging-27615f85
If you use the *-LogFile cmdlets, you get the default behavior, which is to prepend a date / time stamp to each line of output, along with an indicator of which stream it came from ([E] for error, [V] for verbose, etc) and send them to a log file. There are also equivalent *-OutputSubscriber cmdlets which allow you to define the behavior yourself. When you use those commands, you give it a Script Block which will be executed for each line of intercepted console output.
Long story short, I only really have to add two lines of code to my scripts in order to get a log file:
# near the beginning of the script: $script:logFile = Enable-LogFile -Path c:\path\to\log\file.txt # At the end of the script (optional, but can keep unrelated console output from bleeding into the script's log file): $script:logFile | Disable-LogFile
You must be logged in to reply to this topic.