Advertisement
nidaren

Untitled

Apr 13th, 2025
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # === CONFIGURATION ===
  2. $exePath = "Your\Path\Here"   # Full path to the specific .exe
  3. $threshold = 10                       # CPU % threshold
  4. $duration = 20                       # Seconds of low CPU required to kill
  5. $intervalMs = 1000                    # Interval in milliseconds
  6.  
  7. $lowCpuCount = 0
  8.  
  9. # Helper: find process by full exe path
  10. function Get-TargetProcess {
  11.     return Get-Process | Where-Object {
  12.         $_.Path -and ($_.Path -ieq $exePath)
  13.     } | Select-Object -First 1
  14. }
  15.  
  16. Write-Host "Monitoring processes with path: $exePath"
  17.  
  18. # Timer setup
  19. $stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
  20.  
  21. while ($true) {
  22.     # Get the target process
  23.     $targetProc = Get-TargetProcess
  24.  
  25.     if (-not $targetProc) {
  26.         Write-Host "No process found with path: $exePath"
  27.         Start-Sleep -Seconds 2  # Delay before trying again
  28.         continue
  29.     }
  30.  
  31.     $targetPid = $targetProc.Id
  32.     $processName = $targetProc.Name
  33.  
  34.     Write-Host "`nMonitoring process: $exePath (PID: $targetPid)"
  35.  
  36.     # Reset CPU counter
  37.     $lowCpuCount = 0
  38.  
  39.     while ($true) {
  40.         $loopStart = $stopwatch.ElapsedMilliseconds
  41.  
  42.         try {
  43.             $proc = Get-Process -Id $targetPid -ErrorAction Stop
  44.  
  45.             $counterPath = "\Process($processName)\% Processor Time"
  46.             $cpuRaw = (Get-Counter $counterPath).CounterSamples[0].CookedValue
  47.             $cores = (Get-CimInstance Win32_ComputerSystem).NumberOfLogicalProcessors
  48.             $cpu = [math]::Round($cpuRaw / $cores, 2)
  49.  
  50.             if ($cpu -lt $threshold) {
  51.                 $lowCpuCount++
  52.                 Write-Host "[$(Get-Date -f 'HH:mm:ss')] CPU LOW: $cpu%  ($lowCpuCount / $duration)"
  53.             } else {
  54.                 $lowCpuCount = 0
  55.                 Write-Host "[$(Get-Date -f 'HH:mm:ss')] CPU OK:  $cpu%"
  56.             }
  57.  
  58.             if ($lowCpuCount -ge $duration) {
  59.                 Write-Host "`nKilling process $processName (PID: $targetPid)..."
  60.                 Stop-Process -Id $targetPid -Force
  61.                 break  # Exit the inner while loop and continue monitoring for new processes
  62.  
  63.             }
  64.  
  65.         } catch {
  66.             Write-Host "Process not found. Searching for new process..."
  67.             break  # Process has exited, break and continue searching
  68.         }
  69.  
  70.         # Timing control: wait exactly 1000ms per loop
  71.         $loopElapsed = $stopwatch.ElapsedMilliseconds - $loopStart
  72.         $sleepTime = $intervalMs - $loopElapsed
  73.         if ($sleepTime -gt 0) {
  74.             Start-Sleep -Milliseconds $sleepTime
  75.         }
  76.     }
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement