Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ระบบ >> Windows Server

คัดลอกไฟล์ขนาดใหญ่ผ่านเครือข่ายที่ไม่เสถียรด้วย BITS และ PowerShell

ปกติถ่ายโอนไฟล์ผ่าน SMB, FTP, HTTP ปัญหาคือ Resume การดาวน์โหลดไฟล์ใหญ่ยาก โดยเฉพาะ Network ช้า/ไม่เสถียร นอกจากนี้ ใช้ Bandwidth เต็มที่ ส่งผลต่อ Network Performance และ Apps อื่น (QoS ไม่ได้ตลอด) บทความนี้สาธิตใช้ BITS Protocol และ PowerShell คัดลอกไฟล์ใหญ่ผ่าน Network ช้า/ไม่เสถียร

BITS Protocol

BITS (Background Intelligent Transfer Service) – Windows Service สำหรับถ่ายโอนไฟล์ระหว่างระบบ ใช้ Download/Upload ได้ ใช้โดย Windows Update (WSUS), SCCM Distribution Points, ฯลฯ

ข้อดีของ BITS

  • Intelligent Bandwidth Control: ใช้เฉพาะ Bandwidth ว่าง ดีดไดนามิกเมื่อ Apps อื่นใช้ Network
  • Background Transfer: ทำงานเบื้องหลัง โปร่งใสต่อ User
  • Auto Resume: ดำเนินต่อได้แม้ Connection 斷หรือ Reboot
  • Pause/Resume ได้โดยไม่สูญเสีย Data (Robocopy ก็ Resume ได้)
  • Priority Management สำหรับ Download Jobs
  • ใช้ Port 80 (HTTP) หรือ 443 (HTTPS) ไม่ต้องเปิด Port เพิ่มบน Firewall (ไม่เหมือน SMB Port 445 ที่มีช่องโหว่ SMB 1.0)
  • ไม่จำเป็นต้องมี IIS Server Deploy ไว้

BITS เหมาะสำหรับถ่ายโอนไฟล์ใหญ่ผ่าน Network ช้า/ไม่เสถียร (Satellite, GPRS, WAN)

ข้อกำหนด OS และ PowerShell

BITS มาตั้งแต่ Windows XP (มี bitsadmin.exe Deprecated แล้ว) ควรใช้ PowerShell Cmdlets ต้องการ OS ≥ Windows Vista/Server 2008 และ PowerShell 2.0+ Windows 10/Server 2016/2012 R2 รองรับเต็มที่ Tip Windows Server 2003 ใช้ได้ต้องติดตั้ง KB 923845 และ PowerShell 2.0 ต้องมี BITS ทั้ง Client และ Server

ดาวน์โหลดไฟล์ผ่าน BITS ด้วย PowerShell

สมมติดาวน์โหลด ISO จาก IIS HTTP (https://10.1.1.18/erd65_32.iso) Anonymous Access

  1. Import Module: Import-Module BitsTransfer
  2. ดู Cmdlets: get-command *-BITS* (มี 8 ตัว: Add-BitsFile, Complete-BitsTransfer, Get-BitsTransfer, Remove-BitsTransfer, Resume-BitsTransfer, Set-BitsTransfer, Start-BitsTransfer, Suspend-BitsTransfer)

Synchronous Transfer

Start-BitsTransfer –source https://10.1.1.18/erd65_32.iso -destination c:\temp
โหมด Synchronous แสดง Progress Bar คล้าย Copy ปกติ หาก Reboot ต้องดาวน์โหลดใหม่ทั้งไฟล์

Asynchronous Transfer (สำคัญสำหรับ Network ไม่เสถียร)

เพิ่ม -asynchronous จะ Resume อัตโนมัติหลัง Connection กลับมา:

Start-BitsTransfer -source https://10.1.1.18/erd65_32.iso -destination c:\temp -asynchronous

Important Default Priority = Foreground (สูงสุด) จะแย่ง Bandwidth ต้องตั้ง Priority ต่ำกว่า:
Start-BitsTransfer -source https://10.1.1.18/erd65_32.iso -destination c:\temp -asynchronous -Priority low

ตรวจสอบสถานะ: Get-BitsTransfer | fl หรือ Table: Get-BitsTransfer | select DisplayName, BytesTotal, BytesTransferred, JobState | Format-Table -AutoSize

Asynchronous Mode สร้างไฟล์ .TMP Hidden ตอนจบต้อง Convert: Get-BitsTransfer | Complete-BitsTransfer Job จะถูกลบออกจากรายการ

Upload ไฟล์ไป Network Share

Start-BitsTransfer -Source C:\iso\w10_1809.iso -Destination \\manch-fs1\iso -Asynchronous -DisplayName CopyISOtoMan

Suspend: Get-BitsTransfer -Name CopyISOtoMan | Suspend-BitsTransfer
Resume: Get-BitsTransfer -Name CopyISOtoMan | Resume-BitsTransfer -Asynchronous
Add Files: Get-BitsTransfer -Name CopyISOtoMan | Add-BitsFile -Source C:\iso\w10msu\* -Destination \\manch-fs1\iso -Asynchronous
Delete All Jobs: Get-BitsTransfer -Allusers | Remove-BitsTransfer (System Account Jobs ต้อง Run As SYSTEM)

Authentication

Server ต้อง Auth: Start-BitsTransfer -source https://10.1.1.18/erd65_32.iso -destination c:\temp -asynchronous -Priority low -Authentication NTLM -Credential Get-Credential

Script ติดตาม Progress

Import-Module BitsTransfer
$bitsjob = Start-BitsTransfer -Source https://10.1.1.18/erd65_32.iso -Destination c:\temp -Asynchronous
while( ($bitsjob.JobState.ToString() -eq 'Transferring') -or ($bitsjob.JobState.ToString() -eq 'Connecting') )
{ Write-host $bitsjob.JobState.ToString(); $Proc = ($bitsjob.BytesTransferred / $bitsjob.BytesTotal) * 100; Write-Host $Proc "%"; Sleep 3 }
Complete-BitsTransfer -BitsJob $bitsjob

คัดลอกโฟลเดอร์ทั้งหมดด้วย BITS

BITS ไม่ Recursive Copy เอง ต้อง Script เอง (ตรวจสอบ/สร้าง Target Directory ก่อน):

Import-Module BitsTransfer
$Source="\\lond-rep01\share\"
$Destination="c:\tmp\"
if ( -Not (Test-Path $Destination)) { $null = New-Item -Path $Destination -ItemType Directory }
$folders = Get-ChildItem -Name -Path $source -Directory -Recurse
$bitsjob = Start-BitsTransfer -Source $Source\*.* -Destination $Destination -asynchronous -Priority low
while( ($bitsjob.JobState.ToString() -eq 'Transferring') -or ($bitsjob.JobState.ToString() -eq 'Connecting') ) { Sleep 4 }
Complete-BitsTransfer -BitsJob $bitsjob
foreach ($i in $folders) { $exists = Test-Path $Destination\$i; if ($exists -eq $false) {New-Item $Destination\$i -ItemType Directory} $bitsjob = Start-BitsTransfer -Source $Source\$i\*.* -Destination $Destination\$i -asynchronous -Priority low; while( ($bitsjob.JobState.ToString() -eq 'Transferring') -or ($bitsjob.JobState.ToString() -eq 'Connecting') ) { Sleep 4 }; Complete-BitsTransfer -BitsJob $bitsjob }

Upload ไฟล์ขึ้น HTTP Server ด้วย BITS

Target Server ต้องมี IIS + Bits Server Extension Component → Virtual Directory Settings → Bits Uploads → Enable Allow clients to upload files

NTFS Permissions: Anonymous → Write Access, Authenticated Users → RW Access

Upload Command: Start-bits –source c:\iso\winsrv2016.iso -destination https://10.1.1.18/MEDVImages/winsrv2016.iso –Transfertype Upload

Note IIS Default Max Upload 30 MB ต้องแก้ maxAllowContentLength ใน web.config สำหรับไฟล์ใหญ่

BITS เป็น Alternative ที่ยอดเยี่ยมแทน SMB Copy ทนต่อการขาดการเชื่อมต่อ/Reboot ไม่กิน Bandwidth มาก ไม่รบกวน Apps อื่น เหมาะสำหรับถ่ายโอน ISO, VM Files (vmdk, vhdx) ผ่าน WAN