Advertisement
nilrem2

PowerShell Module for Logging Console Output

Apr 8th, 2020
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Log-Info
  2. {
  3.     [CmdletBinding()]
  4.     Param
  5.     (
  6.         [parameter(Mandatory=$true, Position=0)]
  7.         $logInfo
  8.     )
  9.    
  10.     Write-Host ("{1} - {0} - {2}" -f (Get-Date), "INFO", $logInfo)
  11.    
  12. }
  13.  
  14. function Log-Error
  15. {
  16.     [CmdletBinding()]
  17.     Param
  18.     (
  19.         [parameter(Mandatory=$true, Position=0)]
  20.         $logError
  21.     )
  22.  
  23.     Write-Host ("{1} - {0} - {2}" -f (Get-Date), "ERROR", $logError)
  24. }
  25.  
  26. function Log-Warning
  27. {
  28.     [CmdletBinding()]
  29.     Param
  30.     (
  31.         [parameter(Mandatory=$true, Position=0)]
  32.         $logError
  33.     )
  34.  
  35.     Write-Host ("{1} - {0} - {2}" -f (Get-Date), "WARNING", $logError)    
  36. }
  37.  
  38. [string] $logFile
  39. function Get-LogFile
  40. {
  41.     return $Script:logFile
  42. }
  43.  
  44. function Start-Logging
  45. {
  46.     Param(
  47.         [Parameter(Mandatory=$false)][string] $logFile
  48.     )
  49.  
  50.     #Start logging the transcript to a file
  51.     try
  52.     {
  53.         Stop-Transcript | Out-Null
  54.     }
  55.     catch
  56.     {
  57.     }
  58.     $ErrorActionPreference = "Continue"
  59.     $Script:logFile = $Local:logFile
  60.     Start-Transcript -path ($logFile)
  61. }
  62.  
  63. function Stop-Logging
  64. {
  65.     Stop-Transcript
  66. }
  67.  
  68. #Declare which functions to export/make available
  69. Export-ModuleMember -Function 'Get-LogFile'
  70. Export-ModuleMember -Function 'Log-Info'
  71. Export-ModuleMember -Function 'Log-Warning'
  72. Export-ModuleMember -Function 'Log-Error'
  73. Export-ModuleMember -Function 'Start-Logging'
  74. Export-ModuleMember -Function 'Stop-Logging'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement