I developed the following Powershell function to help me synchronize code on my new servers:
function SynchronizeFolder([string]$source, [string]$destination, $recurse, $exclusions)
{
if($source.tostring().endswith("\") -ne $true){$source = "$source\"}
if($destination.tostring().endswith("\") -ne $true){$destination = "$destination\"}
# Variables
$child_items
$destination_file
$destination_folder
$result
# Folder exists?
if((test-path $destination) -eq $false){$result = md $destination}
# Build child items
if($recurse)
{
$child_items = get-childitem $source -recurse
}
else
{
$child_items = get-childitem $source
}
foreach($child_item in $child_items)
{
$destination_file = $destination + $child_item.fullname.substring((get-item $source).fullname.length)
$destination_folder = $destination_file
if($child_item.psiscontainer -eq $false){$destination_folder = $destination_folder.substring(0, $destination_folder.lastindexof("\")) + "\"}
if($child_item.psiscontainer -eq $true)
{
#folder
if((test-path $destination_folder) -eq $false)
{
$result = md $destination_folder
echo $destination_folder
}
}
else
{
#file
if((test-path $destination_file) -eq $true)
{
#exists
if($child_item.lastwritetime -ne (get-item $destination_file).lastwritetime -or $child_item.length -ne (get-item $destination_file).length)
{
if($child_item.fullname -match $exclusions -eq $false)
{
copy $child_item.fullname $destination_folder
echo $destination_file
}
}
}
else
{
#new
if($child_item.fullname -match $exclusions -eq $false)
{
copy $child_item.fullname $destination_folder
echo $destination_file
}
}
}
}
}
0 comments :: Poor Man's File Synchronization using PowerShell
Post a Comment