Enhanced Script Logging module (automatic console output captured to file)

Automatically copy PowerShell console output to a log file (from Output, Error, Warning, Verbose and Debug streams), while still displaying the output at the console. Log file output is prepended with date/time and an indicator of which stream originated the line.

 
 
 
 
 
4.4 Star
(19)
7,946 times
Add to favorites
5/12/2016
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • Do we still need this module?
    1 Posts | Last post August 11, 2018
    • I love this module, it works really well and we have it as a standard in many of our scripts. 
      I am just wondering if we still need to use this module, or have Microsoft now included similar functionality in the later version of PowerShell? 
      Thank you! 
  • Error when running PS from SQL Agent
    2 Posts | Last post October 31, 2017
    • When Running my powershell script in the ISE logging works fine.
      When I run this as part of a SQL Agent job it gives me this error:
      
      A job step received an error at line 268 in a PowerShell script. The corresponding line is '			Write-Verbose $newMessage
      '. Correct the script and reschedule the job. The error information returned by PowerShell is: 'Operation is not valid due to the current state of the object.
      '
      
    • This appears to be an issue with the dbatools module and the logging module together.
  • Logging corrupt in SCCM
    3 Posts | Last post July 20, 2017
    • Hello David,
      
      Thank you for this logging module!
      We have implemented it in several scripts and it works great.
      
      However, we have now run into an issue:
      When we run the scripts (which use PowerShellLogging 1.2.1) with SCCM, the logging that can be viewed from the SCCM console has lots of Chinese characters.
      
      SCCM Version 1702, SCCM Console version 5.008498.1711.
      The logging can be found here:
      Monitoring > Deployments >  “Jobname” > View Status > “Devicename” > More Details > Status
      Then hover over “action output” and the PowerShell console output from the scripts should appear.
      
      Scripts that do not use the PowerShellLogging module have a correct output.
      Also the letters that are used in the logfile as streamindicator do not appear in SCCM PowerShell console output anymore.
      Example:
      Thu, 06 Jul 2017 13:34:27 GMT - [V] Importing alias ....
      
      in SCCM:
      ERBOSE: Importing alias ....
      
      (the V from Verbose is missing)
      
      Is this an issue that can be solved?
      
      Thank you very much,
      
      Jaco de Weerd
    • Forgot to mention: All systems are using PowerShell 4
    • Eh well, almost.
      On systems that use PowerShell 4, only the V from verbose is missing quite often.
      The Chinese characters only appear on systems that have PowerShell 2.
  • Issue importing module
    4 Posts | Last post October 19, 2016
    • Hi David, 
      
      I am having issues importing the module. I get the following error. 
      
      Import-Module : Could not load file or assembly
      'file:///C:\Windows\system32\WindowsPowerShell\v1.0\Modules\powershelllogging\PowerShellLoggingModule.dll' or one of
      its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)
      At line:1 char:1
      + Import-Module powershelllogging
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : NotSpecified: (:) [Import-Module], FileLoadException
          + FullyQualifiedErrorId : System.IO.FileLoadException,Microsoft.PowerShell.Commands.ImportModuleCommand
      
      Any help would be appreciated.
      
      Thanks,
      
      chris
      
      
    • Figured it out. 
      
      Thanks
      CL
    • What caused your issue? how did you fix it? 
    • Had the same Issue - had to go into the properties of each file within that module and "Unblock" it.  Then closed out Powershell re-opened and all was well.
  • Odd behavior after Disable-Logfile
    1 Posts | Last post August 24, 2016
    • Hi David - I love using this tool. I makes my life much easier in writing PS scripts. I don't have to worry about some kind of wrapper function I have to call all the time. I do see some odd behavior though, let me explain. 
      
      I'm loading a series of  child scripts using a parent / root script to call each one. With each child script being called I'm enabling a specific logfile that is uniquely named for each child script, this is done sequentially. When child script returns to the parent I disable that logfile for that script. This method works great and keeps things seperate so that I know that is being done in which script. I am seeing what looks like residual messages from a previous child script getting written to one of the other child scripts at some later point. Other than calling Disable-LogFile is there something else I can do to ensure the buffers are cleared out before the next script starts logging?
      
      The meat and potatos "if you will" of the processing occurs here:
      
      Try 
          {
            Write-Output "------------------- Starting [ $module ] ----------------------"
            $script:LogFile = Enable-LogFile -Path $logPath -ErrorAction Stop
            Import-Module -Name $ParentScriptRoot\Modules\Tasks\$module.psm1 -Force -ErrorAction Stop
            Invoke-Expression -Command "$function" -ErrorAction stop
          }
          Catch 
          {
            $ErrorMessage = $_.Exception.Message
            Write-Error $ErrorMessage -Verbose
          } 
      	Finally 
      	{
            $LogFile | Disable-LogFile
            Remove-Module $module  
          }
      
      Each module in this case is a child script. You have any thoughts on this? I'd greatly appreciate it.
  • Localization parameters
    2 Posts | Last post August 12, 2016
    • Hi David,
      Thanks for your great job. One question:
      Is posible define with parameters TimeZone, date format, language, etc.?
      
      Spanish characters (UNICODE or UTF-8) not write correctly to logfile:
      Example: 
      - Console: áéíóú
      - LogFile: áéíóú
      
      Sorry for my bad english.
      Diego.
      
    • I'll have to add that in a new version.  Thanks for the feedback!
  • Using Resume and Suspend ?
    2 Posts | Last post August 12, 2016
    • Hi David - Great module you have here, but I was wondering how do you resume or suspend? I've tried like this:
      
      $SummaryLog = 'C:\Summary.log'
      $SummaryLogRef = Get-LogFile -Path $SummaryLog
      $SummaryLogRef | Resume-Logging
      
      But I get an error that states the function doesn't accept a pipeline input.
      
      So how does one use it, as I cannot find an example.
      
      The whole purpose here is while I'm doing some processing I'm writing out to a main log but want to write to another log at some point and give a summary of what was done then revert back to writing to the main log.
    • Suspend-Logging and Resume-Logging are global; they stop and resume _all_ log files that the module has running.  If you want to stop / resume a single file while still having others going, then you'd use Disable-LogFile and Enable-LogFile.
  • Can you use Set-PSDebug -Trace 2 with your PowerShellLogging module?
    3 Posts | Last post July 02, 2016
    • Hi David,
      
      I've got a number of Windows batch maintenance scripts which I'm planning on rewriting using Powershell. My existing scripts produce a log of their execution and I would like to provide the same functionality in my Powershell scripts. To do this I'm trying to use Set-PSDebug -Trace 2 with your PowerShellLogging module. This works perfectly using the Enable-LogFile / Disable-LogFile cmdlets. However, I would prefer the log to include a different prefix to the one produced by these cmdlets. You've obviously allowed for this by providing the Enable-OutputSubscriber and Disable-OutputSubscriber cmdlets. However, I'm hitting a problem when I use the OutputSubscriber cmdlets. I get a "Windows PowerShell ISE has stopped working" error if I run my script using ISE and a "Process is terminated due to StackOverflowException" error when running within the PowerShell console.
      
      I can supply a test script which details what I am doing. By commenting out the Set-PSDebug -Trace 2 statement within this script it successfully ends but obviously without the trace information. By moving the Set-PSDebug -Trace 2 statement to the start of the script it is the  
      $LogFile = Enable-OutputSubscriber ` statement which is being processed when the script fails.
      
      Any suggestions on what I'm doing wrong or how I can resolve?
      
      Many thanks
      Ian Adams
      
      PS I'm new to Powershell so feel free to tell me I'm being dumb ….
    • Hmm... will have to think on that.  The problem is that when a script block is executed, it generates some debug output... which executes your script block, which generates more debug output, and so on.  Maybe what I need to do is set a flag that ignores output generated while the subscribers are responding to an event.
    • Hi David,
      
      I was wondering if you've had a chance to think any more about this?
      
      Cheers
      Ian
  • Timestamp is wrong timezone
    3 Posts | Last post May 12, 2016
    • I understand why you are using 'GMT' due to the previous answers to other questions.  But when I use your module it logs the event as happening at "Thu, 12 May 2016 10:40:43 GMT" even though it is really happening at 10:40:43 CST.  This means the the time stamp is actually wrong.  It is using my local time but appending "GMT" to the end without converting the time to GMT.
    • Wow, that bug has been around for a very long time!  :)  Lots of references to DateTime.Now instead of DateTime.UtcNow in the code.  Will fix.
    • Should be all set now, thanks for reporting!
  • Anyway to overwrite previous log?
    4 Posts | Last post April 07, 2016
    • This is great, thanks for contributing.  Is there a way to have it overwrite the log file at the start?
    • Not in this module's commands, no.  You'd need to add a Remove-Item or Set-Content command just before calling Enable-LogFile or Enable-OutputSubscriber.
    • Great, thanks for the feedback, only thing i'm not seeing working is the write-verbose, just get a blank line:
      
      http://i.imgur.com/eHrDBoD.png
      
      no biggie, probably doing something wrong.
    • NEVERMIND!  Didnt have:
      
      $VerbosePreference = 'Continue' 
      $DebugPreference = 'Continue' 
1 - 10 of 26 Items