keep checking if a program is running.

0

I have a program that I need to Kill after it has started running, but I need it to wait until it starts running before it tries to kill it. I am new-ish to powershell and have been reading about recursion, but I think I am missing something.

When I run the below script, if the program is open, it immediately closes (Which is should) but if the program is not open, it will wait 15 seconds and then give me the error below. I was under the impression that a Do Until should recurse until true.

Thanks!

taskkill : ERROR: The process "Epicor.exe" not found.
At C:\Users\mkilgore\Desktop\Untitled2.ps1:8 char:1
+ taskkill /im Epicor.exe /f
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (ERROR: The proc...exe" not found.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError

 

Start Script:

Do { If ((Get-Process Epicor -ea SilentlyContinue) -eq $False) {
Start-Sleep -s 15}

Else { $Epicor = "True" }
}

Until ( $Epicor -eq "True" )
taskkill /im Epicor.exe /f

Cancel
login to comment
0

Recursion is typically when a function calls itself. Loops are not recursion.

On my computer the output of Get-Process was never $False. I recommend checking for $null instead.

Here's an untested script I threw together that I think should work.

function Stop-Epicor {

$Epicor_Process = Get-Process Epicor -ErrorAction SilentlyContinue

if ( $Epicor_Process -eq $null ) {

Start-Sleep -Seconds 15
Stop-Epicor

} else {

Stop-Process -Force $Epicor_Process

}

}

Stop-Epicor
Cancel
login to comment

0
Do {

    Out-Null

}

While (!(Get-Process | Where {$_.Name -like "epicor*"}))

Get-Process | Where { $_.Name -like "epicor*"} | Stop-Process

  That should get you where you want to go. It'll run on the workstation until the process is running, then kill it and exit the script. If you need it to just keep looping and never terminate, then we need to make some adjustments, but this should do the trick. 

Cancel
login to comment

Reply