0
votes

I'm trying to kill a secondary task of a process using powershell, batch, python...anything I can save as script and run it remotely. TaskManager picture as following:

TaskManager

I'd like to kill the one with longer title leaving the "SAP Logon 740" open. Every task of the tree have the same PID, so I can't just kill the process.

I guess this is posible, because I can do it manually going to Task MAnager, expanding the process and ending that specific task but everything I've found consist in killing the process, which isn't possible in my case.

I've so far tried with tasklist/taskkill, powershell (Get-Process, Get-Object Win32_Process...) but I haven't been able to find how to.

Here you have the output of TaskList (Status=Running)

Output

Only one of the task (the one which is front) is showing there.

1
Your image is showing Task Manager's grouping of individual tasks under their running parent app, this does not mean that they're sharing the same process identifier. Your GUI screenshot has no reason to be part of this question, what would be more useful to us would be the representative formatted output from TaskList /V /Fi "Status Eq Running". - Compo
added in the question - iVrLx
You'll notice that the one you've not shown does not have a PID of 2612, as I've already stated. Now open a Command Prompt window, enter TaskKill /? and read the usage information. You'll notice that you can filter its selections using information such as its Image Name and Window Title! - Compo
If I try to use Taskkill /F /FI "WINDOWTITLE eq ABAP*"it closes both windows, the one with windowtitle ABAP* and the one with SAP LOGON 740. - iVrLx

1 Answers

0
votes

As you have used the tag, and even ran your tasklist command using powerhell.exe, I have decided to provide an examples using it.

If your criteria is to stop the process named saplogon with the longest window title string:

GPs saplogon|Sort{$_.mainWindowTitle.Length}|Select -L 1|SpPs -Wh

If your criteria is to stop all processes named saplogon except for the one with the shortest window title string:

GPs saplogon|Sort{$_.mainWindowTitle.Length}|Select -Skip 1|SpPs -Wh`

If you're happy with the output, you can remove  -Wh, (-WhatIf), to actually perform the task. If needed you could even replace that with the  -F, (-Force) option, if necessary.