Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 971
Vippy Avatar asked Aug 31 '12 22:08

Vippy


1 Answers

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.

like image 123
Keith Hill Avatar answered Oct 12 '22 03:10

Keith Hill