1

作为我最后一个问题的后续行动,我想编译一个 perfmon 计数器列表,这些计数器-Continuous以 1 秒间隔(默认)生成并连续采样()Start-Job,睡眠 60 秒,然后运行Receive-job以获取过去 60 秒的统计信息( -Average, -Sum, -Minimum, -Maximum)。

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

下面是两个代码示例,它们显示了非工作和工作结果。

不工作。即使设置了 -Continuous,作业状态也会完成。不会引发错误。

$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
4

1 回答 1

2

我相信问题出在变量 $counter 的范围内。Start-job 在其他运行空间中运行脚本块并且它不可见的变量。您需要将其作为 -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
于 2012-08-15T00:50:41.387 回答