2013-04-01 40 views
1

我有一組正在運行的作業。寫進度條直到獲取作業返回「正在運行」PowerShell

PS C:\vinith> Get-Job 

Id  Name   PSJobTypeName State   HasMoreData  Location    Command 
--  ----   ------------- -----   -----------  --------    ------- 
2  scvmm2012-vin BackgroundJob Running  True   localhost   Param... 
4  scom2012sp1-vin BackgroundJob Running  True   localhost   Param... 
6  scorch2012-vin BackgroundJob Running  True   localhost   Param... 
8  scsm2012sp1-vin BackgroundJob Running  True   localhost   Param... 
10  spfoundation BackgroundJob Running  True   localhost   Param... 

我希望有一個進度條顯示,直到作業正在運行,當作業狀態變爲在PowerShell中

+0

[這裏有一個類似的問題(http://stackoverflow.com/questions/ 4317438 /監視作業中的powershell會話從另一個powershell會話) –

回答

3

「完成」使用Write-Progress進度條應該說完成。使用Get-Job接收當前作業的數量。像這樣,

# Some dummy jobs for illustration purposes 
start-job -ScriptBlock { start-sleep -Seconds 5 } 
start-job -ScriptBlock { start-sleep -Seconds 10 } 
start-job -ScriptBlock { start-sleep -Seconds 15 } 
start-job -ScriptBlock { start-sleep -Seconds 20 } 
start-job -ScriptBlock { start-sleep -Seconds 25 } 

# Get all the running jobs 
$jobs = get-job | ? { $_.state -eq "running" } 
$total = $jobs.count 
$runningjobs = $jobs.count 

# Loop while there are running jobs 
while($runningjobs -gt 0) { 
    # Update progress based on how many jobs are done yet. 
    write-progress -activity "Events" -status "Progress:" ` 
    -percentcomplete (($total-$runningjobs)/$total*100) 

    # After updating the progress bar, get current job count 
    $runningjobs = (get-job | ? { $_.state -eq "running" }).Count 
} 
+0

你好馮,我試過了,但我沒有看到任何狀態-percentcomplete ?,它只是顯示爲空白,這裏是我開始的作業,Start-Job-ScriptBlock {Get-Process | ConvertTo-Json},然後運行上面的腳本來顯示進度,但是我沒有看到完成百分比的任何狀態 - >它沒有用小的「o」點前進,它只是空白 – PowerShell

+0

爲我工作。當然,一旦工作不再處於「運行」狀態,你將只能看到進步。 –

0

請使用您而框下面有進度條的值以及

while($runningjobs -gt 0) { 
# Update progress based on how many jobs are done yet. 
$percent=[math]::Round((($total-$runningjobs)/$total * 100),2) 
write-progress -activity "Starting Provisioning Modules Instances" -status "Progress: $percent%" -percentcomplete (($total-$runningjobs)/$total*100) 

# After updating the progress bar, get current job count 
$runningjobs = (get-job | ? { $_.state -eq "running" }).Count 
相關問題