0

I want to see if mutliple active remote desktop sessions are running on my client PC using a batch script.

When I open my Task Manager I can see a (2) behind MSTSC.exe

I already used:

wmic process where name="mstsc.exe" | find "mstsc.exe" /c

But the result I get is 1 even if there are two remote desktop sessions active.

I wonder if anyone can help me with this challenge.

Compo
  • 36,585
  • 5
  • 27
  • 39
Batch man
  • 5
  • 3
  • Have you tried that command without piping the output? `wmic process where name="mstsc.exe"`. Now based on that output, what do you think `find /c` is looking at? – Compo Apr 12 '18 at 12:00
  • if you want to stay with `wmic` (see Compo's answer), then: `wmic process where name="mstsc.exe" get /value |find /c "Caption"` – Stephan Apr 12 '18 at 12:22

1 Answers1

0

Why not use a simpler method:

TaskList|Find /I /C "mstsc.exe"

To save the number as a variable in a batch file:

@For /F %%A In ('TaskList^|Find /I /C "mstsc.exe"') Do @Set "Num=%%A"
@Echo %Num%

If you still wished to use WMIC for the task then perhaps you could change your provided example to:

WMIC Process Where Name="mstsc.exe" Get Name | Find /I /C "mstsc.exe"
Compo
  • 36,585
  • 5
  • 27
  • 39
  • thanks for your quick response. unfortunately this was not the solution even though two remote desktop sessions are open it will indicate this as one program ( mstsc.exe ). But your methode/code was indeed better. – Batch man Apr 12 '18 at 13:48
  • @Batchman, which method sees the process only once, the `TaskList` version, the `WMIC` version or both? – Compo Apr 12 '18 at 14:17
  • WMIC sees the process once and the tasklist will display (2). – Batch man Apr 13 '18 at 13:59
  • @Batchman, If the TaskList displays `2` then my answer is correct, is it not? Do you just want me to remove the `WMIC` answer so that you can mark my answer as the accepted solution? – Compo Apr 13 '18 at 14:38