Tag Archives: Location

Receive-Job

NAME
    Receive-Job

SYNOPSIS
    Gets the results of the Windows PowerShell background jobs in the current session.

SYNTAX
    Receive-Job [-Job] <Job[]> [[-ComputerName] <string[]>] [-Keep] [-NoRecurse] [<CommonParameters>]

    Receive-Job [[-InstanceId] <Guid[]>] [-Keep] [-NoRecurse] [<CommonParameters>]

    Receive-Job [-Job] <Job[]> [[-Location] <string[]>] [-Keep] [-NoRecurse] [<CommonParameters>]

    Receive-Job [[-Name] <string[]>] [-Keep] [-NoRecurse] [<CommonParameters>]

    Receive-Job [-Job] <Job[]> [[-Session] <PSSession[]>] [-Keep] [-NoRecurse] [<CommonParameters>]

    Receive-Job [-Id] <Int32[]> [-Keep] [-NoRecurse] [<CommonParameters>]

DESCRIPTION
    The Receive-Job cmdlet gets the results of Windows Powershell background jobs. Use Receive-Job to get the results of jobs started by using the Start-Job cmdlet or the AsJob parameter of any cmdlet. You can get the results of all jobs or identify jobs by their name, ID, instance ID, computer name, location, or session, or by submitting a job object.

    When you start a Windows PowerShell background job, the job starts, but the results do not appear immediately. Instead, the command returns an object that represents the background job. The job object contains useful information about the job, but it does not contain the results. This method allows you to continue working in the session while the job runs. For more information about background jobs in Windows PowerShell, see about_jobs.

    To get the results of the command, use the Receive-Job cmdlet. Receive-Job gets the results that have been generated by the time that the Receive-Job command is submitted. If the results are not yet complete, you can run additional Receive-Job commands to get the remaining results.

    By default, job results are deleted from the system when you receive them, but you can use the Keep parameter to save the results so that you can receive them again. To delete the job results, receive them again (without the Keep parameter), close the session, or use the Remove-Job cmdlet to delete the job from the session.

PARAMETERS
    -ComputerName <string[]>
        Gets the results of jobs that were run on the specified computers. Enter the computer names. The default is all jobs in the current session.

        This parameter selects from among the job results that are stored on the local computer. It does not get data from remote computers. To get job results that are stored on remote computers, use the Invoke-Command cmdlet to run a Receive-Job command remotely.

        Required?                    false
        Position?                    2
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -Id <Int32[]>
        Gets the results of jobs with the specified IDs. The default is all jobs in the current session.

        The ID is an integer that uniquely identifies the job within the current session. It is easier to remember and type than the instance ID, but it is unique only within the current session. You can type one or more IDs (separated by commas). To find the ID of a job, type “Get-Job” without parameters.

        Required?                    true
        Position?                    1
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -InstanceId <Guid[]>
        Gets the results of jobs with the specified instance IDs. The default is all jobs in the current session.

        An instance ID is a GUID that uniquely identifies the job on the computer. To find the instance ID of a job, use the Get-Job cmdlet.

        Required?                    false
        Position?                    1
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -Job <Job[]>
        Specifies the job for which results are being retrieved. This parameter is required in a Receive-Job command. Enter a Variable that contains the job or a command that gets the job. You can also pipe a job object to Receive-Job.

        Required?                    true
        Position?                    1
        Default value
        Accept pipeline input?     true (ByValue, ByPropertyName)
        Accept wildcard characters? false

    -Keep [<SwitchParameter>]
        Saves the job results in the system, even after you have received them. By default, the job results are deleted when they are retrieved.

        To delete the results, use Receive-Job to receive them again without the Keep parameter, close the session, or use the Remove-Job cmdlet to delete the job from the session.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Location <string[]>
        Gets only the results of jobs with the specified location. The default is all jobs in the current session.

        Required?                    false
        Position?                    2
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -Name <string[]>
        Gets the results of jobs with the specified friendly name. The default is all jobs in the current session.

        Required?                    false
        Position?                    1
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -NoRecurse [<SwitchParameter>]
        Gets results only from the specified job. By default, Receive-Job also gets the results of all child jobs of the specified job.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Session <PSSession[]>
        Gets the results of jobs that were run in the specified Windows Powershell session (PSSession). Enter a Variable that contains the PSSession or a command that gets the PSSession, such as a Get-PSSession command. The default is all jobs in the current session.

        Required?                    false
        Position?                    2
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    System.Management.Automation.Job
        You can pipe job objects to Receive-Job.

OUTPUTS
    PSObject
        Receive-Job returns the results of the commands in the job.

NOTES

    ————————– EXAMPLE 1 ————————–

    C:\PS>$job = Start-Job -scriptblock {Get-Process}

    C:\PS> Receive-Job -Job $job

    Description
    ———–
    These commands use the Job parameter to get the results of a particular job. The first command uses the Start-Job cmdlet to start a job that runs a “Get-Process” command. The command uses the assignment operator (=) to save the resulting job object in the $job Variable.

    The second command uses the Receive-Job cmdlet to get the results of the job. It uses the Job parameter to specify the job.

    ————————– EXAMPLE 2 ————————–

    C:\PS>$job = Start-Job -scriptblock {Get-Process}

    C:\PS> $job | Receive-Job

    Description
    ———–
    This example is the same as Example 2, except that the command uses a pipeline operator (|) to send the job object to Receive-Job. As a result, the command does not need a Job parameter to specify the job.

    ————————– EXAMPLE 3 ————————–

    C:\PS>$j = Invoke-Command -computername Server01, Server02, Server03 -scriptblock {Get-Service} -AsJob

    C:\PS> $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

    C:\PS> 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

    Description
    ———–
    These commands use the Name parameter of Receive-Job to get the results of one of several background jobs run on remote computers.

    The first command uses the Invoke-Command cmdlet to start a background job that runs a Get-Service command on three remote computers. The command uses the AsJob parameter to run the command as a background job. The command saves the resulting job object in the $j Variable.

    When you use the AsJob parameter of Invoke-Command to start a job, the job object is created on the local computer, even though the job runs on the remote computers. As a result, you use local commands to manage the job.

    Also, when you use AsJob, Windows PowerShell returns one job object that contains a child job for each job that was started. In this case, the job object contains three child jobs, one for each job on each remote computer.

    The second command uses the dot method to 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.

    The third command uses the Receive-Job cmdlet to get the results of the Job3 child job that ran on the Server02 computer. It uses the Name parameter to specify the name of the child job and the Keep parameter to save the job results even after they are received.

    ————————– EXAMPLE 4 ————————–

    C:\PS>$s = New-PSSession -computername Server01, Server02, Server03

    C:\PS> $j = Invoke-Command -session $s -scriptblock {Start-Job -scriptblock {Get-Eventlog -logname system}}

    C:\PS> $j

    Id Name     State     HasMoreData Location Command
    — —-     —–     ———– ——– ——-
    1    Job1     Completed True         Localhost Get-Eventlog system
    2    Job2     Completed True         Localhost Get-Eventlog system
    3    Job3     Completed True         Localhost Get-Eventlog system

    C:\PS> $results = Invoke-Command -session $s -scriptblock {param($j) Receive-Job -Job $j} -ArgumentList $j

    Description
    ———–
    This example shows how to get the results of background jobs run on three remote computers.

    The first command uses the New-PSSession cmdlet to create three PSSessions, one on each of the servers specified in the command. It saves the PSSessions in the $s Variable.

    The second command uses the Invoke-Command cmdlet to run a Start-Job command in each of the PSSessions in the $s Variable. The job runs a Get-Eventlog command that gets the events in the System log. The command saves the results in the $j Variable.

    Because the command used Invoke-Command to run the Start-Job command, the command actually started three independent jobs on each of the three computers. As a result, the command returned three job objects representing three jobs run locally on three different computers.

    The third command displays the three job objects in $j.

    The fourth command uses Invoke-Command to run a Receive-Job command in each of the PSSessions in $s and save the results in the $results Variable.

    Because $j is a local Variable, the script block uses the “param” keyword to declare the Variables in the command and the ArgumentList parameter to supply the value of $j.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113372
    about_jobs
    about_job_details
    about_remote_Jobs
    Start-Job
    Get-Job
    Wait-Job
    Stop-Job
    Remove-Job
    Invoke-Command