0

I'm working on a script to monitor some performance issues in PowerShell. Here's the overview of what I want to do.

  • Check every Committed bytes in use every 60 seconds
  • If below 60%, log and sleep
  • If above 60%, log processes consuming large amounts of paged memory, alert and return to step 1

I'd like this to run infinitely (once the Until part completes start over) but I'm having trouble wrapping my brain around some of these looping concepts. From what I've read, I could possibly use break or continue but I'm just not sure what the most efficient way to do this would be.

Here is part of the code I'm using. If it matters, I'm using a function for the logging functionality and running this as a Job so it's in the background.

        Add-Type -AssemblyName System.Windows.Forms
    Do{
        $value = (Get-Counter -Counter "\Memory\% committed bytes in use").CounterSamples.CookedValue
        LogOnly -Text "Committed bytes in use is currently $($value)"
        Start-Sleep -Seconds 60
      }

    Until($value -gt 60)        
         LogOnly -Text "Commited bytes in use is currently $($value)."
         $Processes = Get-Process | Where {$_.PM -gt 500MB} | ForEach {LogOnly -Text "Processes above 300MB is $($_.ProcessName)"} 
         [System.Windows.Forms.MessageBox]::Show("Committed Memory % has increased to $value.", 'Memory Warning', '0', 'Warning') 

Thanks

1 Answer 1

2

This works fine for me:

while ( $true ) {
  $value = (Get-Counter -Counter "\Memory\% committed bytes in use").CounterSamples.CookedValue
  if ( $value -lt 60 ) {
    Write-Host "Committed bytes in use is under limit. Value is currently $value"
  }
  else {
    Write-Host "Commited byes in use is over limit. Value is currently $value"
  }
  Start-Sleep -Seconds 3
}
Sign up to request clarification or add additional context in comments.

7 Comments

Yeah I tried something like that and it works if the value is less than 60 but I also need to log and take other actions for when it is above 60 and I can't get that to work. I tried using Else{Do other stuff} but it just hangs and never runs that part.
See updated answer. The else clause will run if $value is greater than or equal to 60.
Yeah that's what I have but the else clause isn't running. Here's a simplified version of what I'm trying. While($True){ $value = (Get-Counter -Counter "\Memory\% committed bytes in use").CounterSamples.CookedValue If($value -lt 60){ Write-Host "Committed bytes in use is under limit. Value is currently $($value)"} } Else { Write-Host "Commited byes in use is over limit. Value is currently $($value)." }
I used your code to get $value and updated the answer. It works fine for me.
A good text editor such as Visual Studio Code and proper code formatting and indentation can help with these kinds of problems.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.