Skip to content

Instantly share code, notes, and snippets.

@pbrumblay
Last active September 23, 2020 16:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pbrumblay/6551936 to your computer and use it in GitHub Desktop.
Save pbrumblay/6551936 to your computer and use it in GitHub Desktop.
Powershell FTPS Upload Example
param (
[string]$file = $(throw "-file is required"),
[string]$ftphostpath = $(throw "-ftphostpath is required"),
[string]$username = $(throw "-username is required"),
[string]$password = $(throw "-password is required")
)
$f = dir $file
$req = [System.Net.FtpWebRequest]::Create("ftp://$ftphostpath/" + $f.Name);
$req.Credentials = New-Object System.Net.NetworkCredential($username, $password);
$req.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile;
$req.EnableSsl = $True;
$req.UseBinary = $True
$req.UsePassive = $True
$req.KeepAlive = $True
$req.ConnectionGroupName = "FTPS$username";
$fs = new-object IO.FileStream $f.FullName, 'Open', 'Read'
$ftpStream = $req.GetRequestStream();
$req.ContentLength = $f.Length;
try {
$b = new-object Byte[](10000)
while($true) {
$r = $fs.Read($b, 0, 10000);
if($r -eq 0) { break; }
$ftpStream.Write($b, 0, $r);
}
} finally {
if ($fs -ne $null) { $fs.Dispose(); }
$ftpStream.Close();
$resp = $req.GetResponse();
$resp.StatusDescription;
$resp.Close();
}
@ukreddy-erwin
Copy link

Hi, I need to upload complete folder(only folder contents) to the ftps.
CAn you suggest

@pbrumblay
Copy link
Author

Holy cow! I wrote this 7 years ago. Let's see... assuming it still works (I have moved on from .NET / Powershell to other technologies) you could take a folder name as a parameter on line 2 instead of a single file and then loop over it (something like this: https://stackoverflow.com/a/18848848) to upload each file to the FTPS server. I hope that helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment