PowerShell Template script#2: With detailed logging functions, with color output messages to console and Paramaters values assignments

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 = $LogFile,
    [bool]$DEBUG
)

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

#===============================================================================
# Global Variables TEMP
#===============================================================================
$Global:LogFile = ""

#===============================================================================
# function  Initialize-Globals
#===============================================================================
function  Initialize-Globals {

    $LogPath        = $env:TEMP
    $ScriptName        = "Template-Script.log"
    $Global:LogFile = $LogPath + ” + $ScriptName
    #Debug "Init $LogFile"
}

#===============================================================================
# function Process-Arguments
#===============================================================================
function Process-Arguments {
    $pLogFile = $($LOG)
    if ($pLogFile -ne "") {
        #Debug "Log argument $pLogFile is not NULL"
        $Global:LogFile = $pLogFile
    }
}

#===============================================================================
# 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 YELLOW -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) {
        #Debug "LogFile received is $LogFile"
        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()
#===============================================================================

Initialize-Globals
Process-Arguments
Do-Action
exit 0

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

 

Script Output:

PS C:> Powershell C:TempTemplate2-PSScript-Paramaters.ps1
10:03:2013-20:55:12 INFO: This is information
10:03:2013-20:55:12 WARNING: This is warning
10:03:2013-20:55:12 ERROR: This is an error
10:03:2013-20:55:12 ERROR: Encountered an error, exiting
PS C:> Powershell C:TempTemplate2-PSScript-Paramaters.ps1 -D
C:TempTemplate2-PSScript-Paramaters.ps1 : Missing an argument for parameter ‘DEBUG’. Specify a parameter of type
‘System.Boolean’ and try again.
At line:1 char:43
+ C:TempTemplate2-PSScript-Paramaters.ps1 -D
+                                           ~~
    + CategoryInfo          : InvalidArgument: (:) [Template2-PSScript-Paramaters.ps1], ParameterBindingException
    + FullyQualifiedErrorId :
MissingArgument,Template2-PSScript-Paramaters.ps1

PS C:> Powershell C:TempTemplate2-PSScript-Paramaters.ps1 -D 1
10:03:2013-20:55:29 INFO: This is information
10:03:2013-20:55:29 WARNING: This is warning
10:03:2013-20:55:29 ERROR: This is an error
10:03:2013-20:55:29 DEBUG: This is a debug message
10:03:2013-20:55:29 ERROR: Encountered an error, exiting
PS C:> Powershell C:TempTemplate2-PSScript-Paramaters.ps1 -D 0
10:03:2013-20:55:32 INFO: This is information
10:03:2013-20:55:32 WARNING: This is warning
10:03:2013-20:55:32 ERROR: This is an error
10:03:2013-20:55:32 ERROR: Encountered an error, exiting
PS C:> Powershell C:TempTemplate2-PSScript-Paramaters.ps1 -D 1 -LOG
C:TempTemplate2-PSScript-Paramaters.ps1 : Missing an argument for parameter ‘LOG’. Specify a parameter of type
‘System.String’ and try again.
At line:1 char:48
+ C:TempTemplate2-PSScript-Paramaters.ps1 -D 1 -LOG
+                                                ~~~~
    + CategoryInfo          : InvalidArgument: (:) [Template2-PSScript-Paramaters.ps1], ParameterBindingException
    + FullyQualifiedErrorId : MissingArgument,Template2-PSScript-Paramaters.ps1

PS C:> Powershell C:TempTemplate2-PSScript-Paramaters.ps1 -D 1 -LOG C:temptest-log.log
10:03:2013-20:56:34 INFO: This is information
10:03:2013-20:56:34 WARNING: This is warning
10:03:2013-20:56:34 ERROR: This is an error
10:03:2013-20:56:34 DEBUG: This is a debug message
10:03:2013-20:56:34 ERROR: Encountered an error, exiting
PS C:> type C:temptest-log.log
10:03:2013-20:56:34 INFO: This is information
10:03:2013-20:56:34 WARNING: This is warning
10:03:2013-20:56:34 ERROR: This is an error
10:03:2013-20:56:34 DEBUG: This is a debug message
10:03:2013-20:56:34 ERROR: Encountered an error, exiting
PS C:>

image

 

Script Source for Download:

Leave a Reply

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