A simple powershell function

0 Flares Filament.io 0 Flares ×

Today, I’d like to share a very simple function I wrote some years ago, I would even say many years :). This is a simple powershell function, without any error handling and parameters checking. I needed at some point to have proper logging of my scripts and I wanted to have it displayed on screen and logged to the file as well for some later usage. I wrote this function to achieve my need.

Function takes 4 input parameters :

  • $logFile – the name of the file where to write message
  • $message – message which should be written to the log file and eventually displayed on screen
  • $messageType – type of the message from 0 to 3 – 0 means information will be displayed white font color on screen, 1 is warning message and it will be displayed with yellow color on screen, 2 is an error message type so it will be displayed in red and number 3 is successful message displayed in green.
  • $displayOnScreen=$true – this parameter I’ve added when I needed to log some information twice into 2 different files, so to avoid duplicate displays on screen I’ve added this parameter
    The output on the screen from the function looks like that

And in the file

If it’s useful for you, feel free to use it.

0 Flares LinkedIn 0 Google+ 0 Facebook 0 Twitter 0 Filament.io 0 Flares ×

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.