Guest User

Untitled

a guest
Jan 19th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.23 KB | None | 0 0
  1. Function Log-Start{
  2. <#
  3. .SYNOPSIS
  4. Creates log file
  5.  
  6. .DESCRIPTION
  7. Creates log file with path and name that is passed. Checks if log file exists, and if it does deletes it and creates a new one.
  8. Once created, writes initial logging data
  9.  
  10. .PARAMETER LogPath
  11. Mandatory. Path of where log is to be created. Example: C:\Windows\Temp
  12.  
  13. .PARAMETER LogName
  14. Mandatory. Name of log file to be created. Example: Test_Script.log
  15.  
  16. .PARAMETER ScriptVersion
  17. Mandatory. Version of the running script which will be written in the log. Example: 1.5
  18.  
  19. .INPUTS
  20. Parameters above
  21.  
  22. .OUTPUTS
  23. Log file created
  24.  
  25. .NOTES
  26. Version: 1.0
  27. Author: Luca Sturlese
  28. Creation Date: 10/05/12
  29. Purpose/Change: Initial function development
  30.  
  31. Version: 1.1
  32. Author: Luca Sturlese
  33. Creation Date: 19/05/12
  34. Purpose/Change: Added debug mode support
  35.  
  36. .EXAMPLE
  37. Log-Start -LogPath "C:\Windows\Temp" -LogName "Test_Script.log" -ScriptVersion "1.5"
  38. #>
  39.  
  40. [CmdletBinding()]
  41.  
  42. Param ([Parameter(Mandatory=$true)][string]$LogPath, [Parameter(Mandatory=$true)][string]$LogName, [Parameter(Mandatory=$true)][string]$ScriptVersion)
  43.  
  44. Process{
  45. $sFullPath = $LogPath + "\" + $LogName
  46.  
  47. #Check if file exists and delete if it does
  48. If((Test-Path -Path $sFullPath)){
  49. Remove-Item -Path $sFullPath -Force
  50. }
  51.  
  52. #Create file and start logging
  53. New-Item -Path $LogPath -Value $LogName -ItemType File
  54.  
  55. Add-Content -Path $sFullPath -Value "***************************************************************************************************"
  56. Add-Content -Path $sFullPath -Value "Started processing at [$([DateTime]::Now)]."
  57. Add-Content -Path $sFullPath -Value "***************************************************************************************************"
  58. Add-Content -Path $sFullPath -Value ""
  59. Add-Content -Path $sFullPath -Value "Running script version [$ScriptVersion]."
  60. Add-Content -Path $sFullPath -Value ""
  61. Add-Content -Path $sFullPath -Value "***************************************************************************************************"
  62. Add-Content -Path $sFullPath -Value ""
  63.  
  64. #Write to screen for debug mode
  65. Write-Debug "***************************************************************************************************"
  66. Write-Debug "Started processing at [$([DateTime]::Now)]."
  67. Write-Debug "***************************************************************************************************"
  68. Write-Debug ""
  69. Write-Debug "Running script version [$ScriptVersion]."
  70. Write-Debug ""
  71. Write-Debug "***************************************************************************************************"
  72. Write-Debug ""
  73. }
  74. }
  75.  
  76. Function Log-Write{
  77. <#
  78. .SYNOPSIS
  79. Writes to a log file
  80.  
  81. .DESCRIPTION
  82. Appends a new line to the end of the specified log file
  83.  
  84. .PARAMETER LogPath
  85. Mandatory. Full path of the log file you want to write to. Example: C:\Windows\Temp\Test_Script.log
  86.  
  87. .PARAMETER LineValue
  88. Mandatory. The string that you want to write to the log
  89.  
  90. .INPUTS
  91. Parameters above
  92.  
  93. .OUTPUTS
  94. None
  95.  
  96. .NOTES
  97. Version: 1.0
  98. Author: Luca Sturlese
  99. Creation Date: 10/05/12
  100. Purpose/Change: Initial function development
  101.  
  102. Version: 1.1
  103. Author: Luca Sturlese
  104. Creation Date: 19/05/12
  105. Purpose/Change: Added debug mode support
  106.  
  107. .EXAMPLE
  108. Log-Write -LogPath "C:\Windows\Temp\Test_Script.log" -LineValue "This is a new line which I am appending to the end of the log file."
  109. #>
  110.  
  111. [CmdletBinding()]
  112.  
  113. Param ([Parameter(Mandatory=$true)][string]$LogPath, [Parameter(Mandatory=$true)][string]$LineValue)
  114.  
  115. Process{
  116. Add-Content -Path $LogPath -Value $LineValue
  117.  
  118. #Write to screen for debug mode
  119. Write-Debug $LineValue
  120. }
  121. }
  122.  
  123. Function Log-Error{
  124. <#
  125. .SYNOPSIS
  126. Writes an error to a log file
  127.  
  128. .DESCRIPTION
  129. Writes the passed error to a new line at the end of the specified log file
  130.  
  131. .PARAMETER LogPath
  132. Mandatory. Full path of the log file you want to write to. Example: C:\Windows\Temp\Test_Script.log
  133.  
  134. .PARAMETER ErrorDesc
  135. Mandatory. The description of the error you want to pass (use $_.Exception)
  136.  
  137. .PARAMETER ExitGracefully
  138. Mandatory. Boolean. If set to True, runs Log-Finish and then exits script
  139.  
  140. .INPUTS
  141. Parameters above
  142.  
  143. .OUTPUTS
  144. None
  145.  
  146. .NOTES
  147. Version: 1.0
  148. Author: Luca Sturlese
  149. Creation Date: 10/05/12
  150. Purpose/Change: Initial function development
  151.  
  152. Version: 1.1
  153. Author: Luca Sturlese
  154. Creation Date: 19/05/12
  155. Purpose/Change: Added debug mode support. Added -ExitGracefully parameter functionality
  156.  
  157. .EXAMPLE
  158. Log-Error -LogPath "C:\Windows\Temp\Test_Script.log" -ErrorDesc $_.Exception -ExitGracefully $True
  159. #>
  160.  
  161. [CmdletBinding()]
  162.  
  163. Param ([Parameter(Mandatory=$true)][string]$LogPath, [Parameter(Mandatory=$true)][string]$ErrorDesc, [Parameter(Mandatory=$true)][boolean]$ExitGracefully)
  164.  
  165. Process{
  166. Add-Content -Path $LogPath -Value "Error: An error has occurred [$ErrorDesc]."
  167.  
  168. #Write to screen for debug mode
  169. Write-Debug "Error: An error has occurred [$ErrorDesc]."
  170.  
  171. #If $ExitGracefully = True then run Log-Finish and exit script
  172. If ($ExitGracefully -eq $True){
  173. Log-Finish -LogPath $LogPath
  174. Break
  175. }
  176. }
  177. }
  178.  
  179. Function Log-Finish{
  180. <#
  181. .SYNOPSIS
  182. Write closing logging data & exit
  183.  
  184. .DESCRIPTION
  185. Writes finishing logging data to specified log and then exits the calling script
  186.  
  187. .PARAMETER LogPath
  188. Mandatory. Full path of the log file you want to write finishing data to. Example: C:\Windows\Temp\Test_Script.log
  189.  
  190. .PARAMETER NoExit
  191. Optional. If this is set to True, then the function will not exit the calling script, so that further execution can occur
  192.  
  193. .INPUTS
  194. Parameters above
  195.  
  196. .OUTPUTS
  197. None
  198.  
  199. .NOTES
  200. Version: 1.0
  201. Author: Luca Sturlese
  202. Creation Date: 10/05/12
  203. Purpose/Change: Initial function development
  204.  
  205. Version: 1.1
  206. Author: Luca Sturlese
  207. Creation Date: 19/05/12
  208. Purpose/Change: Added debug mode support
  209.  
  210. Version: 1.2
  211. Author: Luca Sturlese
  212. Creation Date: 01/08/12
  213. Purpose/Change: Added option to not exit calling script if required (via optional parameter)
  214.  
  215. .EXAMPLE
  216. Log-Finish -LogPath "C:\Windows\Temp\Test_Script.log"
  217.  
  218. .EXAMPLE
  219. Log-Finish -LogPath "C:\Windows\Temp\Test_Script.log" -NoExit $True
  220. #>
  221.  
  222. [CmdletBinding()]
  223.  
  224. Param ([Parameter(Mandatory=$true)][string]$LogPath, [Parameter(Mandatory=$false)][string]$NoExit)
  225.  
  226. Process{
  227. Add-Content -Path $LogPath -Value ""
  228. Add-Content -Path $LogPath -Value "***************************************************************************************************"
  229. Add-Content -Path $LogPath -Value "Finished processing at [$([DateTime]::Now)]."
  230. Add-Content -Path $LogPath -Value "***************************************************************************************************"
  231.  
  232. #Write to screen for debug mode
  233. Write-Debug ""
  234. Write-Debug "***************************************************************************************************"
  235. Write-Debug "Finished processing at [$([DateTime]::Now)]."
  236. Write-Debug "***************************************************************************************************"
  237.  
  238. #Exit calling script if NoExit has not been specified or is set to False
  239. If(!($NoExit) -or ($NoExit -eq $False)){
  240. Exit
  241. }
  242. }
  243. }
  244.  
  245. Function Log-Email{
  246. <#
  247. .SYNOPSIS
  248. Emails log file to list of recipients
  249.  
  250. .DESCRIPTION
  251. Emails the contents of the specified log file to a list of recipients
  252.  
  253. .PARAMETER LogPath
  254. Mandatory. Full path of the log file you want to email. Example: C:\Windows\Temp\Test_Script.log
  255.  
  256. .PARAMETER EmailFrom
  257. Mandatory. The email addresses of who you want to send the email from. Example: "admin@9to5IT.com"
  258.  
  259. .PARAMETER EmailTo
  260. Mandatory. The email addresses of where to send the email to. Seperate multiple emails by ",". Example: "admin@9to5IT.com, test@test.com"
  261.  
  262. .PARAMETER EmailSubject
  263. Mandatory. The subject of the email you want to send. Example: "Cool Script - [" + (Get-Date).ToShortDateString() + "]"
  264.  
  265. .INPUTS
  266. Parameters above
  267.  
  268. .OUTPUTS
  269. Email sent to the list of addresses specified
  270.  
  271. .NOTES
  272. Version: 1.0
  273. Author: Luca Sturlese
  274. Creation Date: 05.10.12
  275. Purpose/Change: Initial function development
  276.  
  277. .EXAMPLE
  278. Log-Email -LogPath "C:\Windows\Temp\Test_Script.log" -EmailFrom "admin@9to5IT.com" -EmailTo "admin@9to5IT.com, test@test.com" -EmailSubject "Cool Script - [" + (Get-Date).ToShortDateString() + "]"
  279. #>
  280.  
  281. [CmdletBinding()]
  282.  
  283. Param ([Parameter(Mandatory=$true)][string]$LogPath, [Parameter(Mandatory=$true)][string]$EmailFrom, [Parameter(Mandatory=$true)][string]$EmailTo, [Parameter(Mandatory=$true)][string]$EmailSubject)
  284.  
  285. Process{
  286. Try{
  287. $sBody = (Get-Content $LogPath | out-string)
  288.  
  289. #Create SMTP object and send email
  290. $sSmtpServer = "smtp.yourserver"
  291. $oSmtp = new-object Net.Mail.SmtpClient($sSmtpServer)
  292. $oSmtp.Send($EmailFrom, $EmailTo, $EmailSubject, $sBody)
  293. Exit 0
  294. }
  295.  
  296. Catch{
  297. Exit 1
  298. }
  299. }
  300. }
Add Comment
Please, Sign In to add comment