Skip to content

Instantly share code, notes, and snippets.

@arturosalgado
Forked from pbrumblay/ftpsupload.ps1
Created September 15, 2020 10:08
Show Gist options
  • Save arturosalgado/9f8144baefc60535da0af3265d365036 to your computer and use it in GitHub Desktop.
Save arturosalgado/9f8144baefc60535da0af3265d365036 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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment