Receive-Job
取得目前會話中 PowerShell 背景作業的結果。
語法
Location (預設值)
Receive-Job
[-Job] <Job[]>
[[-Location] <string[]>]
[-Keep]
[-NoRecurse]
[-Force]
[-Wait]
[-AutoRemoveJob]
[-WriteEvents]
[-WriteJobInResults]
[<CommonParameters>]
ComputerName
Receive-Job
[-Job] <Job[]>
[[-ComputerName] <string[]>]
[-Keep]
[-NoRecurse]
[-Force]
[-Wait]
[-AutoRemoveJob]
[-WriteEvents]
[-WriteJobInResults]
[<CommonParameters>]
Session
Receive-Job
[-Job] <Job[]>
[[-Session] <PSSession[]>]
[-Keep]
[-NoRecurse]
[-Force]
[-Wait]
[-AutoRemoveJob]
[-WriteEvents]
[-WriteJobInResults]
[<CommonParameters>]
NameParameterSet
Receive-Job
[-Name] <string[]>
[-Keep]
[-NoRecurse]
[-Force]
[-Wait]
[-AutoRemoveJob]
[-WriteEvents]
[-WriteJobInResults]
[<CommonParameters>]
InstanceIdParameterSet
Receive-Job
[-InstanceId] <guid[]>
[-Keep]
[-NoRecurse]
[-Force]
[-Wait]
[-AutoRemoveJob]
[-WriteEvents]
[-WriteJobInResults]
[<CommonParameters>]
SessionIdParameterSet
Receive-Job
[-Id] <int[]>
[-Keep]
[-NoRecurse]
[-Force]
[-Wait]
[-AutoRemoveJob]
[-WriteEvents]
[-WriteJobInResults]
[<CommonParameters>]
Description
cmdlet Receive-Job 會取得 PowerShell 背景作業的結果,例如使用 Start-Job 任何 cmdlet 或 AsJob 參數開始的任務。 你可以透過工作名稱、ID、實例ID、電腦名稱、位置或會話來取得所有工作結果,或透過提交工作物件來識別。
當你啟動一個 PowerShell 背景工作時,工作會啟動,但結果不會立刻顯示。 指令會回傳一個代表背景工作的物件。 工作物件包含關於工作的有用資訊,但不包含結果。 這種方法讓你在工作執行時繼續工作。 欲了解更多關於PowerShell背景工作,請參見 about_Jobs。
Receive-Job指令小程式會取得指令Receive-Job提交時所產生的結果。 如果結果還沒完成,你可以執行額外 Receive-Job 指令來取得剩餘的結果。
預設情況下,當你收到工作結果時會從系統中刪除,但你可以使用 「保留 」參數來儲存結果,以便日後能再次收到。 要刪除工作結果,請在沒有 Keep 參數的情況下再次執行Receive-Job指令、關閉工作階段,或使用 Remove-Job cmdlet 從工作階段刪除該工作。
從 PowerShell 3.0 Windows 開始,Receive-Job 也會取得自訂工作類型的結果,例如工作流程工作和排程工作實例。 要啟用 Receive-Job 自訂工作類型的結果,請在執行 Receive-Job 指令前,將支援該自訂工作類型的模組匯入會話中,方法是使用 Import-Module cmdlet 或在模組中取得 cmdlet。 關於特定自訂工作類型的資訊,請參閱該自訂工作類型功能的說明文件。
範例
範例一:為特定工作取得結果
$job = Start-Job -ScriptBlock {Get-Process}
Start-Sleep -Seconds 1
Receive-Job -Job $job
這些指令使用 JobReceive-Job 參數來取得特定工作的結果。
第一個指令會以 開始 Start-Job 工作,並將工作物件儲存在變 $job 數中。
第二個指令使用 Receive-Job cmdlet 來取得作業結果。
它使用 Job 參數來指定工作。
範例 2:使用 Keep 參數
$job = Start-Job -ScriptBlock {Get-Service dhcp, fakeservice}
Start-Sleep -Seconds 1
$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 ,並將工作管道傳送到 Receive-Job cmdlet。 該 -Keep 參數也用於允許在首次檢視後再次檢索所有彙整的串流資料。
範例三:取得多個背景工作結果
當你使用 AsJobInvoke-Command 參數啟動工作時,工作物件會在本地電腦上建立,儘管該工作是在遠端電腦上執行。 因此,你只能使用本地指令來管理這個工作。
另外,使用 AsJob 時,PowerShell 會回傳一個工作物件,裡面包含每個啟動工作中的子工作。 在此情況下,工作物件包含三個子工作,分別對應每台遠端電腦上的每個工作。
# 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 code 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.
$invokeCommandSplat = @{
Session = $s
ScriptBlock = {
Start-Job -Name $('MyJob-' +$Env:COMPUTERNAME) -ScriptBlock {
(Get-Date).ToString()
}
}
}
$j = Invoke-Command @invokeCommandSplat
# 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
這個範例展示了如何在三台遠端電腦上執行背景工作結果。 與前述範例不同,執行Invoke-CommandStart-Job指令實際上會在三台電腦上啟動三個獨立的工作。 因此,指令回傳了三個工作物件,代表三個在三台不同電腦上本地執行的作業。
範例五:存取兒童工作
參數 -Keep 會保留作業中聚合後串流的狀態,以便再次檢視。 若無此參數,所有彙總後的串流資料在收到工作時都會被抹除。
欲了解更多資訊,請參見 about_Job_Details
Note
聚合後的串流包含所有子工作的工作串流。 你仍然可以透過工作物件和子工作物件存取各個資料流。
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
表示此指令在返回工作結果後會刪除該工作。 如果該工作有更多結果,該工作仍會被刪除,但 Receive-Job 會顯示訊息。
這個參數只適用於自訂工作類型。 它設計用於儲存工作或工作外的作業類型,例如排程工作。
這個參數在沒有 等待 參數的情況下無法使用。
此參數是在 Windows PowerShell 3.0 中引進的。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | False |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-ComputerName
指定計算機名稱的陣列。
此參數從本地電腦中儲存的工作結果中選擇。 它不會取得遠端電腦執行的工作資料。 要取得儲存在遠端電腦上的作業結果,請使用 Invoke-Command 指令長(cmdlet)遠端執行 Receive-Job 指令。
參數屬性
| 類型: | String[] |
| 預設值: | All computers available |
| 支援萬用字元: | True |
| 不要顯示: | False |
| 別名: | Cn |
參數集
ComputerName
| Position: | 1 |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Force
表示若工作處於 暫停 或 斷開 狀態,該指令小程式會繼續等待。 預設情況下,當工作處於以下任一狀態時,等待Receive-Job參數會回傳或終止等待:
- 已完成
- 失敗
- 已停止
- 已暫停
- 斷線。
Force 參數僅在指令同時使用 Wait 參數時有效。
此參數是在 Windows PowerShell 3.0 中引進的。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | False |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Id
指定一組 ID 陣列。 此指令檔會取得具有指定 ID 的工作結果。
ID 是一個整數,唯一識別當前工作階段的工作。 它比實例 ID 更容易記憶和輸入,但它只在當前會話中唯一。 你可以用逗號分隔一個或多個識別碼。 要找到職缺的ID,請使用 Get-Job。
參數屬性
| 類型: | Int32[] |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
SessionIdParameterSet
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-InstanceId
指定一組實例 ID 陣列。 此指令檔會取得具有指定實例 ID 的工作結果。
實例 ID 是一個 GUID,唯一識別電腦上的工作。 要找到作業的實例 ID,請使用 Get-Job cmdlet。
參數屬性
| 類型: | Guid[] |
| 預設值: | All instances |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
InstanceIdParameterSet
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Job
指定要檢索結果的作業。
輸入包含該工作的變數或取得該工作的指令。 你也可以將工作物件管道傳送到 Receive-Job。
參數屬性
| 類型: | Job[] |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
Location
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | True |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
Session
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | True |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
ComputerName
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | True |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Keep
表示此指令檔會將彙整後的串流資料保存在系統中,即使你已經收到。 預設情況下,經過 以 Receive-Job查看後,彙整後的串流資料會被刪除。
關閉會話或用 Remove-Job cmdlet 移除工作也會刪除彙整的串流資料。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | False |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Location
指定多個地點陣列。 此指令檔只會取得指定地點工作的結果。
參數屬性
| 類型: | String[] |
| 預設值: | All locations |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
Location
| Position: | 1 |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Name
指定一系列友善的名字。 這個指令子會取得擁有指定名稱的工作結果。 支援通配符。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | True |
| 不要顯示: | False |
參數集
NameParameterSet
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-NoRecurse
表示此指令檔僅從指定工作中取得結果。 預設情況下, Receive-Job 也取得該工作所有子工作結果。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | False |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Session
指定一個會話陣列。 此 cmdlet 取得在指定 PowerShell 會話(PSSession)中執行的工作結果。 輸入包含 PSSession 的變數,或是取得 PSSession 的指令,例如 Get-PSSession 指令。
參數屬性
| 類型: | |
| 預設值: | All sessions |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
Session
| Position: | 1 |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Wait
表示此 cmdlet 會抑制命令提示字元,直到所有工作結果都收到為止。 預設情況下,會 Receive-Job 立即回傳可用的結果。
預設情況下, 等待 參數會等待工作處於以下任一狀態:
- 已完成
- 失敗
- 已停止
- 已暫停
- 已中斷連線
若工作狀態為暫停或斷線,若要指示 等待參數繼續 等待,請同時使用 強制 參數與 等待 參數。
此參數是在 Windows PowerShell 3.0 中引進的。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-WriteEvents
表示此指令在等待工作完成時,會回報工作狀態的變化。
此參數僅在指令中使用 Wait 參數且省略 Keep 參數時有效。
此參數是在 Windows PowerShell 3.0 中引進的。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | False |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-WriteJobInResults
表示此指令檔會回傳工作物件,接著是結果。
此參數僅在指令中使用 Wait 參數且省略 Keep 參數時有效。
此參數是在 Windows PowerShell 3.0 中引進的。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | False |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
CommonParameters
此 cmdlet 支援常見參數:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 如需詳細資訊,請參閱 about_CommonParameters。
輸入
Job
你可以把工作物件管線到這個 cmdlet。
輸出
PSObject
此 cmdlet 會回傳工作中指令的結果。
備註
PowerShell 包含下列 Receive-Job的別名:
- 所有平臺:
rcjb