Write-Log PowerShell Logging Function

The Write-Log PowerShell advanced function is designed to be a simple logger function for other cmdlets, advanced functions, and scripts. Often when running scripts one needs to keep a log of what happened and when. The Write-Log accepts a string and a path to a log file and ap

 
 
 
 
 
4.4 Star
(12)
8,578 times
Add to favorites
4/13/2017
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • Can I use Write-Log in place of out-file?
    2 Posts | Last post December 18, 2015
    • Hi There:
      Trying to make the jump from bash scripting to PowerShell. Script logging is one issue I hadn't expected.
      I'd like to use Write-Log to capture any output from my scripts, specifically stdout or stderr (If I can use bash terms) from any executables I may call within the script.
    • My version of write-log cannot do what you want, however if you search this gallery you may find scripts that do that job a little better. What really might help you is to use Start-Transcript which will log everything you type and output to a log file. Then when you're done you can Stop-Transcript.  I have heard some users use Start-Transcript at the beginning of the day to log all their PowerShell activities.
  • where will i insert this script?
    2 Posts | Last post October 12, 2015
    • I have a simple script which collects all the computer disk space and send it as a mail. I want also to create a log files if any error or what all happens when the script is running. Please tell me where should I enter this script so if we run the script it will create log files. If there was any error I can use that log file to find the problem and resolve it.
    • This script is a function so you will either need to dot source it or add it to your module.
      
      dot source
      . .\Function-Write-Log.ps1
      
      After you dot source the script it loads the function into memory for you to use with your other scripts.
      
      Where you would use it in the script is anywhere you would like to log.
      Example
      try {
      Write-Log -Message "Getting Disk space for $Computer" -Path $LogFileName -Level Info
      $DiskInfo = get-wmiobject -class win32_logicaldisk -erroraction stop
      }
      catch {
      Write-Log -Message "Error getting disk space for $Computer $Error[0].Exception" -Path $LogFileName -Level Error
      }
  • Another approach
    2 Posts | Last post April 03, 2015
    • This is the version I came up with a few years ago. Feel free to steal any concepts if you think they will help. 
      
      http://jdhitsolutions.com/blog/2011/03/powershell-automatic-logging/
    • I have re-written the script to use similar methods to yours. Thanks again.
  • Force seems counterintuitive
    4 Posts | Last post April 02, 2015
    • I like the idea and that you automatically add a time stamp to the message. The use of -Force feels backwards. Usually -Force means overwrite an existing file. If I am creating a brand new file I shouldn't have to force that to happen, but for something like logging yes, I probably don't want to accidentally overwrite an existing file. But if I do, that's when -Force makes sense.
      
      I'd also suggest using a parameter of Path instead of Logpath as the former is more widely used in other cmdlets. You can use LogPath as an alias if you want. The recommended practice is to use common parameter names instead of creating new ones that serve the same purpose.
    • Done and Done. Thank you.
    • In more testing I realized that the -Force option over-complicated things. The danger is that add-content can append to any file, binary or text.
    • I understand your logic problem now. Part of the challenge is trying to understand how some is going to use your tool. In this case, most likely they will call Write-Log from their script or function. With log files a common concern is accidentally overwriting an existing file that you need to keep. That's where we started with -Force. Another approach would be to use the -NoClobber parameter. I added a parameter:
      
      [switch]$NoClobber 
      
      Then modified the main code:
      
       if ((Test-Path $path) -AND $NoClobber) {
                  Write-warning "Log file $path already exists and you specified NoClobber. Either delete the file or specify a different name."
                  #bail out
                  Return
              }
              elseif (Test-Path $Path) { 
                  # Appending to existing log file. 
                  $TimeStamp = (Get-Date -Format "yyyy-MM-dd HH:mm:ss") 
                  Write-Verbose "Adding $TimeStamp $LogContent to $Path" 
                  Add-Content $Path -Value "$TimeStamp $LogContent" 
                  } 
      
      I might use it like this in a script:
      
      $log = "d:\temp\logtest3.txt"
      
      $logging = $True
      #or bail out if logging can't happen?
      0..4 | foreach {
       if ($logging) { Write-Log -LogContent "Updating pass $_" -Path $log }
       start-sleep -Seconds (get-random -Minimum 5 -max 10)
      }
      
      I avoid overwriting any existing log files. Subsequent writes only happen if I know I was able to write to the log. Or I can not use NoClobber in which case if the the file doesn't exit, it will be created. Or if it does then I will append. Again, it comes down to how you think you intend to use it. And as for your concern about Add-Content, perhaps sending text using Out-File would be better.
11 - 14 of 14 Items