PowerShell Template script with detailed logging functions along with color output messages to console

Script:

#
# PowerShell Script
#===============================================================================
# Objective:
# ———-
#  This is a basic template script that comes with default script sections along
#  with debug options set.
#
#
# $Header: $

#===============================================================================
# Process-Arguments
#===============================================================================
param (
    [string]$LOG,
    [bool]$DEBUG
)

#===============================================================================
# Include Modules
#===============================================================================

#===============================================================================
# Global Variables Declaration
#===============================================================================
$LogPath    = $env:TEMP
$ScriptName = "Template-Script.log"
$LogFile    = $LogPath + ” + $ScriptName

#===============================================================================
# Function Prototypes Section
#===============================================================================
function MyLog {
    param (
    [string]$msg,
    [int]$flag
    )
    $date = get-date -format MM:dd:yyyy-HH:mm:ss
    $str  = "$date "
    switch ($flag) {
        0 { $str += "INFO: $msg"; $cmd = ‘write-host $str -ForegroundColor BLUE -BackgroundColor WHITE’ }
        1 { $str += "WARNING: $msg"; $cmd = ‘write-host $str -ForegroundColor CYAN -BackgroundColor WHITE’ }
        2 { $str += "ERROR: $msg"; $cmd = ‘write-host $str -ForegroundColor RED -BackgroundColor WHITE’ }
        3 { $str += "DEBUG: $msg"; $cmd = ‘write-host $str -ForegroundColor DARKGREEN -BackgroundColor WHITE’ }
        #default { $cmd = ‘write-host $date INFO: $msg -ForegroundColor BLUE -BackgroundColor WHITE’ }
    }
    #Write-Host "Command is: $cmd"
    invoke-expression -command "$cmd"

    if ($LOG) {
        Write-Output "$str" | Out-File $LogFile -append
    }
   
}

#===============================================================================
# Function Prototypes Section
#===============================================================================
function Info   {param ([string]$mesg); MyLog $mesg 0 }
function MyWarn {param ([string]$mesg); MyLog $mesg 1 }
function MyErr  {param ([string]$mesg); MyLog $mesg 2 }
function MyDie  {param ([string]$mesg); MyLog $mesg 2; exit(1)}
function Debug  {param ([string]$mesg); if ($DEBUG) { MyLog $mesg 3 } }

#===============================================================================
# function Do-Action
#===============================================================================
function Do-Action {

    #cls
    Info "This is information"
    MyWarn "This is warning"
    MyErr "This is an error"
    Debug "This is a debug message"
    MyDie "Encountered an error, exiting"

}

#===============================================================================
# main()
#===============================================================================

Do-Action
exit 0

#===============================================================================
# documentation
#===============================================================================

 

Script Output:

PS C:> powershell C:TempTemplate-With-Logging-Colors.ps1
10:02:2013-20:27:43 INFO: This is information
10:02:2013-20:27:43 WARNING: This is warning
10:02:2013-20:27:43 ERROR: This is an error
10:02:2013-20:27:43 ERROR: Encountered an error, exiting
PS C:> powershell C:TempTemplate-With-Logging-Colors.ps1 -DEBUG 1
10:02:2013-20:27:46 INFO: This is information
10:02:2013-20:27:46 WARNING: This is warning
10:02:2013-20:27:46 ERROR: This is an error
10:02:2013-20:27:46 DEBUG: This is a debug message
10:02:2013-20:27:46 ERROR: Encountered an error, exiting

PS C:> dir "$env:tempTemplate-Script.log"
dir : Cannot find path ‘C:UsersgunnalahAppDataLocalTempTemplate-Script.log’ because it does not exist.
At line:1 char:1
+ dir "$env:tempTemplate-Script.log"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:Usersgunnal…late-Script.log:String) [Get-ChildItem], ItemNotFound
   Exception
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

PS C:> powershell C:TempTemplate-With-Logging-Colors.ps1 -DEBUG 1 LOG
10:02:2013-20:28:29 INFO: This is information
10:02:2013-20:28:29 WARNING: This is warning
10:02:2013-20:28:29 ERROR: This is an error
10:02:2013-20:28:29 DEBUG: This is a debug message
10:02:2013-20:28:29 ERROR: Encountered an error, exiting
PS C:> type "$env:tempTemplate-Script.log"
10:02:2013-20:28:29 INFO: This is information
10:02:2013-20:28:29 WARNING: This is warning
10:02:2013-20:28:29 ERROR: This is an error
10:02:2013-20:28:29 DEBUG: This is a debug message
10:02:2013-20:28:29 ERROR: Encountered an error, exiting
PS C:>

 

 

image

 

Script Source for Download: 

0 thoughts on “PowerShell Template script with detailed logging functions along with color output messages to console

Leave a Reply

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