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 $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" } if ($cleanFolder.IsPresent) { try { Remove-FTPItem -Session $session -Path $remotePath -Recurse } catch { } if ($pathToCheck -ne $remotePath) { if (!(Get-FTPItem -Session $session -Path $pathToCheck -ErrorAction SilentlyContinue)) { New-FTPItem -Session $session -Name $pathToCheck } } New-FTPItem -Session $session -Name $remotePath } else { if (!(Get-FTPItem -Session $session -Path $remotePath -ErrorAction SilentlyContinue)) { $SkipProcessing = $true Write-Host "Skipping upload" } } 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 } } } Export-ModuleMember Send-FtpFile |