在运行Get-Counter性能跟踪时集成倒计时器

时间:2015-12-15 15:13:55

标签: powershell

在Get-Counter事件期间是否可以显示实时倒计时器?

以下代码运行时会出现一个计时器:

Get-Counter -Counter `
  "\Processor(_total)\% Processor Time" `
  -SampleInterval 1 -MaxSamples 120

我希望有一个进度指示器来显示收集性能数据需要多长时间。

1 个答案:

答案 0 :(得分:0)

新答案

你走了,抱歉我误解了。在这种方法中,我们将开始一项工作来运行Get-Counter操作。然后,我们将启动While循环,并迭代以更新进度条。因为我们知道这将花费大约120秒,所以我将计数器设置为在期末结束时大致填充。如果工作更快完成,我们将结束循环。

最后,运行Receive-Job以获取Get-Counter命令的结果。

Start-job -Name GettingCounters  -ScriptBlock { Get-Counter -Counter `
"\Processor(_total)\% Processor Time" `
  -SampleInterval 1 -MaxSamples 120
 }
$i = 0 
$duration = 120 
While ((get-job GettingCounters).State -eq 'Running'){


    #increment
    $i++ 

    #Round the numbers up for a nice output and then Write-Progress
    Write-Progress -Activity "Processing $user" -PercentComplete (($i/$duration)*100) -Status ("Gathering data...")
    start-sleep -Seconds 1 
    }

旧答案

Get-Counter用于获取性能监视器计数器,而不是您正在寻找的cmdlet。

显示超时消息或计数器的最简单方法是使用timeout cmd。

这将显示

Press any key to continue 10, 9 , 8 , 7

等等,直到0,直到动作继续。

如果这是您的目标,您还可以向用户显示进度条。这是一个简单的例子

    #sample range of numbers
$users = (1..13000)

#setting up base number
$i=0

ForEach ($user in $users){
    #increment
    $i++ 

    #Round the numbers up for a nice output and then Write-Progress
    Write-Progress -Activity "Processing $user" -PercentComplete (($i/$users.Count) * 100) -Status ("$i out of " + $users.Count +" completed "+[math]::Round((($i/$users.Count) * 100),2) +" %")
    }

最终结果如下:

enter image description here