使用 WinRM Windows PowerShell Cmdlet 管理 BITS 傳輸作業

Windows 遠端管理 PowerShell Cmdlet 可以管理背景智慧型手機傳送服務 (BITS) 傳輸作業。 如需 BITS 遠端管理的詳細資訊,請參閱 BITS 提供者BITS 提供者類別

下列範例需要 BITS 提供者。 BITS 提供者會在安裝 BITS Compact 伺服器之後提供。 如需安裝 Compact 伺服器的相關信息,請參閱 BITS Compact Server 檔。

  1. 建立 BITS 傳輸作業。

    # Get the credentials to connect to the remote client computer
    $cred = Get-Credential
    $result = Invoke-WsmanAction -Action CreateJob –Resourceuri wmi/root/microsoft/bits/BitsClientJob `
    –Valueset @{DisplayName="TestJob"; RemoteUrl="https://Server01/servertestdir/testfile1.txt"; LocalFile="C:\clienttestdir\testfile1.txt";Type=0} `
    –ComputerName Client1  -Credential $cred
    

    Get-Credential Cmdlet 會要求使用者的認證連線到遠端計算機,並將認證指派給$cred物件。

    Invoke-WsmanAction Cmdlet 會建立 Client1 上的 BITS 傳輸作業,方法是建立 BitsClientJob 類別的實例,並使用 Valueset 參數中定義的哈希表中的資訊。 Valueset 參數會指定填入 CreateJob 方法參數所需的資訊。 在上述範例中,使用者會將 Type 參數設定為 0 (download)。 使用者也會指定下載作業的遠端和本機檔案名稱。 如需建立 BITS 傳輸作業的詳細資訊,以及參數的詳細資訊,請參閱 CreateJob 方法。

    Invoke-WsmanAction Cmdlet 會將結果指派給$result變數。

    注意

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

     

  2. 設定 BITS 傳輸作業的優先順序。

    Set-WsmanInstance  -ResourceURI  wmi/root/microsoft/bits/BitsClientJob -SelectorSet @{JobId=$result.JobId} `
    -ValueSet @{Priority=0} –ComputerName Client1  -Credential $cred
    

    Set-WsmanInstance Cmdlet 會將新的 BITS 傳輸工作優先順序變更為 0 (BG_JOB_PRIORITY_FOREGROUND)。 如需優先順序層級的詳細資訊,請參閱 BG_JOB_PRIORITY 列舉。

  3. 繼續 BITS 傳輸作業。

    Invoke-WsmanAction -Action SetJobState -ResourceUri wmi/root/microsoft/bits/BitsClientJob  -selectorset @{JobId=$result.JobId}  `
    -valueset @{JobState= 2} –ComputerName Client1  -Credential $cred
    

    Invoke-WsmanAction Cmdlet 會呼叫 SetJobState 方法,此方法會將作業狀態設定為 2 (繼續作業)。

  4. 管理 BITS 傳輸作業。

    $IsPprocessing = $TRUE
    while ($IsPprocessing)
    {
        $result = Get-WsmanInstance  -ResourceURI  wmi/root/microsoft/bits/BitsClientJob -selectorset @{JobId = $result.JobId} `
               –ComputerName Client1  -Credential $cred
        if ($result.State -eq 6)
        {
    
    #Complete the job           
            Invoke-WsmanAction -action SetJobState -resourceuri wmi/root/microsoft/bits/BitsClientJob  -selectorset @{JobId=$result.JobId}  `
                          -valueset @{JobState= 1} –ComputerName Client1  -Credential $cred
            "Job Successfully Transferred"
            $IsPprocessing = $FALSE;
        }
        elseif (($result.State -eq 4) -or ($result.State -eq 5))
        {
    
    #Cancel the job
            "Job is in Error " 
            Invoke-WsmanAction -action SetJobState -resourceuri wmi/root/microsoft/bits/BitsClientJob  -selectorset @{JobId=$result.JobId}  `
                         -valueset @{JobState= 0} –ComputerName Client1  -Credential $cred
            # You can troubleshoot or delete the job
            $IsPprocessing = $FALSE;
        }
        else
        {
        "Job is processing\n" 
        }
    
    # Perform other action or poll in a tight loop. This example sleeps for 5 seconds
    sleep 5
    }
    

    上述範例是用來輪詢作業狀態的腳本,並根據狀態採取動作。 下列動作是可能的:

    如需作業狀態的詳細資訊,請參閱 BG_JOB_STATE 列舉。

Get-Credential

Invoke-WsmanAction

Set-WsmanInstance