Receive-Job

현재 세션에서 PowerShell 백그라운드 작업의 결과를 가져옵니다.

Syntax

Receive-Job
       [-Job] <Job[]>
       [[-Location] <String[]>]
       [-Keep]
       [-NoRecurse]
       [-Force]
       [-Wait]
       [-AutoRemoveJob]
       [-WriteEvents]
       [-WriteJobInResults]
       [<CommonParameters>]
Receive-Job
       [-Job] <Job[]>
       [[-Session] <PSSession[]>]
       [-Keep]
       [-NoRecurse]
       [-Force]
       [-Wait]
       [-AutoRemoveJob]
       [-WriteEvents]
       [-WriteJobInResults]
       [<CommonParameters>]
Receive-Job
       [-Job] <Job[]>
       [[-ComputerName] <String[]>]
       [-Keep]
       [-NoRecurse]
       [-Force]
       [-Wait]
       [-AutoRemoveJob]
       [-WriteEvents]
       [-WriteJobInResults]
       [<CommonParameters>]
Receive-Job
       [-Keep]
       [-NoRecurse]
       [-Force]
       [-Wait]
       [-AutoRemoveJob]
       [-WriteEvents]
       [-WriteJobInResults]
       [-Name] <String[]>
       [<CommonParameters>]
Receive-Job
       [-Keep]
       [-NoRecurse]
       [-Force]
       [-Wait]
       [-AutoRemoveJob]
       [-WriteEvents]
       [-WriteJobInResults]
       [-InstanceId] <Guid[]>
       [<CommonParameters>]
Receive-Job
       [-Keep]
       [-NoRecurse]
       [-Force]
       [-Wait]
       [-AutoRemoveJob]
       [-WriteEvents]
       [-WriteJobInResults]
       [-Id] <Int32[]>
       [<CommonParameters>]

Description

cmdlet은 Receive-Job cmdlet 또는 cmdlet의 AsJob 매개 변수를 사용하여 Start-Job 시작한 작업과 같은 PowerShell 백그라운드 작업의 결과를 가져옵니다. 모든 작업의 결과를 가져오거나 이름, ID, 인스턴스 ID, 컴퓨터 이름, 위치 또는 세션 또는 작업 개체를 제출하여 작업을 식별할 수 있습니다.

PowerShell 백그라운드 작업을 시작하면 작업이 시작되지만 결과가 즉시 표시되지는 않습니다. 대신 이 명령은 백그라운드 작업을 나타내는 개체를 반환합니다. 작업 개체는 작업에 대한 유용한 정보를 포함하지만 결과가 포함되지 않습니다. 이 메서드를 사용하면 작업이 실행되는 동안 세션에서 작업을 계속할 수 있습니다. PowerShell의 백그라운드 작업에 대한 자세한 내용은 about_Jobs 참조하세요.

cmdlet은 Receive-Job 명령이 제출될 때까지 Receive-Job 생성된 결과를 가져옵니다. 결과가 아직 완료되지 않은 경우 추가 Receive-Job 명령을 실행하여 다시 기본 결과를 가져올 수 있습니다.

기본적으로 작업 결과는 받을 때 시스템에서 삭제되지만 Keep 매개 변수를 사용하여 결과를 다시 받을 수 있도록 저장할 수 있습니다. 작업 결과를 삭제하려면 Keep 매개 변수 없이 명령을 다시 실행 Receive-Job 하거나 세션을 닫거나 cmdlet을 사용하여 Remove-Job 세션에서 작업을 삭제합니다.

Windows PowerShell 3.0 Receive-Job 부터 워크플로 작업 및 예약된 작업의 인스턴스와 같은 사용자 지정 작업 유형의 결과도 가져옵니다. Receive-Job 사용자 지정 작업 형식의 결과를 가져오려면 cmdlet을 사용하거나 모듈에서 cmdlet을 사용 Import-Module 하거나 가져와서 명령을 실행 Receive-Job 하기 전에 사용자 지정 작업 유형을 지원하는 모듈을 세션으로 가져옵니다. 특정 사용자 지정 작업 유형에 대한 자세한 내용은 사용자 지정 작업 유형 기능의 설명서를 참조하세요.

예제

예제 1: 특정 작업에 대한 결과 가져오기

$job = Start-Job -ScriptBlock {Get-Process}
Receive-Job -Job $job

이러한 명령은 작업 매개 변수 Receive-Job 를 사용하여 특정 작업의 결과를 가져옵니다.

첫 번째 명령은 작업을 Start-Job 시작하고 작업 개체를 변수에 $job 저장합니다.

두 번째 명령은 cmdlet을 사용하여 Receive-Job 작업 결과를 가져옵니다. 작업 매개 변수를 사용하여 작업을 지정합니다.

예제 2: Keep 매개 변수 사용

$job = Start-Job -ScriptBlock {Get-Service dhcp, fakeservice}
$job | Receive-Job -Keep

Cannot find any service with service name 'fakeservice'.
    + CategoryInfo          : ObjectNotFound: (fakeservice:String) [Get-Service], ServiceCommandException
    + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand
    + PSComputerName        : localhost

Status   Name               DisplayName
------   ----               -----------
Running  dhcp               DHCP Client

$job | Receive-Job -Keep

Cannot find any service with service name 'fakeservice'.
    + CategoryInfo          : ObjectNotFound: (fakeservice:String) [Get-Service], ServiceCommandException
    + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand
    + PSComputerName        : localhost

Status   Name               DisplayName
------   ----               -----------
Running  dhcp               DHCP Client

다음은 변수에 $job 작업을 저장하고 작업을 cmdlet으로 Receive-Job 파이프하는 예제입니다. 또한 매개 -Keep 변수를 사용하여 집계된 모든 스트림 데이터를 첫 번째 보기 후에 다시 검색할 수 있습니다.

예제 3: 여러 백그라운드 작업의 결과 가져오기

AsJob 매개 변수 Invoke-Command 를 사용하여 작업을 시작하면 작업이 원격 컴퓨터에서 실행되는 경우에도 작업 개체가 로컬 컴퓨터에 만들어집니다. 따라서 로컬 명령을 사용하여 작업을 관리합니다.

또한 AsJob을 사용하는 경우 PowerShell은 시작된 각 작업에 대한 자식 작업이 포함된 하나의 작업 개체를 반환합니다. 이 경우 작업 개체에는 각 원격 컴퓨터의 각 작업에 대해 하나씩 3개의 자식 작업이 포함됩니다.

# Use the Invoke-Command cmdlet with the -AsJob parameter to start a background job that runs a Get-Service command on three remote computers.
# Store the resulting job object in the $j variable
$j = Invoke-Command -ComputerName Server01, Server02, Server03 -ScriptBlock {Get-Service} -AsJob
# Display the value of the **ChildJobs** property of the job object in $j.
# The display shows that the command created three child jobs, one for the job on each remote computer.
# You could also use the -IncludeChildJobs parameter of the Get-Job cmdlet.
$j.ChildJobs

Id   Name     State      HasMoreData   Location       Command
--   ----     -----      -----------   --------       -------
2    Job2     Completed  True          Server01       Get-Service
3    Job3     Completed  True          Server02       Get-Service
4    Job4     Completed  True          Server03       Get-Service

# Use the Receive-Job cmdlet to get the results of just the Job3 child job that ran on the Server02 computer.
# Use the *Keep* parameter to allow you to view the aggregated stream data more than once.
Receive-Job -Name Job3 -Keep

Status  Name        DisplayName                        PSComputerName
------  ----------- -----------                        --------------
Running AeLookupSvc Application Experience             Server02
Stopped ALG         Application Layer Gateway Service  Server02
Running Appinfo     Application Information            Server02
Running AppMgmt     Application Management             Server02

예제 4: 여러 원격 컴퓨터에서 백그라운드 작업 결과 가져오기

# Use the New-PSSession cmdlet to create three user-managed PSSessions on three servers, and save the sessions in the $s variable.
$s = New-PSSession -ComputerName Server01, Server02, Server03
# Use Invoke-Command run a Start-Job command in each of the PSSessions in the $s variable.
# The creates a new job with a custom name to each server
# The job outputs the datetime from each server
# Save the job objects in the $j variable.
$j = Invoke-Command -Session $s -ScriptBlock {​Start-Job -Name $('MyJob-' +$env:COMPUTERNAME) -ScriptBlock {(Get-Date).ToString()​}​}
# To confirm that these job objects are from the remote machines, run Get-Job to show no local jobs running.
Get-Job​​



# Display the three job objects in $j.
# Note that the Localhost location is not the local computer, but instead localhost as it relates to the job on each Server.
$j

Id   Name               State      HasMoreData   Location   Command
--   ----               -----      -----------   --------   -------
1    MyJob-Server01     Completed  True          Localhost  (Get-Date).ToString()
2    MyJob-Server02     Completed  True          Localhost  (Get-Date).ToString()
3    MyJob-Server03     Completed  True          Localhost  (Get-Date).ToString()

# Use Invoke-Command to run a Receive-Job command in each of the sessions in the $s variable and save the results in the $results variable.
# The Receive-Job command must be run in each session because the jobs were run locally on each server.
$results = Invoke-Command -Session $s -ScriptBlock {​Receive-Job -Name $('MyJob-' +$env:COMPUTERNAME)}

3/22/2021 7:41:47 PM
3/22/2021 7:41:47 PM
3/22/2021 9:41:47 PM

이 예제에서는 세 개의 원격 컴퓨터에서 실행되는 백그라운드 작업의 결과를 가져오는 방법을 보여줍니다. 이전 예제와 달리 명령을 실행하는 Start-Job 데 사용하면 Invoke-Command 실제로 세 대의 컴퓨터 각각에서 3개의 독립적인 작업이 시작되었습니다. 결과적으로 이 명령은 세 개의 다른 컴퓨터에서 로컬로 실행되는 세 개의 작업을 나타내는 세 개의 작업 개체를 반환했습니다.

예제 5: 자식 작업 액세스

매개 변수는 -Keep 작업의 집계된 스트림 상태를 유지하므로 다시 볼 수 있습니다. 이 매개 변수가 없으면 작업이 수신될 때 집계된 모든 스트림 데이터가 지워집니다. 자세한 내용은 about_Job_Details 참조하세요 .

참고 항목

집계된 스트림에는 모든 자식 작업의 스트림이 포함됩니다. 작업 개체 및 자식 작업 개체를 통해 데이터의 개별 스트림에 연결할 수 있습니다.

Start-Job -Name TestJob -ScriptBlock {dir C:\, Z:\}
# Without the Keep parameter, aggregated child job data is displayed once.
# Then destroyed.
Receive-Job -Name TestJob

Directory: C:\

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-r---        1/24/2019   7:11 AM                Program Files
d-r---        2/13/2019   8:32 AM                Program Files (x86)
d-r---        10/3/2018  11:47 AM                Users
d-----         2/7/2019   1:52 AM                Windows
Cannot find drive. A drive with the name 'Z' does not exist.
    + CategoryInfo          : ObjectNotFound: (Z:String) [Get-ChildItem], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
    + PSComputerName        : localhost

# It would seem that the child job data is gone.
Receive-Job -Name TestJob



# Using the object model, you can still retrieve child job data and streams.
$job = Get-Job -Name TestJob
$job.ChildJobs[0].Error

Cannot find drive. A drive with the name 'Z' does not exist.
    + CategoryInfo          : ObjectNotFound: (Z:String) [Get-ChildItem], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
    + PSComputerName        : localhost

매개 변수

-AutoRemoveJob

이 cmdlet은 작업 결과를 반환한 후 작업을 삭제한다는 것을 나타냅니다. 작업에 더 많은 결과가 있는 경우 작업은 여전히 삭제되지만 Receive-Job 메시지가 표시됩니다.

이 매개 변수는 사용자 지정 작업 유형에서만 작동합니다. 예약된 작업의 인스턴스와 같이 작업 또는 세션 외부의 형식을 저장하는 작업 유형의 인스턴스를 위해 설계되었습니다.

Wait 매개 변수 없이는 이 매개 변수를 사용할 수 없습니다.

이 매개 변수는 Windows PowerShell 3.0에서 도입되었습니다.

Type:SwitchParameter
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-ComputerName

컴퓨터 이름의 배열을 지정합니다.

이 매개 변수는 로컬 컴퓨터에 저장된 작업 결과 중에서 선택하며, 원격 컴퓨터에서 실행되는 작업에 대한 데이터는 가져올 수 없습니다. 원격 컴퓨터에 저장된 작업 결과를 얻으려면 cmdlet을 Invoke-Command 사용하여 명령을 원격으로 Receive-Job 실행합니다.

Type:String[]
Aliases:Cn
Position:1
Default value:All computers available
Required:False
Accept pipeline input:True
Accept wildcard characters:True

-Force

작업이 일시 중단됨 또는 연결 끊김 상태인 경우 이 cmdlet이 계속 대기 중임을 나타냅니다. 기본적으로 Wait 매개 변수는 Receive-Job 작업이 다음 상태 중 하나에 있을 때 대기를 반환하거나 종료합니다.

  • Completed
  • 실패
  • 중지됨
  • 일시 중단됨
  • 연결 끊김.

Force 매개 변수는 Wait 매개 변수가 명령에 사용되는 경우에만 유효합니다.

이 매개 변수는 Windows PowerShell 3.0에서 도입되었습니다.

Type:SwitchParameter
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Id

ID 배열을 지정합니다. 이 cmdlet은 지정된 ID를 사용하여 작업의 결과를 가져옵니다.

ID는 현재 세션의 작업을 고유하게 식별하는 정수입니다. 인스턴스 ID보다 기억하고 입력하는 것이 더 쉽지만 현재 세션에서만 고유합니다. 하나 이상의 ID를 쉼표로 구분하여 입력할 수 있습니다. 작업의 ID를 찾으려면 .를 사용합니다 Get-Job.

Type:Int32[]
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-InstanceId

인스턴스 ID의 배열을 지정합니다. 이 cmdlet은 지정된 인스턴스 ID를 사용하여 작업의 결과를 가져옵니다.

인스턴스 ID는 컴퓨터에서 작업을 고유하게 식별하는 GUID입니다. 작업의 인스턴스 ID를 찾으려면 cmdlet을 Get-Job 사용합니다.

Type:Guid[]
Position:0
Default value:All instances
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-Job

결과를 검색할 작업을 지정합니다.

작업을 가져오는 명령이나 작업이 포함된 변수를 입력합니다. 작업 개체를 .에 파이프할 Receive-Job수도 있습니다.

Type:Job[]
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-Keep

이 cmdlet은 집계된 스트림 데이터를 받은 후에도 시스템에 저장한다는 것을 나타냅니다. 기본적으로 집계된 스트림 데이터는 .을 Receive-Job사용하여 본 후 지워집니다.

세션을 닫거나 cmdlet을 Remove-Job 사용하여 작업을 제거하면 집계된 스트림 데이터도 삭제됩니다.

Type:SwitchParameter
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Location

위치 배열을 지정합니다. 이 cmdlet은 지정된 위치에 있는 작업의 결과만 가져옵니다.

Type:String[]
Position:1
Default value:All locations
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Name

친숙한 이름의 배열을 지정합니다. 이 cmdlet은 지정된 이름을 가진 작업의 결과를 가져옵니다. 와일드카드 문자가 지원됩니다.

Type:String[]
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:True

-NoRecurse

이 cmdlet은 지정된 작업에서만 결과를 가져옵니다. 기본적으로 Receive-Job 지정된 작업의 모든 자식 작업의 결과도 가져옵니다.

Type:SwitchParameter
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Session

세션 배열을 지정합니다. 이 cmdlet은 지정된 PowerShell 세션(PSSession)에서 실행된 작업의 결과를 가져옵니다. PSSession이 포함된 변수 또는 명령과 같이 Get-PSSession PSSession을 가져오는 명령을 입력합니다.

Type:PSSession[]
Position:1
Default value:All sessions
Required:False
Accept pipeline input:True
Accept wildcard characters:False

-Wait

이 cmdlet은 모든 작업 결과를 받을 때까지 명령 프롬프트를 표시하지 않음을 나타냅니다. 기본적으로 Receive-Job 사용 가능한 결과를 즉시 반환합니다.

기본적으로 Wait 매개 변수는 작업이 다음 상태 중 하나가 될 때까지 대기합니다.

  • Completed
  • 실패
  • 중지됨
  • 일시 중단됨
  • 연결 끊김.

작업 상태가 일시 중단 또는 연결 끊김인 경우 대기 매개 변수를 계속 대기하도록 하려면 대기 매개 변수와 Force 매개 변수를 함께 사용합니다.

이 매개 변수는 Windows PowerShell 3.0에서 도입되었습니다.

Type:SwitchParameter
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-WriteEvents

이 cmdlet은 작업이 완료될 때까지 기다리는 동안 작업 상태의 변경 내용을 보고합니다.

이 매개 변수는 Wait 매개 변수가 명령에 사용되고 Keep 매개 변수가 생략된 경우에만 유효합니다.

이 매개 변수는 Windows PowerShell 3.0에서 도입되었습니다.

Type:SwitchParameter
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-WriteJobInResults

이 cmdlet이 작업 개체를 반환한 다음 결과를 반환한다는 것을 나타냅니다.

이 매개 변수는 Wait 매개 변수가 명령에 사용되고 Keep 매개 변수가 생략된 경우에만 유효합니다.

이 매개 변수는 Windows PowerShell 3.0에서 도입되었습니다.

Type:SwitchParameter
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

입력

Job

작업 개체를 이 cmdlet으로 파이프할 수 있습니다.

출력

PSObject

이 cmdlet은 작업의 명령 결과를 반환합니다.

참고

Windows PowerShell에는 다음 별칭이 포함됩니다.Receive-Job

  • rcjb