共用方式為


Get-Job

取得在目前會話中執行的PowerShell背景工作。

Syntax

Get-Job
   [-IncludeChildJob]
   [-ChildJobState <JobState>]
   [-HasMoreData <Boolean>]
   [-Before <DateTime>]
   [-After <DateTime>]
   [-Newest <Int32>]
   [[-Id] <Int32[]>]
   [<CommonParameters>]
Get-Job
   [-IncludeChildJob]
   [-ChildJobState <JobState>]
   [-HasMoreData <Boolean>]
   [-Before <DateTime>]
   [-After <DateTime>]
   [-Newest <Int32>]
   [-State] <JobState>
   [<CommonParameters>]
Get-Job
   [-IncludeChildJob]
   [-ChildJobState <JobState>]
   [-HasMoreData <Boolean>]
   [-Before <DateTime>]
   [-After <DateTime>]
   [-Newest <Int32>]
   [-Command <String[]>]
   [<CommonParameters>]
Get-Job
   [-IncludeChildJob]
   [-ChildJobState <JobState>]
   [-HasMoreData <Boolean>]
   [-Before <DateTime>]
   [-After <DateTime>]
   [-Newest <Int32>]
   [-InstanceId] <Guid[]>
   [<CommonParameters>]
Get-Job
   [-IncludeChildJob]
   [-ChildJobState <JobState>]
   [-HasMoreData <Boolean>]
   [-Before <DateTime>]
   [-After <DateTime>]
   [-Newest <Int32>]
   [-Name] <String[]>
   [<CommonParameters>]
Get-Job
   [-Filter] <Hashtable>
   [<CommonParameters>]

Description

Get-Job Cmdlet 會取得物件,這類物件代表已在目前工作階段中啟動的背景工作。 您可以使用 Get-Job 來取得使用 Start-Job Cmdlet 啟動的作業,或使用任何 Cmdlet 的 AsJob 參數。

如果沒有參數, Get-Job 命令會取得目前會話中的所有作業。 您可以使用 Get-Job 的參數取得特定工作。

Get-Job 傳回的工作物件包含關於工作的有用資訊,但是不包含工作結果。 若要取得結果,請使用 Receive-Job Cmdlet。

Windows PowerShell 背景作業是在背景中執行的命令,不需要與目前的會話互動。 一般而言,您會使用背景工作來執行複雜命令,需要很長的時間才能完成。 如需 Windows PowerShell 中背景工作的詳細資訊,請參閱 about_Jobs。

從 Windows PowerShell 3.0 開始,Get-Job Cmdlet 也可取得自訂工作類型 (例如工作流程工作和排程工作的執行個體)。 若要尋找工作的工作類型,可使用工作的 PSJobTypeName 屬性。

若要讓 Get-Job 取得自訂作業類型,請在執行 Get-Job 命令之前,先將支援自定義作業類型的模組匯入會話,方法是使用 Import-Module Cmdlet 或使用 或 取得模組中的 Cmdlet。 如需有關特定自訂工作類型的資訊,請參閱自訂工作類型功能的文件。

範例

範例 1:取得目前會話中啟動的所有背景工作

PS C:\> Get-Job

這個命令會取得目前工作階段中啟動的所有背景工作。 它不會包含在其他工作階段中建立的工作,即使這類工作是在本機電腦上執行也一樣。

範例 2:使用實例標識元停止作業

The first command uses the **Get-Job** cmdlet to get a job. It uses the *Name* parameter to identify the job. The command stores the job object that **Get-Job** returns in the $j variable. In this example, there is only one job with the specified name.
PS C:\> $j = Get-Job -Name Job1

The second command gets the **InstanceId** property of the object in the $j variable and stores it in the $ID variable.
PS C:\> $ID = $j.InstanceID

The third command displays the value of the $ID variable.
PS C:\> $ID

Guid
----
03c3232e-1d23-453b-a6f4-ed73c9e29d55

The fourth command uses Stop-Job cmdlet to stop the job. It uses the *InstanceId* parameter to identify the job and $ID variable to represent the instance ID of the job.
PS C:\> Stop-Job -InstanceId $ID

這些命令示範如何取得工作的執行個體識別碼,然後使用它來停止工作。 與工作名稱 (這不是唯一的) 不同,執行個體識別碼是唯一的。

範例 3:取得包含特定命令的作業

PS C:\> Get-Job -Command "*get-process*"

此命令會取得系統上包含 Get-Process 命令的作業。 命令使用 Get-JobCommand 參數限制抓取的工作。 此命令會使用通配符 (*) ,在命令字串中的任何位置取得 包含 Get-Process 命令的工作。

範例 4:使用管線取得包含特定命令的工作

PS C:\> "*get-process*" | Get-Job

如同上一個範例中的命令,此命令會取得系統上包含 Get-Process 命令的作業。 此命令使用管線運算子 (|) 以引號將字串傳送至 Get-Job Cmdlet。 它等同於前一個命令。

範例 5:取得尚未啟動的作業

PS C:\> Get-Job -State NotStarted

這個命令只會取得已建立但尚未啟動的工作。 這包括已排定在未來執行的工作以及尚未排程的工作。

範例 6:取得尚未指派名稱的工作

PS C:\> Get-Job -Name Job*

此命令會取得所有作業名稱開頭為作業的作業。 因為作業編號>是作業<的預設名稱,所以此命令會取得所有沒有明確指派名稱的作業。

範例 7:使用作業物件來代表命令中的作業

The first command uses the **Start-Job** cmdlet to start a background job that runs a **Get-Process** command on the local computer. The command uses the *Name* parameter of **Start-Job** to assign a friendly name to the job.
PS C:\> Start-Job -ScriptBlock {Get-Process} -Name MyJob

The second command uses Get-Job to get the job. It uses the *Name* parameter of **Get-Job** to identify the job. The command saves the resulting job object in the $j variable.
PS C:\> $j = Get-Job -Name MyJob

The third command displays the value of the job object in the $j variable. The value of the **State** property shows that the job is completed. The value of the **HasMoreData** property shows that there are results available from the job that have not yet been retrieved.
PS C:\> $j
Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
6      MyJob           BackgroundJob   Completed     True            localhost            Get-Process

The fourth command uses the **Receive-Job** cmdlet to get the results of the job. It uses the job object in the $j variable to represent the job. You can also use a pipeline operator to send a job object to **Receive-Job**.
PS C:\> Receive-Job -Job $j
Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    124       4    13572      12080    59            1140 audiodg
    783      16    11428      13636   100             548 CcmExec
     96       4     4252       3764    59            3856 ccmsetup
...

這個範例示範如何使用 Get-Job 取得工作物件,然後示範如何使用該工作物件來代表命令中的工作。

範例 8:取得所有作業,包括由不同方法啟動的作業

The first command uses the **Start-Job** cmdlet to start a job on the local computer.
PS C:\> Start-Job -ScriptBlock {Get-EventLog System}

The second command uses the *AsJob* parameter of the **Invoke-Command** cmdlet to start a job on the S1 computer. Even though the commands in the job run on the remote computer, the job object is created on the local computer, so you use local commands to manage the job.
PS C:\> Invoke-Command -ComputerName S1 -ScriptBlock {Get-EventLog System} -AsJob

The third command uses the **Invoke-Command** cmdlet to run a **Start-Job** command on the S2 computer. By using this method, the job object is created on the remote computer, so you use remote commands to manage the job.
PS C:\> Invoke-Command -ComputerName S2 -ScriptBlock {Start-Job -ScriptBlock {Get-EventLog System}}

The fourth command uses **Get-Job** to get the jobs stored on the local computer. The **PSJobTypeName** property of jobs, introduced in Windows PowerShell 3.0, shows that the local job started by using the **Start-Job** cmdlet is a background job and the job started in a remote session by using the **Invoke-Command** cmdlet is a remote job.
PS C:\> Get-Job
Id     Name       PSJobTypeName   State         HasMoreData     Location        Command
--     ----       -------------   -----         -----------     --------        -------
1      Job1       BackgroundJob   Running       True            localhost       Get-EventLog System
2      Job2       RemoteJob       Running       True            S1              Get-EventLog System

The fifth command uses **Invoke-Command** to run a **Get-Job** command on the S2 computer.The sample output shows the results of the Get-Job command. On the S2 computer, the job appears to be a local job. The computer name is localhost and the job type is a background job.For more information about how to run background jobs on remote computers, see about_Remote_Jobs.
PS C:\> Invoke-Command -ComputerName S2 -ScriptBlock {Start-Job -ScriptBlock {Get-EventLog System}}
Id    Name     PSJobTypeName  State      HasMoreData   Location   Command
--    ----     -------------  -----      -----------   -------    -------
4     Job4     BackgroundJob  Running    True          localhost  Get-Eventlog System

這個範例示範 Get-Job Cmdlet 可以取得目前會話中啟動的所有作業,即使它們是使用不同的方法啟動也一樣。

範例 9:調查失敗的工作

The first command uses the **Start-Job** cmdlet to start a job on the local computer. The job object that **Start-Job** returns shows that the job failed. The value of the **State** property is Failed.
PS C:\> Start-Job -ScriptBlock {Get-Process}
Id     Name       PSJobTypeName   State       HasMoreData     Location             Command
--     ----       -------------   -----       -----------     --------             -------
1      Job1       BackgroundJob   Failed      False           localhost            Get-Process

The second command uses the **Get-Job** cmdlet to get the job. The command uses the dot method to get the value of the **JobStateInfo** property of the object. It uses a pipeline operator to send the object in the **JobStateInfo** property to the Format-List cmdlet, which formats all of the properties of the object (*) in a list.The result of the **Format-List** command shows that the value of the **Reason** property of the job is blank.
PS C:\> (Get-Job).JobStateInfo | Format-List -Property *
State  : Failed
Reason :

The third command investigates more. It uses a **Get-Job** command to get the job and then uses a pipeline operator to send the whole job object to the **Format-List** cmdlet, which displays all of the properties of the job in a list.The display of all properties in the job object shows that the job contains a child job named Job2.
PS C:\> Get-Job | Format-List -Property *
HasMoreData   : False
StatusMessage :
Location      : localhost
Command       : get-process
JobStateInfo  : Failed
Finished      : System.Threading.ManualReset
EventInstanceId    : fb792295-1318-4f5d-8ac8-8a89c5261507
Id            : 1
Name          : Job1
ChildJobs     : {Job2}
Output        : {}
Error         : {}
Progress      : {}
Verbose       : {}
Debug         : {}
Warning       : {}
StateChanged  :

The fourth command uses **Get-Job** to get the job object that represents the Job2 child job. This is the job in which the command actually ran. It uses the dot method to get the **Reason** property of the **JobStateInfo** property.The result shows that the job failed because of an Access Denied error. In this case, the user forgot to use the Run as administrator option when starting Windows PowerShell.Because background jobs use the remoting features of Windows PowerShell, the computer must be configured for remoting to run a job, even when the job runs on the local computer.For information about requirements for remoting in Windows PowerShell, see about_Remote_Requirements. For troubleshooting tips, see about_Remote_Troubleshooting.
PS C:\> (Get-Job -Name job2).JobStateInfo.Reason
Connecting to remote server using WSManCreateShellEx api failed. The async callback gave the following error message: Access is denied.

此命令示範如何使用 Get-Job 傳回的作業對象來調查作業失敗的原因。 它也會示範如何取得每個工作的子工作。

範例 10:取得篩選的結果

The first command uses the **Workflow** keyword to create the WFProcess workflow.
PS C:\> Workflow WFProcess {Get-Process}

The second command uses the *AsJob* parameter of the WFProcess workflow to run the workflow as a background job. It uses the *JobName* parameter of the workflow to specify a name for the job, and the *PSPrivateMetadata* parameter of the workflow to specify a custom ID.
PS C:\> WFProcess -AsJob -JobName WFProcessJob -PSPrivateMetadata @{MyCustomId = 92107}

The third command uses the *Filter* parameter of **Get-Job** to get the job by custom ID that was specified in the *PSPrivateMetadata* parameter.
PS C:\> Get-Job -Filter @{MyCustomId = 92107}
Id     Name            State         HasMoreData     Location             Command
--     ----            -----         -----------     --------             -------
1      WFProcessJob    Completed     True            localhost            WFProcess

這個範例示範如何使用 Filter 參數取得工作流程工作。 Filter 參數 (在 Windows PowerShell 3.0 中引進) 只適用於自訂工作類型,例如,工作流程工作和已排程的工作。

範例 11:取得子作業的相關信息

The first command gets the jobs in the current session. The output includes a background job, a remote job and several instances of a scheduled job. The remote job, Job4, appears to have failed.
PS C:\> Get-Job
Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
2      Job2            BackgroundJob   Completed     True            localhost            .\Get-Archive.ps1
4      Job4            RemoteJob       Failed        True            Server01, Server02   .\Get-Archive.ps1
7      UpdateHelpJob   PSScheduledJob  Completed     True            localhost            Update-Help
8      UpdateHelpJob   PSScheduledJob  Completed     True            localhost            Update-Help
9      UpdateHelpJob   PSScheduledJob  Completed     True            localhost            Update-Help
10     UpdateHelpJob   PSScheduledJob  Completed     True            localhost            Update-Help

The second command uses the *IncludeChildJob* parameter of **Get-Job**. The output adds the child jobs of all jobs that have child jobs.In this case, the revised output shows that only the Job5 child job of Job4 failed.
PS C:\> Get-Job -IncludeChildJob
Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
2      Job2            BackgroundJob   Completed     True            localhost           .\Get-Archive.ps1
3      Job3                            Completed     True            localhost           .\Get-Archive.ps1
4      Job4            RemoteJob       Failed        True            Server01, Server02  .\Get-Archive.ps1
5      Job5                            Failed        False           Server01            .\Get-Archive.ps1
6      Job6                            Completed     True            Server02            .\Get-Archive.ps1
7      UpdateHelpJob   PSScheduledJob  Completed     True            localhost            Update-Help
8      UpdateHelpJob   PSScheduledJob  Completed     True            localhost            Update-Help
9      UpdateHelpJob   PSScheduledJob  Completed     True            localhost            Update-Help
10     UpdateHelpJob   PSScheduledJob  Completed     True            localhost            Update-Help

The third command uses the *ChildJobState* parameter with a value of Failed.The output includes all parent jobs and only the child jobs that failed.
PS C:\> Get-Job -Name Job4 -ChildJobState Failed
Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
2      Job2            BackgroundJob   Completed     True            localhost           .\Get-Archive.ps1
4      Job4            RemoteJob       Failed        True            Server01, Server02  .\Get-Archive.ps1
5      Job5                            Failed        False           Server01            .\Get-Archive.ps1
7      UpdateHelpJob   PSScheduledJob  Completed     True            localhost            Update-Help
8      UpdateHelpJob   PSScheduledJob  Completed     True            localhost            Update-Help
9      UpdateHelpJob   PSScheduledJob  Completed     True            localhost            Update-Help
10     UpdateHelpJob   PSScheduledJob  Completed     True            localhost            Update-Help

The fifth command uses the **JobStateInfo** property of jobs and its **Reason** property to discover why Job5 failed.
PS C:\> (Get-Job -Name Job5).JobStateInfo.Reason
Connecting to remote server Server01 failed with the following error message:
Access is denied.
For more information, see the about_Remote_Troubleshooting Help topic.

這個範例示範使用 Get-Job Cmdlet 的 IncludeChildJobChildJobState 參數的效果。

參數

-After

取得在指定日期和時間之後結束的已完成工作。 輸入 DateTime 物件,例如由 Get-Date Cmdlet 傳回的物件,或可以轉換成 DateTime 物件的字串,例如 Dec 1, 2012 2:00 AM11/06

這個參數只適用於含有 EndTime 屬性的自訂工作類型,例如,工作流程工作和已排程的工作。 它不適用於標準背景工作,例如使用 Start-Job Cmdlet 所建立的背景工作。 如需支援此參數的詳細資訊,請參閱工作類型的說明主題。

此參數是在 Windows PowerShell 3.0 引進。

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

-Before

取得在指定日期和時間之前結束的已完成工作。 輸入 DateTime 物件。

這個參數只適用於含有 EndTime 屬性的自訂工作類型,例如,工作流程工作和已排程的工作。 它不適用於標準背景工作,例如使用 Start-Job Cmdlet 所建立的背景工作。 如需支援此參數的詳細資訊,請參閱工作類型的說明主題。

此參數是在 Windows PowerShell 3.0 引進。

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

-ChildJobState

只取得具有指定狀態的子工作。 此參數可接受的值為:

  • NotStarted
  • 執行中
  • Completed
  • 失敗
  • 已停止
  • 封鎖
  • 暫止
  • 已中斷連接
  • Suspending
  • 停止中

根據預設,Get-Job 無法取得子工作。 使用 IncludeChildJob 參數, Get-Job 會取得所有子作業。 如果您使用 ChildJobState 參數,IncludeChildJob 參數就沒有任何作用。

此參數是在 Windows PowerShell 3.0 引進。

Type:JobState
Accepted values:NotStarted, Running, Completed, Failed, Stopped, Blocked, Suspended, Disconnected, Suspending, Stopping, AtBreakpoint
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Command

將命令陣列指定為字串。 此 Cmdlet 會取得包含指定命令的作業。 預設為所有工作。 您可以使用通配符來指定命令模式。

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

-Filter

指定條件的雜湊表。 此 Cmdlet 會取得滿足所有條件的作業。 輸入索引鍵為工作屬性且值為工作屬性值的雜湊表。

這個參數只適用於自訂工作類型,例如,工作流程工作和已排程的工作。 它不適用於標準背景工作,例如使用 Start-Job Cmdlet 建立的作業。 如需支援此參數的詳細資訊,請參閱工作類型的說明主題。

此參數是在 Windows PowerShell 3.0 引進。

Type:Hashtable
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-HasMoreData

指出此 Cmdlet 是否只取得具有指定 HasMoreData 屬性值的作業。 HasMoreData 屬性會指出是否已在目前工作階段中收到所有的工作結果。 若要取得具有更多結果的作業,請指定值 $True。 若要取得沒有更多結果的工作,請指定值 $False。

若要取得作業的結果,請使用 Receive-Job Cmdlet。

當您使用 Receive-Job Cmdlet 時,它會從其記憶體內部的會話特定記憶體中刪除它所傳回的結果。 當它傳回目前會話中作業的所有結果時,它會將作業的 HasMoreData 屬性值設定為 $False) ,表示目前會話中作業沒有任何結果。 使用 Receive-JobKeep 參數,來防止 Receive-Job 刪除結果以及變更 HasMoreData 屬性的值。 如需詳細資訊,請鍵入 Get-Help Receive-Job

HasMoreData 屬性是目前工作階段特定的屬性。 如果自定義作業類型的結果儲存在會話之外,例如排程的工作類型,這會在磁碟上儲存作業結果,您可以使用不同會話中的 Receive-Job Cmdlet 再次取得作業結果,即使 HasMoreData 的值$False也一樣。 如需詳細資訊,請參閱自訂工作類型的說明主題。

此參數是在 Windows PowerShell 3.0 引進。

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

-Id

指定這個 Cmdlet 取得之作業的標識子陣列。

識別碼是一個整數,可唯一識別目前工作階段中的工作。 記住和輸入比實例標識碼更容易,但只在目前的會話中是唯一的。 您可以輸入一或多個識別碼,並以逗號分隔。 若要尋找作業的識別碼,請輸入 Get-Job 不含參數的 。

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

-IncludeChildJob

指出除了父作業之外,此 Cmdlet 還會傳回子作業。

此參數特別適用於調查工作流程作業,其中 Get-Job 會傳回容器父作業和作業失敗,因為失敗的原因會儲存在子作業的 屬性中。

此參數是在 Windows PowerShell 3.0 引進。

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

-InstanceId

指定這個 Cmdlet 取得之作業實例識別碼的陣列。 預設為所有工作。

執行個體識別碼是 GUID,可唯一識別電腦上的工作。 若要尋找工作的執行個體識別碼,請使用Get-Job

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

-Name

指定這個 Cmdlet 取得之作業實例易記名稱的陣列。 輸入工作名稱,或使用萬用字元來輸入工作名稱模式。 根據預設,Get-Job 會取得目前工作階段中的所有工作。

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

-Newest

指定要取得的一些作業。 此 Cmdlet 會取得最近結束的工作。

Newest 參數不會依結束時間順序排序或傳回最新的工作。 若要排序輸出,請使用 Sort-Object Cmdlet。

此參數是在 Windows PowerShell 3.0 引進。

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

-State

指定工作狀態。 此 Cmdlet 只會取得處於指定狀態的作業。 此參數可接受的值為:

  • NotStarted
  • 執行中
  • Completed
  • 失敗
  • 已停止
  • 封鎖
  • 暫止
  • 已中斷連接
  • Suspending
  • 停止中

根據預設,Get-Job 會取得目前工作階段中的所有工作。

如需作業狀態的詳細資訊,請參閱 MSDN 連結庫中的 JobState 列舉

Type:JobState
Accepted values:NotStarted, Running, Completed, Failed, Stopped, Blocked, Suspended, Disconnected, Suspending, Stopping, AtBreakpoint
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

輸入

None

您無法使用管線傳送輸入至此 Cmdlet。

輸出

System.Management.Automation.RemotingJob

此 Cmdlet 會傳回代表會話中作業的物件。

備註

  • 工作的 PSJobTypeName 屬性會指出工作的工作類型。 屬性值是由工作類型作者所決定。 下列清單顯示常見的工作類型。

    • BackgroundJob。 使用 Start-Job 啟動的本機作業。

    • RemoteJob。 作業使用 Invoke-Command Cmdlet 的 AsJob 參數,在 PSSession 中啟動。

    • PSWorkflowJob。 使用工作流程 AsJob 一般參數啟動的工作。