使用 Windows PowerShell 建立 BITS 傳送工作

您可以使用 PowerShell Cmdlet 來建立同步和異步的背景智慧型傳送服務 (BITS) 傳輸作業。

本主題中的所有範例都會使用 Start-BitsTransfer Cmdlet。 若要使用 Cmdlet,請務必先匯入模組。 若要安裝模組,請執行下列命令:Import-Module BitsTransfer。 如需詳細資訊,請在PowerShell提示字元中輸入 Get-Help Start-BitsTransfer

重要

當您在非互動式內容中執行的進程內使用 *-BitsTransfer Cmdlet,例如 Windows 服務時,可能無法將檔案新增至 BITS 作業,這可能會導致暫停狀態。 若要讓作業繼續進行,用來建立傳輸作業的身分識別必須登入。 例如,在以工作排程器工作身分執行的 PowerShell 腳本中建立 BITS 作業時,除非已啟用工作排程器的工作設定「只在使用者登入時執行」,否則 BITS 傳輸永遠不會完成。

 

建立同步 BITS 傳輸作業

Start-BitsTransfer -Source https://Server01/serverdir/testfile1.txt `
-Destination C:\clientdir\testfile1.txt

注意

嚴重腔調字元 (') 用來表示換行符。

 

在上述範例中,檔案的本機和遠端名稱分別在SourceDestination 參數中指定。 當檔案傳送完成或進入錯誤狀態時,命令提示字元就會出現。

默認傳輸類型為 [下載]。 當您將檔案上傳至 HTTP 位置時, TransferType 參數必須設定為 Upload。

因為 Start-BitsTransfer Cmdlet 會強制執行參數位置,因此不需要為 Source 和 Destination 參數指定參數名稱。 因此,此命令可以簡化,如下所示。

Start-BitsTransfer https://Server01/serverdir/testfile1.txt C:\clientdir\testfile1.txt

使用多個檔案建立同步 BITS 傳輸作業

Start-BitsTransfer -Source C:\clientsourcedir\*.txt `
-Destination c:\clientdir\ -TransferType Download

在上述範例中 ,Start-BitsTransfer 命令會建立新的 BITS 傳輸作業。 所有檔案都會新增至此作業,並循序傳送至用戶端。

注意

目的地路徑不可以使用萬用字元。 目的地路徑支援相對目錄、根路徑或隱含目錄(也就是目前目錄)。 目的地檔案無法使用通配符重新命名。 此外,HTTP 和 HTTPS URL 不適用於通配符。 通配符僅適用於 UNC 路徑和本機目錄。

 

若要建立同步 BITS 傳輸作業,並指定遠端伺服器的認證

Start-BitsTransfer -DisplayName MyJob -Credential Username\Domain `
-Source https://server01/servertestdir/testfile1.txt -Destination c:\clienttestdir\testfile1.txt `
-ProxyUsage Override -ProxyList @(https://proxy1, 123.24.21.23, proxy3)

在上述範例中,使用者會建立 BITS 傳輸作業,以從需要驗證的伺服器下載檔。 系統會提示使用者輸入認證,而且 Credential 參數會將認證對象傳遞至 Start-BitsTransfer Cmdlet。 用戶設定明確的 Proxy,而 BITS 傳輸作業只會使用 ProxyList 參數所定義的 Proxy。 DisplayName 參數會提供 BITS 傳輸作業唯一的顯示名稱。

從 CSV 檔案建立同步 BITS 傳輸作業

Import-CSV filelist.txt | Start-BitsTransfer -TransferType Upload

注意

“|” 是管道字元。

 

在上述範例中,使用者會建立 BITS 傳輸作業,以從用戶端上傳多個檔案。 Import-CSV Cmdlet 會匯入來源和目的地檔案位置,並將其管線傳送至 Start-BitsTransfer 命令。 Start-BitsTransfer 命令會為每個檔案建立新的 BITS 傳輸作業、將檔案新增至作業,然後將檔案循序傳輸至伺服器。

Filelist.txt檔案的內容應為下列格式:

Source, Destination
c:\clienttestdir\testfile1.txt, https://server01/servertestdir/testfile1.txt
c:\clienttestdir\testfile2.txt, https://server01/servertestdir/testfile2.txt
c:\clienttestdir\testfile3.txt, https://server01/servertestdir/testfile3.txt
c:\clienttestdir\testfile4.txt, https://server01/servertestdir/testfile4.txt

建立異步 BITS 傳輸作業

$Job = Start-BitsTransfer -Source https://Server1.TrustedDomain.com/File1.zip `
       -Destination d:\temp\downloads\ -Asynchronous

while (($Job.JobState -eq "Transferring") -or ($Job.JobState -eq "Connecting")) `
       { sleep 5;} # Poll for status, sleep for 5 seconds, or perform an action.

Switch($Job.JobState)
{
    "Transferred" {Complete-BitsTransfer -BitsJob $Job}
    "Error" {$Job | Format-List } # List the errors.
    default {"Other action"} #  Perform corrective action.
}

在上述範例中,BITS 傳輸工作已指派給$Job變數。 檔案會循序下載。 傳輸作業完成之後,檔案會立即可供使用。 如果 $Job.JobState 傳回 “Transferred”,$Job物件會傳送至 Complete-BitsTransfer Cmdlet。

如果 $Job.JobState 傳回 “Error”,$Job物件會傳送至 Format-List Cmdlet 以列出錯誤。

管理 PowerShell 遠端工作階段

從 Windows 10 版本 1607 開始,您可以執行 PowerShell Cmdlet、BITS 管理員 或其他使用 BITS 介面的應用程式,從連線至另一部計算機(實體或虛擬)的 PowerShell 遠端命令行使用 BITS 介面。 使用 PowerShell Direct 命令行至相同實體機器上的虛擬機時,無法使用此功能,而且在使用 WinRM Cmdlet 時無法使用此功能。

從遠端 PowerShell 會話建立的 BITS 作業會在該工作階段的使用者帳戶內容下執行,而且只有在至少有一個作用中的本機登入工作階段或與該使用者帳戶相關聯的遠端 PowerShell 會話時才會進行。 您可以使用PowerShell的持續性 PSSessions 來執行遠端命令,而不需要讓每個作業保持開啟 PowerShell 視窗才能繼續進行,如使用 Windows PowerShell 管理員 註冊遠端電腦中所述

  • New-PSSession 會建立持續性遠端 PowerShell 會話。 建立之後,PSSession 物件會保存在遠端計算機中,直到明確刪除為止。 在作用中會話中起始的任何 BITS 作業都會進行傳輸數據,即使用戶端與會話中斷連線也一樣。
  • Disconnect-PSSession 會中斷用戶端計算機與遠端 PowerShell 會話的連線,而遠端電腦會繼續維護會話的狀態。 最重要的是,遠端會話的進程會繼續執行,而BITS作業會繼續進行。 用戶端電腦甚至可以在呼叫 Disconnect-PSSession 之後重新啟動和/或關閉。
  • 連線-PSSession 會將用戶端電腦重新連線到作用中的遠端 PowerShell 會話。
  • Remove-PSSession 會卸除遠端 PowerShell 會話。

下列範例示範如何使用PowerShell Remote來處理異步 BITS 傳輸作業,讓作業即使在未主動連線到遠端會話時仍可繼續進行。

# Establish a PowerShell Remote session in Server01 with name 'MyRemoteSession'
New-PSSession -ComputerName Server01 -Name MyRemoteSession -Credential Domain01\User01

# Enter the previously-established session to execute commands
Enter-PSSession -Name MyRemoteSession

# Enumerate active BITS transfers on the remote machine
Get-BitsTransfer

# While running in the context of the PowerShell Remote session, start a new BITS transfer
Start-BitsTransfer -Source https://Server1.TrustedDomain.com/File1.zip -Destination c:\temp\downloads\ -Asynchronous

# Exit the PowerShell Remote session's context
Exit-PSSession

# Disconnect the 'MyRemoteSession' PowerShell Remote session from the current PowerShell window
# After this command, it is safe to close the current PowerShell window and turn off the local machine
Disconnect-PSSession -Name MyRemoteSession


# The commands below can be executed from a different PowerShell window, even after rebooting the local machine
# Connect the 'MyRemoteSession' PowerShell Remote session to the current PowerShell window
Connect-PSSession -ComputerName Server01 -Name MyRemoteSession

# Enter the previously-established session to execute commands
Enter-PSSession -Name MyRemoteSession

# Enumerate active BITS transfers on the remote machine
Get-BitsTransfer

# Manage BITS transfers on the remote machine via Complete-BitsTransfer, Remove-BitsTransfer, etc.

# Exit the PowerShell Remote session's context
Exit-PSSession

# Destroy the 'MyRemoteSession' PowerShell Remote session when no longer needed
Remove-PSSession -Name MyRemoteSession

Start-BitsTransfer

Complete-BitsTransfer