英文:
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 = "<PID of running instance of a program>"
$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)}}
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) }
如果您想要对象,但希望使用您选择的属性名称,那么请向您的计算属性添加Name
或Label
键:
$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 = 'myPropertyName'; Expression = { [math]::Round($_.CookedValue / $CpuCores, 2) }}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论