[英]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. 好的,我已经看了几十个关于
Write-Progress
的例子,并观察了以下内容。 Most of the time used in conjuction with a loop, usually "for" or "foreach". 大部分时间与循环结合使用,通常是“for”或“foreach”。 Most examples don't do anything but count to 100. Other more useful examples will perform a certain command, such as copying files.
大多数示例不做任何事情,但计数为100.其他更有用的示例将执行某个命令,例如复制文件。 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
. 该脚本运行大约15分钟,在此期间我想使用
Write-Progress
提供状态。
My Question: 我的问题:
How can I use Write-Progress
to provide status while ALL my scripts are executing? 在我的所有脚本执行时,如何使用
Write-Progress
提供状态? 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. 基本上我想围绕我的所有脚本“包装”
Write-Progress
,或者从单个脚本中为多个被调用脚本提供状态的最佳方法。
Best Example: 最好的例子:
The best use of this I've seen thus far is when using the Update-Help
CmdLet in PowerShell V3. 到目前为止,我见过的最好的用法是在PowerShell V3中使用
Update-Help
CmdLet。 But since I cannot see the source-code for the Update-Help
CmdLet, this doesn't help me. 但由于我无法看到
Update-Help
CmdLet的源代码,这对我没有帮助。
Try this out. 试试吧。 First file is master.p1:
第一个文件是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: 第二个文件是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. 将这两个文件放在同一个目录中,并从PowerShell(或ISE)执行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. 关键是将顶级进度的
ParentId
传递给子脚本,以便它们可以在同一上下文中报告进度。 If you provide a unique Id
for each, they can get their own separate progress bar. 如果您为每个提供唯一的
Id
,他们可以获得自己的单独进度条。 Or just the same Id
everywhere to update a single progress bar. 或者只是相同的
Id
到处更新单个进度条。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.