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

I’ve made some more changes to the syncing script. The first was some corrections about how it deals with paths with ‘odd’ symbols in them (like “[“) and the second was to properly output objects listing all the changes its made (for logging or further processing).

Update : I’ve revisited this script a few times with new additions and modifications.The latest full version of the script is here.  That post also includes links covering the other revisions to the script.

I’ll go over the changes below;

        [parameter(Mandatory=$True)]
        [ValidateScript({Test-Path -LiteralPath $_ -PathType Container})]
        [string]$SourceFolder,
        [parameter(Mandatory=$True)]
        [ValidateScript({Test-Path -LiteralPath $_ -IsValid })]

The validation here has been updated to use -LiteralPath instead of -Path.  This prevents errors when a symbol that can be used for POSIX manipulation of paths (like “[“) is present.

<# .SYNOPSIS Creates an object to be used to report on the success of an action #>
function New-ReportObject
{
    New-Object -typename PSObject| Add-Member NoteProperty "Successful" $False -PassThru |
         Add-Member NoteProperty "Process" "" -PassThru |
         Add-Member NoteProperty "Message" "" -PassThru    
}

This function creates an object with some attributes used to report on the action the script has just performed. So for each copy it outputs an object with the success of that copy and any errors or problems that occurred. These could be output to a report or filtered to get the errors, which can then be fixed.

These objects are used to report throughout.

For example;

    if (!(Test-Path -LiteralPath $TargetFolder -PathType Container))
    {
        $Output=New-ReportObject
        Write-Verbose "Creating Folder : $($TargetFolder)" 
        $Output.Process="Create Folder"
        try
        {
            $Output.Message="Adding folder missing from Target : $TargetFolder"
            Write-Verbose $Output.Message
            New-Item $TargetFolder -ItemType "Directory" > $null
            $Output.Successful=$True
        }
        catch
        {
            $Output.Message="Error adding folder $TargetFolder)"
            Write-Error $Output.Message
            Write-Error $_
        }
        $Output
    }

First a new report object is created with;

$Output=New-ReportObject

The process attribute is then updated with what the script is doing (“Create Folder”), what the outcome was;

$Output.Message="Adding folder missing from Target : $TargetFolder"

and whether the action was successful;

$Output.Successful=$True

When the script runs all of these reporting objects are output to the pipeline and can be piped on to another command or captured into a variable.

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: