FtpHandling/Send-FtpFile.ps1

<#
 .Synopsis
  Sends a file to a FTP Server
 .Description
  Sends a file to a FTP server
 .Parameter ftpServer
  FTP Server
  .Parameter ftpUser
  user to log on to server
  .Parameter ftpPassword
  Password for user
  .Parameter sourcePath
  Path where files should be sent from
  .Parameter sourcePathExclude
  Excludes different files
  .Parameter remotePath
  Path to the remote directory on the ftp server
  .Parameter branchName
  Name of the branch the files are from
  .Parameter cleanFolder
  Add this switch if existing contents in remote directory should be deleted
  .Example
  Send-FtpFile -ftpServer "ftp://ftp.ftp.com" -ftpUser "user" -ftpPassword (ConvertTo-SecureString "P@ssword1" -AsPlainText -Force) -sourcePath "C:\Install" -remotePath "directory" -branchName "abc"
#>

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 { Write-Output "" }

        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-Output "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
}