PowerShell中的进度条

本文介绍PowerShell中Write-Progress cmdlet的使用技巧,包括如何通过为活动分配ID并将其作为子活动的ParentID来增强进度条功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PowerShell has a thousand nice features, but one of the nicer ones that I end up using all the time is the built in Write-Progress cmdlet. Shady the Intern came by today with a PowerShell script that printed dots to report progress, like:

PowerShell具有上千种出色的功能,但是我一直使用的更好的功能之一是内置的Write-Progress cmdlet。 Shady the Intern到今天有了一个PowerShell脚本,该脚本打印了点以报告进度,例如:

Doing some stuff........................

I recommend he switch to Write-Progress. A nice feature of Write-Progress that I don't see used enough is the assigning of IDs to Activities, and then referencing those IDs as ParentIDs when writing out the progress of a Child Activity.

我建议他切换到Write-Progress。 我认为使用进度不足的一个不错的功能是给活动分配ID,然后在写出子活动的进度时将这些ID称为ParentID。

for ($i = 1; $i -le 10; $i++) 
{
  write-progress -id 1 -activity "Doing some stuff" -status "whatever" -percentComplete ($i*10);
   sleep 1; 
   for ($j = 1; $j -le 10; $j++)
   {
      write-progress -id 2 -parentId 1 -activity "Doing some child stuff" -status "yay" -percentComplete ($j*10)
      sleep 0.75
   }
}
PowerShell Progress

Here's a trivial example. Two for loops, each sleeping for a bit. The second for is a child of the first. Notice that ID of first loop is -activity 1 and the second references that activity via -parentActivity. This really adds, in my opinion, to the fit and finish of any script.

这是一个简单的例子。 两个for循环,每个循环睡一会儿。 第二个是第一个孩子。 注意,第一个循环的ID为-activity 1,第二个循环通过-parentActivity引用该活动。 在我看来,这确实增加了任何脚本的适合度和完成度。

翻译自: https://www.hanselman.com/blog/progress-bars-in-powershell

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值