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

Here’s another quick update of the script with 2 small fixes and a new piece of functionality (the ability to sync to multiple target folders from the same source).The first was adding “-Attributes !ReparsePoint” to the Get-ChildItem commands;

  $SourceList=Get-ChildItem -LiteralPath $SourceFolder -Attributes !ReparsePoint
 if (Test-Path -LiteralPath $TargetFolder)
 {
 $TargetList=Get-ChildItem -LiteralPath $TargetFolder -Attributes !ReparsePoint
 }

This was suggested by Scott on the comments to stop it traversing symbolic links (and causing loops potentially).

The other small change was adding -LiteralPath to the Test-Path commands;

  if (!(Test-Path -LiteralPath $TargetFolder -PathType Container))

This prevents an error being generated if there are any special characters in the path (suggested by Robert in the comments).

Finally, I made some changes to support multiple target folders, both in the XML configuration file and in the passed parameters (requested by Scott).  This pretty much boiled down to changing the parameters of the recursive function to take an array of strings (paths) instead of a single string;

  [string[]]$TargetFolders,

and making most of the body of the function apply to members of that array instead of a plain string;

  Foreach ($TargetFolder in $TargetFolders)
 {
 }

A similar loop was put in for the bit of code that processes passed parameters (rather than a config file);

  Write-Log "Syncing folder pair passed as parameters."
 foreach ($TargetFolder in $TargetFolders)
 {
 $ResultObjects=Sync-OneFolder -SourceFolder $SourceFolder -TargetFolder $TargetFolder -PassedExceptions $Exceptions -Filter $Filter -WhatIf:$WhatIf | 
 Tee-Object -Variable Changes
 }

The script promptly broke because I’d renamed the parameters in the function from “TargetFolder” to “TargetFolders” and I’d already used that variable name.  So I’ve renamed a few internal variables as well.

 

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 )

Twitter picture

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

Facebook photo

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

Connecting to %s

%d bloggers like this: