Advertisement
stephanlinke

[PowerShell] Logging Function

Aug 12th, 2016
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2.  #  PowerShell Console Logging function
  3.  #  [parameters]
  4.  #  @type    {string} defines the type of the message. Can be success,information, warning, error or empty.
  5.  #  @message {string} your log message
  6.  #  [notes]
  7.  #  Make sure that $verbose is set to $true, otherwise, it won't output anything.
  8.  #  [examples]
  9.  # Console-ShowMessage "information" "Operation started"
  10.  # Console-ShowMessage "success" "Operation finished succesful!"
  11.  # Console-ShowMessage "warning" "Problems have been found..."
  12.  # Console-ShowMessage "error" "Operation has failed!"
  13.  # Console-ShowMessage -message "I'm just a simple log message"
  14.  #>
  15.  
  16. function Console-ShowMessage([string]$type,$message){
  17.     if($verbose){
  18.         Write-Host ("[{0}] " -f (Get-Date)) -NoNewline;
  19.         switch ($type){
  20.             "success"       { Write-Host "    success    "  -BackgroundColor Green      -ForegroundColor White -NoNewline; }
  21.             "information"   { Write-Host "  information  "  -BackgroundColor DarkCyan   -ForegroundColor White -NoNewline; }
  22.             "warning"       { Write-Host "    warning    "  -BackgroundColor DarkYellow -ForegroundColor White -NoNewline; }
  23.             "error"         { Write-Host "     error     "  -BackgroundColor DarkRed    -ForegroundColor White -NoNewline; }
  24.             default         { Write-Host "     notes     "  -BackgroundColor DarkGray   -ForegroundColor White -NoNewline; }
  25.         }
  26.         Write-Host (" {0}{1}" -f $message,$Global:blank)
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement