-
-
Save arturosalgado/9f8144baefc60535da0af3265d365036 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(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment