PowerShell Upload Download FTP over TLS

PowerShell Upload Download FTP over TLS

1./ PowerShel Upload FTP over TLS

[Net.ServicePointManager]::ServerCertificateValidationCallback={$true}

$Dir = "D:\test-ftp.txt"

foreach($item in (dir $dir))

{

 ​​ ​​ ​​​​ write-output "————————————–"

 ​​ ​​ ​​​​ $fileName = $item.FullName

 ​​ ​​ ​​​​ write-output $fileName

 ​​ ​​ ​​​​ $ftp = [System.Net.FtpWebRequest]::Create("ftp://172.16.0.40//YourRemoteSubPath/"+$item.Name)

 ​​ ​​ ​​​​ $ftp = [System.Net.FtpWebRequest]$ftp

 ​​ ​​ ​​​​ $ftp.UsePassive = $true

 ​​ ​​ ​​​​ $ftp.UseBinary = $true

 ​​ ​​ ​​​​ $ftp.EnableSsl = $true

 ​​ ​​ ​​​​ $ftp.Credentials = new-object System.Net.NetworkCredential("Username","PASSWORD")

 ​​ ​​ ​​​​ $ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile

 ​​ ​​ ​​​​ $rs = $ftp.GetRequestStream()

 ​​ ​​​​ 

 ​​ ​​ ​​​​ $reader = New-Object System.IO.FileStream ($fileName, [IO.FileMode]::Open, [IO.FileAccess]::Read, [IO.FileShare]::Read)

 ​​ ​​ ​​​​ [byte[]]$buffer = new-object byte[] 4096

 ​​ ​​ ​​​​ [int]$count = 0

 ​​ ​​ ​​​​ do

 ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ $count = $reader.Read($buffer, 0, $buffer.Length)

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ $rs.Write($buffer,0,$count)

 ​​ ​​ ​​​​ } while ($count -gt 0)

 ​​ ​​ ​​​​ $reader.Close()

 ​​ ​​ ​​​​ $rs.Close()

 ​​ ​​ ​​​​ write-output "+transfer completed"

 ​​ ​​​​ 

#  ​​ ​​​​ $item.Delete()

#  ​​ ​​​​ write-output "+file deleted"

}

#https://mariuszgorzoch.wordpress.com/2014/01/07/upload-of-files-to-ftps-from-powershell/?unapproved=1863&moderation-hash=47b2634bf5645bd53452f459669098f5#comment-1863

2./ PowerShel​​ Download​​ FTP over TLS

[Net.ServicePointManager]::ServerCertificateValidationCallback={$true}

$username='Username'

$password='PASSWORD'

$ftp='ftp://172.16.0.40/'

$remote_file='/YourRemoteSubPath/test-ftp.txt'

$local_file =​​ 'D:\test-ftp.txt'

 

$ftpuri = $ftp + $remote_file

$uri=[system.URI] $ftpuri

$ftprequest=[system.net.ftpwebrequest]::Create($uri)

$ftprequest.Credentials=New-Object System.Net.NetworkCredential($username,$password)

$ftprequest.Method=[system.net.WebRequestMethods+ftp]::DownloadFile

$ftprequest.UseBinary = $true

$FTPRequest.EnableSsl = $true

$ftprequest.KeepAlive = $false

$response=$ftprequest.GetResponse()

$strm=$response.GetResponseStream()

try

{

$targetfile = New-Object IO.FileStream ($local_file,[IO.FileMode]::Create)

"File created: $local_file"

[byte[]]$readbuffer = New-Object byte[] 1024

do{

$readlength = $strm.Read($readbuffer,0,1024)

$targetfile.Write($readbuffer,0,$readlength)

}

while ($readlength -ne 0)

 

$targetfile.close()

}

catch

{

$_|fl * -Force

}

#https://www.thomasmaurer.ch/2010/11/powershell-ftp-upload-and-download/

#https://mariuszgorzoch.wordpress.com/2014/01/07/upload-of-files-to-ftps-from-powershell/?unapproved=1863&moderation-hash=47b2634bf5645bd53452f459669098f5#comment-1863

 

SaKuRai

Xin chào, Mình là Sakurai. Blog này là nơi để note lại và chia sẻ những kiến thức, kinh nghiệm mà mình và anh em trong Team. Cảm ơn các bạn đã quan tâm theo dõi!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Optimized with PageSpeed Ninja