Script Template with logging function


posted by Axel Kara
11-26-2009

Downloads: 1,119
File size: 4.4kB
Views: 6,043

Embed
Script Template with logging function
  1. #******************************************************************* 
  2. # Global Variables 
  3. #******************************************************************* 
  4. $Script:Version      = '1.0.9.1126' 
  5. $Script:LogSeparator = '*******************************************************************************' 
  6. $Script:LogFile      = "" 
  7.  
  8. #******************************************************************* 
  9. # Functions 
  10. #******************************************************************* 
  11. function Get-ScriptName(){ 
  12. # 
  13. #      .SYNOPSIS 
  14. #          Extracts the script name 
  15. #      .DESCRIPTION 
  16. #          Extracts the script file name without extention 
  17. #      .NOTES 
  18. #         Author:    Axel Kara, axel.kara@gmx.de 
  19. # 
  20.     $tmp = $MyInvocation.ScriptName.Substring($MyInvocation.ScriptName.LastIndexOf('\') + 1) 
  21.     $tmp.Substring(0,$tmp.Length - 4) 
  22.  
  23. function Write-Log($Msg, [System.Boolean]$LogTime=$true){ 
  24. # 
  25. #      .SYNOPSIS 
  26. #          Creates a log entry 
  27. #      .DESCRIPTION 
  28. #          By default a time stamp will be logged too. This can be 
  29. #        disabled with the -LogTime $false parameter 
  30. #      .NOTES 
  31. #         Author:    Axel Kara, axel.kara@gmx.de 
  32. #      .EXAMPLE 
  33. #          Write-Log -Msg 'Log entry created successfull.' [-LogTime $false] 
  34. # 
  35.     if($LogTime){ 
  36.         $date = Get-Date -format dd.MM.yyyy 
  37.         $time = Get-Date -format HH:mm:ss 
  38.        Add-Content -Path $LogFile -Value ($date + " " + $time + "   " + $Msg
  39.     
  40.     else
  41.         Add-Content -Path $LogFile -Value $Msg 
  42.     
  43.  
  44. function Initialize-LogFile($File, [System.Boolean]$reset=$false){ 
  45. # 
  46. #      .SYNOPSIS 
  47. #          Initializes the log file 
  48. #      .DESCRIPTION 
  49. #         Creates the log file header 
  50. #          Creates the folder structure on local drives if necessary 
  51. #        Resets existing log if used with -reset $true 
  52. #      .NOTES 
  53. #         Author:    Axel Kara, axel.kara@gmx.de 
  54. #      .EXAMPLE 
  55. #          Initialize-LogFile -File 'C:\Logging\events.log' [-reset $true] 
  56. # 
  57. try{ 
  58.         #Check if file exists 
  59.         if(Test-Path -Path $File){ 
  60.             #Check if file should be reset 
  61.             if($reset){ 
  62.                 Clear-Content $File -ErrorAction SilentlyContinue 
  63.             
  64.         
  65.         else
  66.             #Check if file is a local file 
  67.             if($File.Substring(1,1) -eq ':'){ 
  68.                 #Check if drive exists 
  69.                 $driveInfo = [System.IO.DriveInfo]($File
  70.                 if($driveInfo.IsReady -eq $false){ 
  71.                     Write-Log -Msg ($driveInfo.Name + " not ready."
  72.                 
  73.                  
  74.                 #Create folder structure if necessary 
  75.                 $Dir = [System.IO.Path]::GetDirectoryName($File
  76.                 if(([System.IO.Directory]::Exists($Dir)) -eq $false){ 
  77.                     $objDir = [System.IO.Directory]::CreateDirectory($Dir
  78.                     Write-Log -Msg ($Dir + " created."
  79.                 
  80.             
  81.         
  82.         #Write header 
  83.         Write-Log -LogTime $false -Msg $LogSeparator 
  84.         Write-Log -LogTime $false -Msg (((Get-ScriptName).PadRight($LogSeparator.Length - ("   Version " + $Version).Length," ")) + "   Version " + $Version
  85.         Write-Log -LogTime $false -Msg $LogSeparator 
  86.     
  87.     catch{ 
  88.         Write-Log -Msg $_ 
  89.     
  90.  
  91. function Read-Arguments($Values = $args) { 
  92. # 
  93. #      .SYNOPSIS 
  94. #          Reads named script arguments 
  95. #      .DESCRIPTION 
  96. #          Reads named script arguments separated by '=' and tagged with'-' character 
  97. #      .NOTES 
  98. #         Author:    Axel Kara, axel.kara@gmx.de 
  99. # 
  100.     foreach($value in $Values){ 
  101.          
  102.         #Change the character that separates the arguments here 
  103.         $arrTmp = $value.Split("="
  104.          
  105.         switch ($arrTmp[0].ToLower()) { 
  106.             -log
  107.                 $Script:LogFile = $arrTmp[1] 
  108.             
  109.         
  110.     
  111.  
  112. #******************************************************************* 
  113. # Main Script 
  114. #******************************************************************* 
  115. if($args.Count -ne 0){ 
  116.     #Read script arguments 
  117.     Read-Arguments 
  118.     if($LogFile.StartsWith("\\")){ 
  119.         Write-Host "UNC" 
  120.     
  121.     elseif($LogFile.Substring(1,1) -eq ":"){ 
  122.         Write-Host "Local" 
  123.     
  124.     else
  125.         $LogFile = [System.IO.Path]::Combine((Get-Location), $LogFile
  126.     
  127.      
  128.     if($LogFile.EndsWith(".log") -eq $false){ 
  129.         $LogFile += ".log" 
  130.     
  131.  
  132. if($LogFile -eq ""){ 
  133.     #Set log file 
  134.     $LogFile = [System.IO.Path]::Combine((Get-Location), (Get-ScriptName) + ".log"
  135.  
  136. #Write log header 
  137. Initialize-LogFile -File $LogFile -reset $false 
  138.  
  139.  
  140.  
  141. #/////////////////////////////////// 
  142. #/// Enter your script code here /// 
  143. #/////////////////////////////////// 
  144.  
  145.  
  146. #Write log footer 
  147. Write-Log -LogTime $false -Msg $LogSeparator 
  148. Write-Log -LogTime $false -Msg '' 

This template contains a text based logging function.

By default the log file is placed beside the calling script, but you can also specify a log file with the -log script parameter.

Copyright 2012 PowerShell.com. All rights reserved.