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)] [switch] $cleanFolder ) 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 Remove-FTPItem -Session $session -Path $remotePath -Recurse New-FTPItem -Session $session -Name $remotePath } else { throw "Error connecting to FTP" } } $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 |