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

 

BÀI VIẾT CÙNG CHUYÊN MỤC

Không copy được giữa máy Windows và máy ảo qua mRemoteNG/RDP: Nguyên nhân và cách sửa
Không copy được giữa máy Windows và máy ảo qua ...

mRemoteNG Remote Desktop RDP Clipboard Redirection rdpclip.exe VPS Windows ...

Hướng dẫn bật Nested Virtualization trên ESXi để chạy Android Studio Emulator trong máy ảo Windows
Hướng dẫn bật Nested Virtualization trên ESXi để chạy ...

Nested Virtualization ESXi VMware Android Studio Android Emulator WHPX Hyper-V ...

PITR (Point In Time Recovery) là gì? Khôi phục Database về đúng thời điểm cần thiết
PITR (Point In Time Recovery) là gì? Khôi phục ...

PITR, Point In Time Recovery, PITR là gì, Database Recovery, PostgreSQL PITR, ...

Backup Database Full, Incremental, Differential khác nhau thế nào?
Backup Database Full, Incremental, Differential khác ...

Backup Database, Full Backup, Incremental Backup, Differential Backup, Backup ...

Database Cluster là gì? High Availability, Failover và Cluster Database hoạt động như thế nào?
Database Cluster là gì? High Availability, Failover và ...

Database Cluster, Database Cluster là gì, High Availability Database, Database ...