Cannot kill process with powershell (Stop-Process) - windows

I have a ps1 script (Windows 2008 R2) to kill process:
Stop-Process -Name "c_proc".
c_proc is a program that runs in console mode.
After running this script c_proc does not exit, it just keeps restarting in console mode.
Another way to kill c_proc is to kill conhost.exe but it didn't work either.
c_proc can exit when you type in its console windows "exit".
How can I send the message "exit" to the console window via PowerShell or cmd?
How can I kill c_proc?

This is the right code to kill the process c_proc. I found the parent process:
Stop-Process -Name "start"
Stop-Process -Name "c_proc"

Related

How to keep the PowerShell process alive after closing powershell terminal in windows

I need to run a PowerShell command in windows PowerShell, its running fine as expected. The problem is when I close the Windows PowerShell terminal, it kills the process, whereas I want the process to continue running forever.
Is there a way to close the terminal and have process working in the background.
Here is my command that I need to run:
.\start.ps1 -accountUuid '{XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXX}' `
-repositoryUuid '{XXXXXX-XXXXX-XXXX-XXXX-XXXXXXXXXX}' `
-runnerUuid '{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX}' `
-OAuthClientId XXXXXXXXXXXXXXXXX `
-OAuthClientSecret XXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXX `
-workingDirectory '..\temp'
You can try with the command Start-process with option -NoNewWindow to run the process in the background. Add all your arguments in -ArgumentList.
Start-Process -FilePath ".\start.ps1" -ArgumentList "-accountUuid ...", "-repositoryUuid ..." -NoNewWindow
More info here

How to close other PowerShell windows from script

I have a PowerShell script which has the following steps:
Opens another PowerShell window
Navigates to an angular projects directory
Runs a command to serve the project
Is there a way that I can close all other running PowerShell windows, but keep the currently running script and it's newly created window open? Following the changes, I would like it to behave like this:
Close all other PowerShell windows
Opens another PowerShell window
Navigates to an angular projects directory
Runs a command to serve the project
You could use Get-Process to enumerate all running powershell processes, then filter out the current one by comparing with the value of $PID, before piping the rest to Stop-Process:
Get-Process powershell |? Id -ne $PID |Stop-Process -Force
You can include the child process by ID if necessary:
$childProcess = Start-Process powershell $childProcArgs -PassThru
Get-Process powershell |? Id -notin #($PID;$childProcess.Id) |Stop-Process -Force
... although I would suggest simply killing all other powershell instances first, and then launch the child process after
Is there a way that I can close all other running PowerShell windows,
but keep the currently running script and it's newly created window
open?
$Previous = Get-process -Name *Powershell*;
{YOUR SCRIPT}
Stop-process $previous

How do I kill a process in Powershell

Say I'm running a python script that doesn't exit properly. The powershell console does not return control to me without having to close the shell and open a new one. With Bash I can simply press Control-C to kill any process. What am I missing here?
Windows 8.1 Powershell
To kill a process, you can use the command Stop-Process.
Or you can try Ctrl + Break shortcut.
Use the following stop-service <service> choose a flag like -force to force stop even if it has dependent services as well.
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/stop-service?view=powershell-5.1

Can Start-Process start a process and let it run even when script ends in TFS?

I'm using Start-Process cmdlet as part of a Powershell script being executed by the new scriptable TFS build system.
My issue is that I'm starting some executables from my Powershell script and once the build step ends, it kills started processes.
I've also also tried to use ProcessStartInfo directly and Start-Job with no luck.
When I run that script alone it ends, but it leaves the started processes opened.
Is there any way to solve this?
You can use start-Job:
Start-Job -ScriptBlock { start C:\Windows\notepad.exe }
After exiting the PS, Notepad is still open
Check this post for more info:
if you start a script using Start-Process, it will survive the shell
termination, but if you started it from a console window then it stays
bound to that window and closing the window will terminate the process

kill remote job initiated by invoke-command

what is the easy way to kill remote job initiated by invoke-command with background processes?
I kick remote PS script to run legacy exe file and need a way to kill that process.
I was going to use stop-job first but as I am reading it works only for local processes.
Then I was going to use Remove-job -force command but if I understood right, it cannot kill running process until it completes (again my remote ps script will start another process to run exe file).
I am reading I can kill a process using wmi terminate command but I do not know how to get PID of remote process (I cannot use name because I can kill processes from other users)
what is the best way to achieve my goal?
my script looks like that:
invoke-command -computername server1,server2,server3..etc -script-block {my.exe} -asjob
loop to wait for all processes to complete and report progress
check for CTRL-C press and kill all remote instances on {my.exe}
ok checked this morning from work and this works fine from a calling script:
get-job | remove-job -force
I was confused by MSDN doc which says:
When you stop a background job, Windows PowerShell **completes** all tasks that are pending in that job queue and then ends the job
Check out this answer, you can add the start process command to your script block and return the pid or save it to a text file to be reference later.
PowerShell - get process ID of called application

Resources