Last active
September 23, 2020 16:07
-
-
Save pbrumblay/6551936 to your computer and use it in GitHub Desktop.
Powershell FTPS Upload Example
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
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(); | |
} | |
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
Hi, I need to upload complete folder(only folder contents) to the ftps.
CAn you suggest