使用 WinRM Windows PowerShell Cmdlet 管理 BITS 传输作业

Windows 远程管理 PowerShell cmdlet 可以管理后台智能传输服务 (BITS) 传输作业。 有关 BITS 远程管理的详细信息,请参阅 BITS 提供程序BITS 提供程序类

以下示例需要 BITS 提供程序。 BITS 提供程序在安装 BITS Compact 服务器后可用。 有关安装 Compact 服务器的信息,请参阅 BITS Compact 服务器文档。

  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 通过创建 BitsClientJob 类的实例并使用 Valueset 参数中定义的哈希表中的信息,在 Client1 上创建 BITS 传输作业。 Valueset 参数指定填充 CreateJob 方法的参数所需的信息。 在前面的示例中,用户将 Type 参数设置为 0(下载)。 用户还指定下载作业的远程文件和本地文件的名称。 有关创建 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