1 回答

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超9個(gè)贊
Boe Prox多年前就此做了一篇出色的文章。他還使用PoshRSJob模塊對此進(jìn)行了擴(kuò)展。使用這個(gè)模塊可以很容易地處理這個(gè)問題。
#Create a synchronized hashtable
$sync = [hashtable]::Synchronized(@{
Time = ''
Stop = $false
Updater = ''
})
#create 5 RSJobs
1..5 | Start-RSJob -ScriptBlock {
param($sync) #accept $sync as a param
$updater = [Guid]::NewGuid() #unique id per job
while(-not $sync.Stop) { #run until told not to
$sync.Time = Get-Date
$sync.Updater = $updater
start-sleep -Seconds 1
}
} -ArgumentList $sync #pass $sync as a param
運(yùn)行這個(gè)會(huì)產(chǎn)生 5 個(gè)工作:
Id Name State HasMoreData HasErrors Command
-- ---- ----- ----------- --------- -------
1 Job1 Running False False ...
2 Job2 Running False False ...
3 Job3 Running False False ...
4 Job4 Running False False ...
5 Job5 Running False False ...
然后您可以檢查父進(jìn)程中的 $sync 并看到它不斷地從這些作業(yè)中更新。請注意,這些不僅僅是字符串表示,而是完整的對象。
PS C:\> $sync
Name Value
---- -----
Time 4/22/2019 11:58:35 AM
Stop False
Updater 9ab28c51-2941-4866-a064-165b1ceca673
PS C:\> $sync
Name Value
---- -----
Time 4/22/2019 11:58:37 AM
Stop False
Updater 113e78a8-1774-4cdf-9638-7235109f0a0d
終止我們設(shè)置的作業(yè)$sync.Stop = $true
PS C:\> Get-RSJob
Id Name State HasMoreData HasErrors Command
-- ---- ----- ----------- --------- -------
1 Job1 Completed False False ...
2 Job2 Completed False False ...
3 Job3 Completed False False ...
4 Job4 Completed False False ...
5 Job5 Completed False False ...
- 1 回答
- 0 關(guān)注
- 115 瀏覽
添加回答
舉報(bào)