试试这个。第一个文件是master.p1:
$parentId = 1
$childId = 2
Write-Progress -Id $parentId -Activity "Running master script" -Status "Step 1 of 3" -PercentComplete 0
.\slave.ps1 $parentId $childId
Write-Progress -Id $parentId -Activity "Running master script" -Status "Step 2 of 3" -PercentComplete 33.3
.\slave.ps1 $parentId $childId
Write-Progress -Id $parentId -Activity "Running master script" -Status "Step 3 of 3" -PercentComplete 66.3
.\slave.ps1 $parentId $childId
第二个文件是slave.ps1:
param([int32]$ProgressParentId, [int32]$progressId)
for($i = 0; $i -le 100; $i += 10)
{
Write-Progress -Id $progressId -ParentId $parentId `
-Activity "Running slave script" `
-Status "Processing $i" `
-CurrentOperation "CurrentOp $i" -PercentComplete $i
Start-Sleep -Milliseconds 500
}
Put those two files in the same dir and from PowerShell (or ISE) execute master.ps1. I have used this approach before to report progress of multiple phases across multiple scripts. The key is to pass the ParentId
of the top level progress to the child scripts so they can report progress in that same context. If you provide a unique Id
for each, they can get their own separate progress bar. Or just the same Id
everywhere to update a single progress bar.