retrieving cpu speed and remove } from output

蹲街弑〆低调 提交于 2019-12-24 12:33:56

问题


I'm trying to get cpu speed.

This is what I've done so far

$cpu = [string](get-wmiobject Win32_Processor | select name)
$($cpu.split("@")[-1]).trim()

and my output is

2.40GHz}

How can I remove "}" from my output without having to play with string functions? Is there a better way to achieve my goal? Thanks in advance


回答1:


PS > $p = Get-WmiObject Win32_Processor | Select-Object -ExpandProperty Name
PS > $p -replace '^.+@\s'
2.40GHz



回答2:


You know what ... I'am Unhappy !

Powershell gives objects ! objects contains informations, and guys you are still trying to manipulate strings

(get-wmiobject Win32_Processor).MaxClockSpeed

Gives the max CPU

After that you can give the string format you want

$cpuSpeed = ((get-wmiobject Win32_Processor).MaxClockSpeed)/1000
$cpuspeedstring = ("{0}Go" -f  $cpuspeed)



回答3:


split() and trim() are string functions, by the way.

You can replace }:

$($cpu.split("@")[-1]).trim() -replace '}',''

Addendum: Here's a simpler way.

$cpu = (get-wmiobject Win32_Processor).name.split(' ')[-1]

The } you were seeing was an artifact produced by casting the results of Select-Object (which creates an object) to a string. Instead you just take the name property directly, split on the space character instead and take the last segment of the string[].



来源:https://stackoverflow.com/questions/6657288/retrieving-cpu-speed-and-remove-from-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!