PowerShell 技能连载 - 进度条技巧(第 4 部分)

由于广大用户的要求,这里提供了一段代码,演示如何使用嵌套进度条并显示每个任务的“真实”进度指示器:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$servers = 'dc-01', 'dc-02', 'msv3', 'msv4'
$ports = 80, 445, 5985

$counterServers = 0
$servers | ForEach-Object {
# increment server counter and calculate progress
$counterServers++
$percentServers = $counterServers * 100 / $servers.Count

$server = $_
Write-Progress -Activity 'Checking Servers' -Status $server -Id 1 -PercentComplete $percentServers

$counterPorts = 0
$ports | ForEach-Object {
# increment port counter and calculate progress
$counterPorts++
$percentPorts = $counterPorts * 100 / $ports.Count


$port = $_
Write-Progress -Activity 'Checking Port' -Status $port -Id 2 -PercentComplete $percentPorts

# here would be your code that performs some task, i.e. a port test:
Start-Sleep -Seconds 1
}
}

PowerShell 技能连载 - 进度条技巧(第 4 部分)

http://blog.vichamp.com/2023/05/19/progress-bar-tricks-part-4/

作者

吴波

发布于

2023-05-19

更新于

2023-05-22

许可协议

评论