PowerShell: Synchronizing a Folder (and Sub-Folders) Part 10

Hi.  In this part I add some text logging to the script.  Not much else I can say really 🙂

The new updated script is here.

All the logging is done with a single new function that’s added as below;

function Write-Log {
    [CmdletBinding()]
    param
    (
        [Parameter(
            ValueFromPipeline=$true)]
        [String]$Output="",
        [switch]$IsError=$False,  
        [switch]$IsWarning=$False,
        [switch]$Heading=$False,
        [switch]$Emphasis=$False,
        [switch]$WriteHost=$False,
        [switch]$NoFileWrite=$False
    )
    BEGIN {
        $TitleChar="*"
    }
    PROCESS {        
        $FormattedOutput=@()
        if ($Heading) {
            $TitleBar=""
            #Builds a line for use in a banner
            for ($i=0;$i -lt ($Output.Length)+2; $i++) {
                $TitleBar+=$TitleChar
            }
            $FormattedOutput=@($TitleBar,"$TitleChar$Output$TitleChar",$TitleBar,"")
        }elseif ($Emphasis) {
            $FormattedOutput+="","$TitleChar$Output$TitleChar",""
        }else {
            $FormattedOutput+=$Output
        }
        if ($IsError) {
            $PreviousFunction=(Get-PSCallStack)[1]
            $FormattedOutput+="Calling Function: $($PreviousFunction.Command) at line $($PreviousFunction.ScriptLineNumber)"
            $FormattedOutput=@($FormattedOutput | ForEach-Object {(Get-Date -Format HH:mm:ss.fff)+" : ERROR " + $_})
            $FormattedOutput | Write-Error
        }elseif ($IsWarning) {
            $FormattedOutput=@($FormattedOutput | ForEach-Object {(Get-Date -Format HH:mm:ss.fff)+" : WARNING " + $_})
            $FormattedOutput | Write-Warning            
        }else {
            $FormattedOutput=$FormattedOutput | ForEach-Object {(Get-Date -Format HH:mm:ss.fff)+" : " + $_}
            if ($WriteHost) {
                $FormattedOutput | Write-Host
            }else {

                $FormattedOutput | Write-Verbose
            }
        }
        if (!$NoFileWrite) {
            if (($Script:LogFileName -ne $Null) -and ($Script:LogFileName -ne "")) {
                $FormattedOutput | Out-File -Append $Script:LogFileName
            }  

        }
    }
    END {
    }
}

Normally the function just writes to a text file (the filename for that is held in the script-wide variable, $Script:LogFileName).  The text to be written has a timestamp appended to the front of it and it can take streamed or multi-line input and deal with it correctly.

There are some optional switches though that change the default behaviour.

-IsError and -IsWarning write the output to the Error and Warning streams and also record the function that generated the error or warning.

-Heading and -Emphasis allow some formatting to be done to the output to make certain parts stand out.

If -WriteHost is specified the output is written to the host instead of to the default Verbose stream.

And if you just want to write to the streams or host and not the file, -NoFileWrite can be specified.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: