Write-Progress
在 PowerShell 命令窗口中显示进度栏。
语法
Write-Progress
[-Activity] <String>
[[-Status] <String>]
[[-Id] <Int32>]
[-PercentComplete <Int32>]
[-SecondsRemaining <Int32>]
[-CurrentOperation <String>]
[-ParentId <Int32>]
[-Completed]
[-SourceId <Int32>]
[<CommonParameters>]
说明
Write-Progress
cmdlet 在 PowerShell 命令窗口中显示进度栏,描述正在运行的命令或脚本的状态。 可以选择条形图反映的指示器以及进度栏上方和下方显示的文本。
示例
示例 1:显示“for”循环的进度
for ($i = 1; $i -le 100; $i++ ) {
Write-Progress -Activity "Search in Progress" -Status "$i% Complete:" -PercentComplete $i
Start-Sleep -Milliseconds 250
}
此命令显示从 1 到 100 的 for
循环的进度。
Write-Progress
cmdlet 包括状态栏标题 Activity
、状态行和变量 $i
(for
循环中的计数器),指示任务的相对完成性。
示例 2:显示嵌套“for”循环的进度
for($I = 0; $I -lt 10; $I++ ) {
$OuterLoopProgressParameters = @{
Activity = 'Updating'
Status = 'Progress->'
PercentComplete = $I * 10
CurrentOperation = 'OuterLoop'
}
Write-Progress @OuterLoopProgressParameters
for($j = 1; $j -lt 101; $j++ ) {
$InnerLoopProgressParameters = @{
ID = 1
Activity = 'Updating'
Status = 'Progress'
PercentComplete = $j
CurrentOperation = 'InnerLoop'
}
Write-Progress @InnerLoopProgressParameters
Start-Sleep -Milliseconds 25
}
}
Updating
Progress ->
[ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo]
OuterLoop
Updating
Progress
[oooooooooooooooooo ]
InnerLoop
此示例显示两个嵌套 for
循环的进度,每个循环由进度栏表示。
第二个进度栏的 Write-Progress
命令包括 ID 参数,该参数将其与第一个进度栏区区分开来。
如果没有 ID 参数,进度栏将相互叠加,而不是显示在另一个下方。
示例 3:在搜索字符串时显示进度
# Use Get-EventLog to get the events in the System log and store them in the $Events variable.
$Events = Get-EventLog -LogName System
# Pipe the events to the ForEach-Object cmdlet.
$Events | ForEach-Object -Begin {
# In the Begin block, use Clear-Host to clear the screen.
Clear-Host
# Set the $i counter variable to zero.
$i = 0
# Set the $out variable to an empty string.
$out = ""
} -Process {
# In the Process script block search the message property of each incoming object for "bios".
if($_.Message -like "*bios*")
{
# Append the matching message to the out variable.
$out=$out + $_.Message
}
# Increment the $i counter variable which is used to create the progress bar.
$i = $i+1
# Determine the completion percentage
$Completed = ($i/$Events.Count) * 100
# Use Write-Progress to output a progress bar.
# The Activity and Status parameters create the first and second lines of the progress bar
# heading, respectively.
Write-Progress -Activity "Searching Events" -Status "Progress:" -PercentComplete $Completed
} -End {
# Display the matching messages using the out variable.
$out
}
此命令显示命令的进度,用于在系统事件日志中查找字符串“bios”。
PercentComplete 参数值将已处理的事件数除以 $i
检索的事件总数 $Events.Count
,然后将结果乘以 100。
示例 4:显示嵌套进程的每个级别的进度
foreach ( $i in 1..10 ) {
Write-Progress -Id 0 "Step $i"
foreach ( $j in 1..10 ) {
Write-Progress -Id 1 -ParentId 0 "Step $i - Substep $j"
foreach ( $k in 1..10 ) {
Write-Progress -Id 2 -ParentId 1 "Step $i - Substep $j - iteration $k"
Start-Sleep -Milliseconds 150
}
}
}
Step 1
Processing
Step 1 - Substep 2
Processing
Step 1 - Substep 2 - Iteration 3
Processing
在此示例中,可以使用 ParentId 参数的缩进输出来显示每个步骤中的父子关系。
参数
-Activity
指定状态栏上方标题中的第一行文本。 此文本描述报告其进度的活动。
类型: | String |
Position: | 0 |
默认值: | None |
必需: | True |
接受管道输入: | False |
接受通配符: | False |
-Completed
指示进度栏是否可见。 如果省略此参数,Write-Progress
显示进度信息。
类型: | SwitchParameter |
Position: | Named |
默认值: | None |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-CurrentOperation
指定进度栏下方的文本行。 此文本描述当前正在执行的作。
类型: | String |
Position: | Named |
默认值: | None |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-Id
指定一个 ID,用于区分每个进度栏与其他进度栏。 在单个命令中创建多个进度栏时,请使用此参数。 如果进度条没有不同的 ID,它们将被叠加,而不是显示在序列中。 不允许负值。
类型: | Int32 |
Position: | 2 |
默认值: | None |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-ParentId
指定当前活动的父活动。 如果当前活动没有父活动,请使用该值 -1
。
类型: | Int32 |
Position: | Named |
默认值: | None |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-PercentComplete
指定已完成的活动百分比。 如果百分比完成未知或不适用,请使用该值 -1
。
类型: | Int32 |
Position: | Named |
默认值: | None |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-SecondsRemaining
指定在活动完成之前剩余的预计秒数。 如果剩余的秒数未知或不适用,请使用该值 -1
。
类型: | Int32 |
Position: | Named |
默认值: | None |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-SourceId
指定记录的源。 可以代替 ID,但不能与其他参数(如 ParentId)一起使用。
类型: | Int32 |
Position: | Named |
默认值: | None |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-Status
指定状态栏上方标题中的第二行文本。 此文本描述活动的当前状态。
类型: | String |
Position: | 1 |
默认值: | None |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
输入
None
不能通过管道将对象传递给此 cmdlet。
输出
None
此 cmdlet 不返回任何输出。
备注
如果未显示进度栏,请检查 $ProgressPreference
变量的值。 如果该值设置为 SilentlyContinue
,则不会显示进度栏。 有关 PowerShell 首选项的详细信息,请参阅 about_Preference_Variables。
cmdlet 的参数对应于 System.Management.Automation.ProgressRecord 类的属性。 有关详细信息,请参阅 ProgressRecord 类。