开发者

retrieving cpu speed and remove } from output

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}

开发者_如何学GoHow 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


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


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)


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[].

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜