0

I'm developing code for monitoring memory that a specific program is using and then, if memory goes too high, it's gonna kill the process.

I'm wondering how I could remove extra blank spaces backward and forward the result, and then put the content into a variable.

Right now, the code is working, but it brings me some blank spaces. I know it's because of the Format-Table -Hide function, but I don't know how to fix it.

Eg.: With Chrome process.

$Pmemory = Get-Process -Name chrome |
           Group-Object -Property ProcessName |
           Select @{n='Memory';e={'{0:N0}' -f (($_.Group|Measure-Object WorkingSet -Sum).Sum / 1MB)}} |
           ft -hide |
           Out-String

$Lmemory = 1000

After all, I'm gonna compare both values, but my main question is how to remove those blank spaces.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • 7
    *"how to remove those blank spaces"* By not having them in the first place. Replace `ft -Hide | Out-String` with `Select-Object -Expand Memory`. Only ever use `Format-*` cmdlet when you're presenting data to a user. NEVER when further processing of the data is intended/required. – Ansgar Wiechers Feb 28 '19 at 19:26
  • Thank so much. It works! – Guilherme borges Feb 28 '19 at 19:31

1 Answers1

0

You have to remove the Format-Table and Out-String from your code

Like this

$Pmemory = (Get-Process -Name chrome |
           Group-Object -Property ProcessName |
           Select @{n='Memory';e={'{0:N0}' -f (($_.Group|Measure-Object WorkingSet -Sum).Sum / 1MB)}}).Memory

$Lmemory = 1000

Output:

PS C:\Windows\system32> $Pmemory
1.968
Sergio Tanaka
  • 1,325
  • 1
  • 6
  • 18