Erase all previous process
When you create a scheduled task, sometimes and old task is still running or already launched even if the task is stopped. To stop all previous process, I created a little function 🙂 :
function stopprocess($processname){
$wpids=$null
$pids=$null
$a=(Get-WmiObject -class win32_process | where{$_.ProcessName -eq $processname})
$a|foreach-object{
$processid=$_.processid
$process=(Get-WmiObject -class win32_process | where{$_.ProcessID -eq $processid}).getowner()
$user=$process.user
If($user -eq “svc-one-mpmadmin”){
If($pids -eq $null){
$pids=$processid
}
If($processid -gt $pids){
$pids=$processid
}
$wpids+=”/” + $processid
}
}
$wpidslength=$wpids.length – 1
$wpids=$wpids.substring(1,$wpidslength)
$wpids.split(“/”)|
where{$_ -ne $pids}|
foreach-object{
Stop-Process -id $_
}
}
To use that function, just write :
stopprocess “ping.exe”
This function will stop all process based on the ProcessID. Only the latest PID will left.