Last active
May 26, 2018 19:25
-
-
Save DCAG/306b4db5babd1a62665376baaf5849ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Send-FTP | |
{ | |
<# | |
.SYNOPSIS | |
Upload files to FTP server | |
.DESCRIPTION | |
Upload files to a FTP address, | |
using System.Net.WebClient to connect to the FTP server | |
uploading to the given address | |
.PARAMETER Address | |
FTP Address - must start with ftp:// and end with / | |
.PARAMETER Credentials | |
Credentials for FTP | |
.PARAMETER File | |
Array of files to upload to a remote FTP server | |
.EXAMPLE | |
$Credentials = Import-CliXml ".\FTPCreds.xml" | |
Send-FTP -Address "ftp://FTP.granola.tech/" -Credentials $Credentials -File "C:\ToUpload\Note.txt" | |
# Would upload the file to ftp://FTP.granola.tech/Note.txt | |
.NOTES | |
Written by Amir Granot. | |
Inspired by: https://gallery.technet.microsoft.com/scriptcenter/80647f66-139c-40a4-bb7a-04a2d73d423c | |
#> | |
[CmdletBinding(SupportsShouldProcess)] | |
param( | |
[Parameter(Mandatory)] | |
[ValidatePattern("ftp://.*/")] | |
[string]$Address, | |
[Parameter(Mandatory)] | |
[pscredential]$Credentials, | |
[string[]]$File | |
) | |
Begin | |
{ | |
$WebClient = New-Object System.Net.WebClient | |
$WebClient.Credentials = [System.Net.NetworkCredential]::new($Credentials.UserName, $Credentials.Password) | |
} | |
Process | |
{ | |
Get-ChildItem $File | ForEach-Object { | |
$RemoteFileName = '{0}{1}' -f $Address, $_.Name | |
$Uri = [System.Uri]::($RemoteFileName) | |
if ($pscmdlet.ShouldProcess($_.FullName, "Upload to $RemoteFileName")) | |
{ | |
$Result = $WebClient.UploadData($Uri, [System.IO.File]::ReadAllBytes($_)) | |
"File {0} was uploaded. Result = {1}" -f $_.FullName, [Text.Encoding]::UTF8.GetBytes($Result) | Write-Verbose | |
} | |
} | |
} | |
End | |
{ | |
$WebClient.Dispose() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment