问题描述:

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

网友答案:

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
}
相关阅读:
Top