다음을 통해 공유


WinRM Windows PowerShell Cmdlet을 사용하여 BITS 전송 작업 관리

Windows 원격 관리 PowerShell cmdlet은 BITS(Background Intelligent Transfer Service) 전송 작업을 관리할 수 있습니다. BITS 원격 관리에 대한 자세한 내용은 BITS 공급자BITS 공급자 클래스를 참조하세요.

다음 예제에는 BITS 공급자가 필요합니다. BITS Compact 서버가 설치된 후에 BITS 공급자를 사용할 수 있습니다. 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은 BitsClientJob 클래스의 instance 만들고 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
    }
    

    앞의 예제는 작업의 상태 폴링하고 상태 따라 작업을 수행하는 스크립트입니다. 다음 작업을 수행할 수 있습니다.

    • $result. 상태는 4(BG_JOB_STATE_ERROR), Invoke-WsmanAction cmdlet은 SetJobState 메서드를 호출하고 작업을 취소합니다.
    • $result. 상태는 5(BG_JOB_STATE_TRANSIENT_ERROR), Invoke-WsmanAction cmdlet은 SetJobState 메서드를 호출하고 작업을 취소합니다.
    • $result. 상태는 6(BG_JOB_STATE_TRANSFERRED), Invoke-WsmanAction cmdlet은 SetJobState 메서드를 호출하고 상태를 완료하도록 설정합니다.

    작업 상태에 대한 자세한 내용은 BG_JOB_STATE 열거형을 참조하세요.

Get-Credential

Invoke-WsmanAction

Set-WsmanInstance