Skip to content

Instantly share code, notes, and snippets.

@magicwenli
Last active December 14, 2022 12:16
Show Gist options
  • Save magicwenli/6afb6f34c60303918cfb4e7e5af89796 to your computer and use it in GitHub Desktop.
Save magicwenli/6afb6f34c60303918cfb4e7e5af89796 to your computer and use it in GitHub Desktop.
$process_name = "notepad.exe"
$time_minute_limit = 0.1
while ($true) {
$process = Get-Process | Where-Object { $_.Path -like "*$process_name" }
if ($process) {
$last_time = (New-TimeSpan -Start ($process).StartTime).TotalMinutes
Write-Output "$process_name is running, live time is $([math]::round($last_time,3)) minutes"
if ($last_time -gt $time_minute_limit) {
$process | Stop-Process -Force
Write-Output "Now is $(Get-Date) , tried to kill $($process.ProcessName) , last_time is $([math]::round($last_time,5)) minutes"
Write-Output "Process is alive: $(!$process.HasExited)"
}
Start-Sleep 5
}
Start-Sleep 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment