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

    try {
        $files = Get-ChildItem $sourcePath
        if ($null -eq $files) {
            return
        }
    }
    catch {
        return
    }
    
    $pathToCheck = $remotePath

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

    $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
    }
    else {
        throw "Error connecting to FTP"
    }
    
    $oldConfirmPreference = $ConfirmPreference
    $ConfirmPreference = "high"

    if ($cleanFolder.IsPresent) {
        try {
            Remove-FTPItem -Session $session -Path $remotePath -Recurse
        } catch { }

        if ($pathToCheck -ne $remotePath) {
            try {
                Get-FTPChildItem -Session $session -Path $pathToCheck
            }
            catch {
                New-FTPItem -Session $session -Path (Split-Path $pathToCheck -Parent) -Name (Split-Path $pathToCheck -Leaf) -ErrorAction SilentlyContinue
            }
        }

        try {
            Get-FTPChildItem -Session $session -Path $remotePath
        }
        catch {
            New-FTPItem -Session $session -Path (Split-Path $remotePath -Parent) -Name (Split-Path $remotePath -Leaf)
        }
    }
    else {
        try {
            Get-FTPChildItem -Session $session -Path $remotePath
        }
        catch {
            $SkipProcessing = true
            Write-Host "Skipping upload; $remotePath doesn't exist"
        }
    }

    if (!$SkipProcessing) {
        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
        }
    }

    $ConfirmPreference = $oldConfirmPreference
}
Export-ModuleMember Send-FtpFile