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

Mẫu nhúng HTML cho khóa học – Course trên Canvas LMS tại CloudX
Mẫu nhúng HTML cho khóa học – Course trên Canvas LMS ...

Mẫu nhúng HTML cho khóa học – Course trên Canvas LMS tại CloudX

Telegram biến mất khỏi App Store: Người dùng iPhone tải và cài đặt thế nào cho an toàn?
Telegram biến mất khỏi App Store: Người dùng iPhone ...

Telegram biến mất khỏi App Store: Người dùng iPhone tải và cài đặt thế nào cho ...

CẢNH BÁO BẢO MẬT: Lỗ Hổng Nghiêm Trọng CVE-2026-42533 Trên NGINX – Cập Nhật Ngay Để Tránh Rủi Ro Bị Tấn Công
CẢNH BÁO BẢO MẬT: Lỗ Hổng Nghiêm Trọng CVE-2026-42533 ...

CẢNH BÁO BẢO MẬT: Lỗ Hổng Nghiêm Trọng CVE-2026-42533 Trên NGINX – Cập Nhật ...

BACKUP DỮ LIỆU LỚN TỪ LINUX UBUNTU 24.04 LTS LÊN GOOGLE DRIVE AN TOÀN, TỐI ƯU BĂNG THÔNG
BACKUP DỮ LIỆU LỚN TỪ LINUX UBUNTU 24.04 LTS LÊN ...

BACKUP DỮ LIỆU LỚN TỪ LINUX UBUNTU 24.04 LTS LÊN GOOGLE DRIVE AN TOÀN, TỐI ƯU ...

Top 5 hệ thống LMS tốt nhất năm 2026 – Giải pháp quản lý học tập hiện đại cho trường học và doanh nghiệp
Top 5 hệ thống LMS tốt nhất năm 2026 – Giải pháp quản ...

Top 5 hệ thống LMS tốt nhất năm 2026 – Giải pháp quản lý học tập hiện đại cho ...

Hướng dẫn cấu hình SSL Let's Encrypt nhiều Website IIS bằng Win-ACME (Windows Server 2025)
Hướng dẫn cấu hình SSL Let's Encrypt nhiều Website IIS ...

Hướng dẫn cấu hình SSL Let's Encrypt nhiều Website IIS bằng Win-ACME (Windows ...