从数组启动 - 获取计数器路径的作业

时间:2012-08-15 00:23:59

标签: powershell performancecounter

作为我的last question的后续内容,我想编译一个perfmon计数器列表,这些计数器以1秒的间隔(默认)生成并连续采样(-ContinuousStart-Job ,睡眠60秒然后运行Receive-job以获得过去60秒的统计数据(-Average, -Sum, -Minimum, -Maximum)。

我现在遇到的问题是当我使用绝对计数器路径时,作业开始并保持“运行”状态。如果我尝试遍历一组性能计数器字符串,则作业状态将变为“已完成”。

以下是两个显示非工作和工作结果的代码示例。

不起作用。作业状态即使设置了“连续”也完成。没有错误。

$jobs=@{}
$counters=@("\Processor(*)\% Processor Time",
        "\Network Interface(*)\Bytes Received/sec",
        "\Network Interface(*)\Bytes Sent/sec")
foreach ($counterPath in $counters) {
    $job=Start-Job {get-counter -Counter "$counterPath" -Continuous | foreach {$_.CounterSamples} }
    $jobs[$job.id]=$counterPath
}

按预期工作,但不允许多个计数器通过循环启动作业。

$jobs=@{}
$job=Start-Job {get-counter -Counter "\Processor(*)\% Processor Time" -Continuous | foreach {$_.CounterSamples} }
$jobs[$job.id]=$counter

结果输出

PS C:\Users\msnow> $jobs=@{}
[string]$counter="\Processor(*)\% Processor Time"
$job=Start-Job {get-counter -Counter "$counter" -Continuous | foreach {$_.CounterSamples} }
$jobs[$job.id]=$counter

__________________________________________________________________________________________________________________________
PS C:\Users\msnow> $jobs=@{}
$job=Start-Job {get-counter -Counter "\Processor(*)\% Processor Time" -Continuous | foreach {$_.CounterSamples} }
$jobs[$job.id]=$counter

__________________________________________________________________________________________________________________________
PS C:\Users\msnow> Get-Job

Id              Name            State      HasMoreData     Location             Command                  
--              ----            -----      -----------     --------             -------                  
1               Job1            Completed  True            localhost            get-counter -Counter "...
3               Job3            Running    True            localhost            get-counter -Counter "...
__________________________________________________________________________________________________________________________
PS C:\Users\msnow> receive-job -id 3 | measure CookedValue -sum -Average


Count    : 11466
Average  : 5.20268509822716
Sum      : 59653.9873362726
Maximum  : 
Minimum  : 
Property : CookedValue

1 个答案:

答案 0 :(得分:2)

我认为问题出在变量$ counter的范围内。 Start-job在其他运行空间中运行scriptblock,并且该变量不可见。您需要将其作为-argumentlist传递.try:

Start-Job {get-counter -Counter "$($args[0])" -Continuous | foreach {$_.CounterSamples} } -ArgumentList $counter

Start-Job {param($counter) ; get-counter -Counter "$($counter)" -Continuous | % {$_.CounterSamples} } -ArgumentList $counter