Write-Progress
在 PowerShell 命令視窗中顯示進度列。
語法
Default (預設值)
Write-Progress
[[-Activity] <String>]
[[-Status] <String>]
[[-Id] <Int32>]
[-PercentComplete <Int32>]
[-SecondsRemaining <Int32>]
[-CurrentOperation <String>]
[-ParentId <Int32>]
[-Completed]
[-SourceId <Int32>]
[<CommonParameters>]
Description
Write-Progress Cmdlet 會在 PowerShell 命令視窗中顯示進度列,描述執行中命令或腳本的狀態。 您可以選取列所反映的指標,以及進度列上方和下方顯示的文字。
PowerShell 7.2 新增了 $PSStyle 自動變數,用來控制 PowerShell 如何使用 ANSI 逸出序列顯示特定資訊。
$PSStyle.Progress 成員可讓您控制進度檢視列轉譯。
-
$PSStyle.Progress.Style- ANSI 字串設定轉譯樣式。 -
$PSStyle.Progress.MaxWidth- 設定檢視的最大寬度。 預設為120。 最小值為 18。 -
$PSStyle.Progress.View- 具有值、Minimal和Classic的列舉。Classic是現有的轉譯,沒有任何變更。Minimal是單行最小渲染。Minimal是預設值。
如需 $PSStyle的詳細資訊,請參閱 about_ANSI_Terminals.md。
備註
如果主機不支援虛擬終端機,$PSStyle.Progress.View 會自動設定為 Classic。
範例
範例 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' 循環的進度
$PSStyle.Progress.View = 'Classic'
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 = 'Inner Progress'
PercentComplete = $j
CurrentOperation = 'InnerLoop'
}
Write-Progress @InnerLoopProgressParameters
Start-Sleep -Milliseconds 25
}
}
Updating
Progress ->
[ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo]
OuterLoop
Updating
Inner Progress
[oooooooooooooooooo ]
InnerLoop
本範例會將進度檢視設定為 Classic,然後顯示兩個巢狀 for 循環的進度,每個迴圈都以進度列表示。
第二個進度列的 Write-Progress 命令包含 標識子 參數,可區分它與第一個進度列。
如果沒有 標識碼 參數,進度列會彼此迭加,而不是顯示在另一個下方。
備註
本範例會將進度檢視設定為 Classic,以顯示每個進度列 CurrentOperation 值。 當進度檢視設定為 Minimal時,不會顯示 CurrentOperation 值。
範例 3:搜尋字串時顯示進度
# Use Get-WinEvent to get the events in the System log and store them in the $Events variable.
$Events = Get-WinEvent -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:顯示巢狀進程每個層級的進度
$PSStyle.Progress.View = 'Classic'
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 |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | 0 |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Completed
指出進度列是否可見。 如果省略此參數,Write-Progress 會顯示進度資訊。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-CurrentOperation
指定 Classic 進度檢視中進度列下方的文字行。 此文字描述目前正在進行的作業。 當進度檢視設定為 Minimal時,此參數不會有任何作用。
參數屬性
| 類型: | String |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Id
指定標識碼,以區分每個進度列與其他進度列。 當您在單一命令中建立多個進度列時,請使用此參數。 如果進度列沒有不同的標識碼,則會加疊,而不是顯示在數列中。 不允許負值。
參數屬性
| 類型: | Int32 |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | 2 |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-ParentId
指定目前活動的父活動。 如果目前活動沒有父活動,請使用值 -1。
參數屬性
| 類型: | Int32 |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-PercentComplete
指定已完成的活動百分比。 如果百分比完成未知或不適用,請使用值 -1。
參數屬性
| 類型: | Int32 |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-SecondsRemaining
指定活動完成前的預計秒數。 如果剩餘的秒數未知或不適用,請使用值 -1。
參數屬性
| 類型: | Int32 |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-SourceId
指定記錄的來源。 您可以使用這個取代 識別碼,但不能與其他參數搭配使用,例如 ParentId。
參數屬性
| 類型: | Int32 |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Status
指定狀態列上方標題中的第二行文字。 此文字描述活動的目前狀態。
參數屬性
| 類型: | String |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | 1 |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
CommonParameters
此 Cmdlet 支援一般參數:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 如需詳細資訊,請參閱 about_CommonParameters。
輸入
None
您不能將物件透過管道傳送到此 Cmdlet。
輸出
None
此 Cmdlet 不會傳回任何輸出。
備註
如果進度列未出現,請檢查 $ProgressPreference 變數的值。 如果值設定為 SilentlyContinue,則不會顯示進度列。 如需 PowerShell 喜好設定的詳細資訊,請參閱 about_Preference_Variables。
Cmdlet 的參數會對應至 System.Management.Automation.ProgressRecord 類別的屬性。 如需詳細資訊,請參閱 ProgressRecord 類別。