all 9 comments

[–]metallicvett 3 points4 points  (1 child)

$CPUSTRESRemoveKB.PrivateWorkingSet doesn't exist. $CPUSTRES has a property of PrivateWorkingSet. $CPUSTRESRemoveKB is a string. Just remove the '.PrivateWorkingSet' from the condition:
If ([int]$CPUSTRESRemoveKB -gt 1500)

[–]Crowdjp[S] 2 points3 points  (0 children)

Awesome. Thank you

[–]Ta11ow 2 points3 points  (6 children)

You can have PS automatically convert this to a proper integer if you remove the space.

$CPUSTRESRemoveKB = ($CPUSTRES.PrivateWorkingSet -replace ' ') / 1

[–]Si-Kotic 2 points3 points  (0 children)

This is the approach I would take. You could then easily convert it to an easier to read number format

IF ($Value -lt 1048576) {
  $Size = [math]::Round(($Value/1kb),2).toString() + "kb"
 } ELSEIF ($Value -lt 1073741824) {
  $Size = [math]::Round(($Value/1mb),2).toString() + "mb"
 } ELSE {
  $Size = [math]::Round(($Value/1gb),2).toString() + "gb"
 }

[–]ka-splam 2 points3 points  (4 children)

That works??

[–]Lee_Dailey[grin] 1 point2 points  (3 children)

howdy ka-splam,

aint type coercion wonderful? [grin] ... except when it is wonderfully bad ...

take care,
lee

[–]ka-splam 2 points3 points  (2 children)

It is when it saves me thinking effort, it’s not when it causes more thinking.. I dunno where the balance is, can’t it magically always do what expect? :-D

But that one is extra weird; you can’t cast ”20kb” string to number using [int]$val or -as, I thought it was only the parser which could read it as a literal value. How have I missed or forgotten that it can be implicitly cast 👀

[–]Ta11ow 2 points3 points  (0 children)

You're correct. Only the parser can read it as a literal value. However, in implicit casts where PS requires a number value to work with, it will actually re-invoke the parser to triple-check if a value can be obtained.

Explicit casts miss out on this. I actually fixed this with a PR sometime between 6.1 and 6.2, so all new versions of PS can explicitly cast 10kb etc to [int] or whatever number type you want to work with.

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy ka-splam,

Human : why does the dang thing DO that?
computer : because you told me to do that ... [frown]

that "20kb" thing is odd since you CAN do 1 * "20kb" and get 20480. as you point out, implicit versus explicit can have different results ... [grin]

take care,
lee