简体   繁体   中英

PowerShell: Write-Progress for Progress on Multiple Scripts

My Research:

Okay, I have looked at dozens upon dozens of examples for Write-Progress and observed the following. Most of the time used in conjuction with a loop, usually "for" or "foreach". Most examples don't do anything but count to 100. Other more useful examples will perform a certain command, such as copying files. No examples envelope entire scripts.

My Setup:

I have several scripts (thousands of lines of code) which call each other at certain times. One single script controls or calls all the other scripts. The script runs for about 15 minutes, during that time I would like to provide status using Write-Progress .

My Question:

How can I use Write-Progress to provide status while ALL my scripts are executing? Basically I want to "wrap" Write-Progress around all my scripts or whatever is the best method for providing status for multiple called scripts from a single script.

Best Example:

The best use of this I've seen thus far is when using the Update-Help CmdLet in PowerShell V3. But since I cannot see the source-code for the Update-Help CmdLet, this doesn't help me.

Try this out. First file is 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

Second file is 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM