2

UPDATE:

Ok below is the current code I wrote up but it errors out saying ERROR: The RPC server is unavailable

If I manually just type in taskkill /s cn /im csmmviewer.exe within cmd it works, what I'm I missing here?

@echo off
:start
set /p "cn=Enter the computer name "
echo.
set /p "question=You will now close csmmviewer.exe on %cn% are you sure? [Y/N] "
echo.
if /I "%question%" EQU "Y" goto :run
if /I "%question%" EQU "N" goto :start
:run
taskkill /s cn /im csmmviewer.exe
set /p "again=Do you have another computer name? [Y/N] "
echo.
if /I "%again%" EQU "Y" goto :start
if /I "%again%" EQU "N" goto :exit

Hello noob here looking for some much needed help please.

At my job I run into this issue where too many people log into an application and when users that really need to use it can’t because too many people are logged into it (using up all the licenses) and I have to start kicking them off by killing the app remotely. The below command works every time but I have to manually copy and paste it into the command prompt and replace PCJXXXXX with the actual computer name. I will like to create a .bat file or .cmd file that I can just double click on which will then just ask to enter the computer name which I would just then enter and it runs the full command to end the task.

My script:

taskkill /s pcjXXXXX /u domain_name\admin /im csmmviewer.exe    

XXXXX is what I delete and enter the actual name, can anyone help, if you need more details please let me know your questions?

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • 1
    This is easy to do with `SET /P`. Use the command `SET /?` to see more information about using it. The way SO works is that you put your code into the question, describe what it is supposed to do and what it is doing. If you do not have any code and need to hire a programmer, you might try https://www.stackoverflowbusiness.com/talent – lit Jan 24 '18 at 21:20
  • [A to Z Index of Windows command line](https://ss64.com/nt/) – Squashman Jan 24 '18 at 21:34
  • How many computers in your network? How often do you have to this? – jwdonahue Jan 24 '18 at 22:27
  • thanks Squashman I'll start there and try it out – Cristian Luzbet Jan 24 '18 at 22:40
  • about 600 computers are in my network juwdonahue. On avg I do it about 2-3 times a week. – Cristian Luzbet Jan 24 '18 at 22:41
  • 1
    Have you asked the application vendor to timeout and disconnect inactive users? How do you determine which users to kill? – lit Jan 24 '18 at 22:56
  • So use `set /p` as was suggested by @lit. I was going to suggest an aliasing scheme that would let you just select a script named after the computer that you could just click on, but that's probably more work than it's worth. Another option would be remove the application from the users paths and make them launch it from a script that could then detect when the user is not present and kill its own child. But that's even more work, at least initially. – jwdonahue Jan 24 '18 at 23:59
  • @lit yes I asked but they refused because they want my job to purchase more licenses which my job doesn't want to pay more for, of course.I determine which to kill off because I know which users don't need to record compared to users who do need to record and are more important than users who just listen. thank you lit and jwdonahue for your input – Cristian Luzbet Jan 25 '18 at 01:41
  • There are lots of similar questions to this one on SO. https://stackoverflow.com/questions/1223721/in-windows-cmd-how-do-i-prompt-for-user-input-and-use-the-result-in-another-com is the closest I could find. – jwdonahue Jan 25 '18 at 03:59
  • Possible duplicate of [In Windows cmd, how do I prompt for user input and use the result in another command?](https://stackoverflow.com/questions/1223721/in-windows-cmd-how-do-i-prompt-for-user-input-and-use-the-result-in-another-com) – jwdonahue Jan 25 '18 at 04:00
  • @jwdonahue Cannot really see a link as a duplicate, if the link question does not have an accepted answer. – Gerhard Jan 25 '18 at 08:58

2 Answers2

1

You can use a parameter (like killap PCJ12345), when you call it from command line. %1 refers to the first parameter.

@echo off
if "%1" == "" (
  set /p "PC=Enter PC Name: PCJ"
) else set "pc=%1"
taskkill /s pcj%PC% /u domain_name\admin /im csmmviewer.exe

(or if the string PCJ is fix, simpler
killap 12345, when you replace set "pc=%1" with set "pc=PCJ%1")

If you don't give a parameter, (like when you start it by doubleclicking) it will ask you for the name.

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • ok I think I'm getting there but it errors out saying ERROR: The RPC server is unavailable. I updated my original post showing the current codes I have set – Cristian Luzbet Jan 25 '18 at 13:14
  • 1
    in your last code `taskkill /s cn /im csmmviewer.exe` tries to connect to a server named `cn`. You don't have such. Change to `taskkill /s %cn% /im csmmviewer.exe` – Stephan Jan 25 '18 at 16:16
  • that was it! thank you! figured it was something dumb, thank you everyone for all your help! – Cristian Luzbet Jan 27 '18 at 18:04
0

Using set /P by simply prompting the user for input, stores the result in a variable and use the variable from there onwards.

@echo off
set /P "var=Please enter your PC Name: "
taskkill /s pcj%var% /u domain_name\admin /im csmmviewer.exe

for more detail on set you can run set /? from cmd.exe

Gerhard
  • 22,678
  • 7
  • 27
  • 43