FtpHandling/Send-FtpFile.ps1

function Send-FtpFile {
    Param(
        [Parameter(Mandatory=$true)]
        [string] $ftpServer,
        [Parameter(Mandatory=$true)]
        [string] $ftpUser,
        [Parameter(Mandatory=$true)]
        [SecureString] $ftpPassword,
        [Parameter(Mandatory=$true)]
        [string] $sourcePath,
        [Parameter(Mandatory=$false)]
        [string] $sourcePathExclude = "",
        [Parameter(Mandatory=$true)]
        [string] $remotePath,
        [Parameter(Mandatory=$false)]
        [string] $branchName,
        [Parameter(Mandatory=$false)]
        [switch] $cleanFolder
        )
    
    $SkipProcessing = $false

    if ($null -ne $branchName) {
        if ($branchName -match '/$') {
            $branchName = $branchName -replace ".$"
        }
        if ($remotePath -notmatch '/$') {
            $remotePath += '/';
        }
        if ($branchName -contains '*/*') {
            $remotePath += $branchName.Split('-')[-1]
        } 
        else {
            $remotePath += $branchName
        }
    }

    if ($cleanFolder.IsPresent) {
        $ResponseStatus = Set-FTPConnection -Credentials (New-Object System.Management.Automation.PSCredential($ftpUser,$ftpPassword)) -Server $ftpServer -Session CleanupSession -UsePassive -EnableSsl -KeepAlive -UseBinary -ignoreCert
        if (($ResponseStatus | Select-Object -ExpandProperty WelcomeMessage) -like "230 User logged in.*") {
            $session = Get-FTPConnection -Session CleanupSession

            try {
                Remove-FTPItem -Session $session -Path $remotePath -Recurse
            } catch { }

            if (!(Get-FTPItem -Path (Split-Path $remotePath -Parent) -ErrorAction SilentlyContinue)) {
                $SkipProcessing = $true
                Write-Host "Skipping upload"
            } else {
                New-FTPItem -Session $session -Name $remotePath
            }
        }
        else {
            throw "Error connecting to FTP"
        }
    } 
    else {
        if (!(Get-FTPItem -Path $remotePath -ErrorAction SilentlyContinue)) {
            $SkipProcessing = $true
            Write-Host "Skipping upload"
        }
    }

    if (!$SkipProcessing) {
        $ResponseStatus = Set-FTPConnection -Credentials (New-Object System.Management.Automation.PSCredential($ftpUser, $ftpPassword)) -Server $ftpServer -Session UploadSession -UsePassive -EnableSsl -KeepAlive -UseBinary -ignoreCert
        if (($ResponseStatus | Select-Object -ExpandProperty WelcomeMessage) -like "230 User logged in.*") {
            $session = Get-FTPConnection -Session UploadSession

            if ($sourcePathExclude -eq "") {
                $files = (Get-ChildItem -Path $sourcePath)
            }
            else {
                $files = (Get-ChildItem -Path $sourcePath -Exclude $sourcePathExclude)
            }
            $files | ForEach-Object {
                Add-FTPItem -Session $session -Path $remotePath -LocalPath $_.FullName -Overwrite
            }
        }
    }
}
Export-ModuleMember Send-FtpFile