Extract single property from Get-Counter object

huangapple go评论58阅读模式
英文:

Extract single property from Get-Counter object

问题

I can help you with the translation. Here's the translated code portion without the code:

你正在编写一个 PowerShell 脚本来进行性能监控,并希望以整数形式返回给定进程 PID 的百分比 CPU 使用率。我从 Get-Process applet 获取 PID(以及用户名以进行关联),然后将其与进程名称关联,然后获取 CPU 使用率。直到我的困扰的部分,代码如下:

$ProcessPID = "<运行程序的 PID>"
$ProcessName = (Get-Process -Id $ProcessPID).Name
$CpuCores = (Get-WMIObject Win32_ComputerSystem).NumberOfLogicalProcessors
$Samples = (Get-Counter "\Process($Processname*)% Processor Time").CounterSamples
$Samples | Select @{Expression={[Decimal]::Round(($_.CookedValue / $CpuCores), 2)}}

这段代码运行正常,返回类似以下的对象:

[Decimal]::Round(($_.CookedValue / $CpuCores), 2)
-------------------------------------------------

6.98

我需要的是仅返回值 '6.98'。

我尝试使用 $Samples.CookedValue(当我将对象传递到 'gm' 命令时显示为属性),将对象转换为字符串并使用 RexEx 提取整数部分(我使用在线正则表达式生成器确保表达式正确),但到目前为止都没有成功。整个对象总是被返回。所以,简而言之,我需要知道如何从 get-counter 对象中提取值。

英文:

I'm writing a PowerShell script to do performance monitoring and would like to return the Percent CPU for a given process PID as a whole number. I get the PID from the Get-Process applet (along with the username for correlation) and correlate that to the process name before getting the CPU percentage. The code up until my dilema is as follows:

$ProcessPID = &quot;&lt;PID of running instance of a program&gt;&quot;
$ProcessName = (Get-Process -Id $ProcessPID).Name
$CpuCores = (Get-WMIObject Win32_ComputerSystem).NumberOfLogicalProcessors
$Samples = (Get-Counter &quot;\Process($Processname*)\% Processor Time&quot;).CounterSamples
$Samples | Select @{Expression={[Decimal]::Round(($_.CookedValue / $CpuCores), 2)}}

This works fine and returns an object like this:

[Decimal]::Round(($_.CookedValue / $CpuCores), 2)
-------------------------------------------------

6.98

What I need is to return just the value '6.98'.

I've tried using $Samples.CookedValue (which shows as a property when I pipe the object to the 'gm' command), converting the object to a string and using RexEx to extract the whole number (I used an online regex builder to ensure the expression was correct) but nothing has worked thus far. The entire object is always returned. So, in a nutshell, I need to know how to extract just the value from the get-counter object.

答案1

得分: 2

Select-Object 创建新对象,对象具有属性和值,如果您想要单个值或值数组,那么不要使用它:

$Samples | ForEach-Object { [math]::Round($_.CookedValue / $CpuCores, 2) }

如果您想要对象,但希望使用您选择的属性名称,那么请向您的计算属性添加NameLabel

$Samples | Select-Object @{ Name = 'myPropertyName'; Expression = { [math]::Round($_.CookedValue / $CpuCores, 2) }}
英文:

Select-Object creates new objects and objects have properties and values, if you want a single value or an array of values then don't use it:

$Samples | ForEach-Object { [math]::Round($_.CookedValue / $CpuCores, 2) }

If you want to have objects but with a property name of your choice, then add the Name or Label key to your calculated property:

$Samples | Select-Object @{ Name = &#39;myPropertyName&#39;; Expression = { [math]::Round($_.CookedValue / $CpuCores, 2) }}

huangapple
  • 本文由 发表于 2023年4月11日 05:46:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980976.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定