<# FPS:Containers:Make:Public:Push-Consolidation:ACTIVE #>

 

function Push-Consolidation {

 

 

<#

 

.DESCRIPTION

Module: Make

Public CmdLet: Push-Consolidation.ps1

Dependencies: Get-Folder.ps1, Update-Log.ps1, Remove-DirTree.ps1, Move-SingleFile.ps1, (Get-Answer.ps1)

 

This function pushes all files that don't exist in the target directory to make a consolodated directory.  If files do exist, the

function will only replace the file if the source file has a later update date.

 

.EXAMPLE

if (Push-NeededFor Push-Consolidation) { . (Get-ActiveFileNm -PS1 Push-Consolidation) }

$pConsolidate = $false

$pSource = "D:\FPS Archive\FPS-Archive\FPS"

$pTarget = "D:\FPS Archive\FPS-Combined"

 

if ($pConsolidate) {

    Push-Consolidation -Source $pSource -Target $pTarget -Consolidate

} else {

    Push-Consolidation -Source $pSource -Target $pTarget

}

 

.CC

 

[2024.11.06 JD] Changed the log file to be the global default

[2024.10.10 JD] Original code release

 

.NOTES

 

 

#>

 

    [CmdletBinding()]

    param(

    [Parameter(Mandatory=$false)] [string]$Source,

    [Parameter(Mandatory=$false)] [string]$Target,

    [Parameter(Mandatory=$false)] [switch]$Consolidate

    )

 

    #region Initialization

 

    $testCmdLet, $overrideTestSnippet, $overrideSnippet = "", "", "" # Initialize override variables

    . (Get-ActiveFileNm -PS1 Initialize-Make-Locations)

    . (Get-ActiveFileNm -PS1 Load-Make-Dependencies)

 

    #endregion

   

    $sfpBuffer = "Info*Process starting ... copy $Source to $Target."

 

    #Organize the directories.

 

    if ($Source -gt "") { $SourceContainer = $Source } else { $SourceContainer = Get-Answer "Enter the source location: " }

    if ($Target -gt "") { $TargetContainer = $Target } else { $TargetContainer = Get-Answer "Enter the target location: " }

 

    # First, Check for new folders that will be necessary in the target

    $srcFiles = Get-Folder $SourceContainer

    $srcPaths = Get-ChildItem -Path $SourceContainer -Recurse -Directory -Force -ErrorAction SilentlyContinue | Select-Object FullName

 

        foreach ($srcPath in $srcPaths) {

 

        $tgtPath = $srcPath.FullName.Replace($SourceContainer,$TargetContainer)

        if (-not (Test-Path $tgtPath)) {

 

            # If Consolidate has been specified, create the directory - otherwise just print it out.

            if ($PSBoundParameters.Consolidate -eq $true) {  mkdir $tgtPath | Out-Null }

 

            $sfpBuffer += "|Info*Missing Directory on target created - $tgtPath"

 

        }

 

    }

 

        $tgtFileSet = $tgtFiles | Select LastWriteTime, FullName, Name

    $srcFileSet = $srcFiles | Select LastWriteTime, FullName, Name

 

    # Dates, when matched, have to both be converted to UTC so they are in alignment with their timezone.

    function local:getLastWriteTimeUtc([string]$path) {

        try {

            Get-Date -Date (Get-Item $path).LastWriteTimeUtc.ToString('yyyy-MM-dd HH:mm:ss')

        } catch { }

    }

 

    # Now that the directories are sync'd, make sure the files are sync'd.  Check for missing and updated files (timestamp mismatch)

    foreach ($srcFile in $srcFileSet) {

        $tgtFile = Get-ChildItem -Path ($srcFile.Fullname.replace($Source,$Target)) -Force -ErrorAction SilentlyContinue | Select-Object LastWriteTime, FullName, Name

 

        if ($tgtFile.FullName -gt "") {

 

            $utcSrc = getLastWriteTimeUtc $srcFile.FullName

            $utcTgt = getLastWriteTimeUtc $tgtFile.FullName

 

            if ($utcSrc -ne $utcTgt) {

 

                [string]$sfpBufferCast = "Source/Target timestamp mismatch for "+$tgtFile.FullName+" - "+$utcSrc.toString()+" --- "+$utcTgt.toString()

                $sfpBuffer += "|Info*"+$sfpBufferCast

 

                if ($PSBoundParameters.Sync -eq $true) { Move-SingleFile -SourceFile $srcFile.FullName -TargetFile $tgtFile.FullName  -KeepSource 'YES' -KeepTarget 'NO' }

 

            }

 

        } else {

 

            [string]$sfpBufferCast = "Target file missing and will be added - "+$srcFile.FullName

            $sfpBuffer += "|Info*"+$sfpBufferCast

 

            $tgtFile = $srcFile.Fullname.replace($Source,$Target)

            if ($PSBoundParameters.Sync -eq $true) {

                # First make sure the path exists

                $sink = Mount-Path (Split-Path -Path $srcFile.FullName -Parent)

 

                Move-SingleFile -SourceFile $srcFile.FullName -TargetFile $tgtFile -KeepSource 'YES' -KeepTarget 'NO'

 

            }

 

        }

 

    }

 

 

    # Write the completion message

    $sfpBuffer += "|Info*Process complete ... copy $Source to $Target."

    Update-Log -Sev "Buffered" -Module "Make" -Element "Push-Consolidation" -Buffer $sfpBuffer -LogFile $makLoc_Logs -Buffered

 

 

}