Batch file that runs whatever command you type on multiple command prompts

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
roem99
Posts: 3
Joined: 23 Jun 2016 11:08

Batch file that runs whatever command you type on multiple command prompts

#1 Post by roem99 » 23 Jun 2016 11:11

I was wondering if someone knew of a Batch code that when you open it you can type in whatever command or just a text and it gets executed on the selected command prompts?

douglas.swehla
Posts: 75
Joined: 01 Jun 2016 09:25

Re: Batch file that runs whatever command you type on multiple command prompts

#2 Post by douglas.swehla » 25 Jun 2016 12:18

roem99 wrote:I was wondering if someone knew of a Batch code that when you open it you can type in whatever command or just a text and it gets executed on the selected command prompts?


It sounds like you want to launch a batch file that starts an interactive session where you can launch child CMD.EXE processes, and then send commands to just one of them, all of them, or some selected list of them. Is that what you want to do? It's hard to imagine a real need for what it sounds like you want, compared to existing solutions.

What do you mean by "just a text"?

Please tell us more about exactly what you're trying to do, and include specific examples.

roem99
Posts: 3
Joined: 23 Jun 2016 11:08

Re: Batch file that runs whatever command you type on multiple command prompts

#3 Post by roem99 » 25 Jun 2016 17:45

So the deal is. i am runnin a HQ setup for hundreds of minecraft and VPS servers with remote desktop. some of the servers like minecraft needs to be shutdown or started at the same time so its faster. and all i need is a command prompt that is connected to the minecraft batch files so when i type a command like 'hello' it will appear on all the minecraft batch servers that is running and i selected in the config of the sender. so basicly what i am asking is for a script that will open a command prompt that will automaticly sync/send commands to selected running commandprompts as i type them in on the command prompt sender.

douglas.swehla
Posts: 75
Joined: 01 Jun 2016 09:25

Re: Batch file that runs whatever command you type on multiple command prompts

#4 Post by douglas.swehla » 26 Jun 2016 12:01

Okay, so you want to send the same command to multiple machines. That can be done, but not the way you're asking. Before we start, let's clarify some concepts.

Each machine has a copy of the CMD.EXE program, which is the thing that opens a console window where you can type commands. (The "command prompt", strictly speaking, is the bit if text in the console window (e.g., C:\>) where you are "prompted" to type a command, but many people use it to mean the console window itself.) You can start multiple instances of CMD.EXE at the same time, but there is only one copy of the program. Batch files are text files that contain a list of commands that can be run by CMD.EXE. One machine can store many batch files. You can use multiple instances of CMD.EXE to run the same batch file many times at once.

On a network, it is possible for machines to connect to each other using several different protocols; Remote Desktop is one of these, but there are more. It is possible for the copy of CMD.EXE one machine to run batch files that are stored on another machine on the same network. It is also possible for a user running CMD.EXE on MachineA to use command line tools to log into MachineB, start an instance of MachineB's copy of CMD.EXE, and enter commands one at a time, or run batch files; when you do this, it is MachineB that is running the commands, not MachineA.

What is not possible is for a user on MachineA to send commands to MachineB and MachineC at exactly the same time; it has to be done in sequence. Also, since you connect to a machine, and run a batch file, the phrase "connect to a batch file" doesn't mean anything.

When you run a batch file, you can do it in such a way that you don't have to wait for it to finish before moving on. You can also put them in a place that makes them run automatically when a machine starts up or shuts down.

Putting it all together, what we're probably going to want to have is:
  • one core script that does all the actual setup tasks, located in a shared folder, that each remote machine will find and run
  • a barebones script, copied to the startup folder in each of the hundreds of remote machines, that checks to see if that machine needs to restart, then calls the core script
  • a master script, run from your controlling computer, that copies the barebones script to each remote machine, logs into that machine, starts the script, and moves on to the next one

If HQ setup tasks can be run without intervention once the machine is restarted, then the solution will probably look something like these two scripts:

Code: Select all

:: net-setup.bat (master script)
for %%A in (machine1 machine2 machineN) do (
   rem Copy ind-setup.bat to startup folder.
   rem Remote into machine and run ind-setup.bat, without waiting for finish.
)


Code: Select all

::ind-setup.bat (barebones script, in startup folder)
if %need-to-restart%==true shutdown /r /c "Restarting for HQ setup."
rem If machine restarts, this script will get run at startup, first line will do nothing, and setup will proceed normally.
rem Call core script in shared location. You could also just have the commands here, depending on your priorities.


If the HQ setup tasks do require intervention during setup, that's a whole other set of complications.

  • Please post the contents of the setup script you're trying to run, as it exists now.
  • Describe what conditions you're checking to see if a machine needs to be restarted or not.
  • Is there any problem with restarting all machines, even if they don't need it?
  • Might there be users logged in, or is it safe to restart without warning?
  • Do you use the same id and password on all the remote machines?
  • Can the remote machines all access the same shared locations? For example, could they all run something like "S:\shared_files\hq_setup.bat"?

We need to know whether the remote machines are set up to accept remote shell commands. In the script below, replace "machine1 machine2 machine3" with some of your remote machine names, run the it from the machine you'll use to control everything, and post the results here. I don't have a network to test this on, so I'm going from memory and research. It may not work as expected, but shouldn't break anything.

Code: Select all

for %%A in (machine1 machine2 machine3) do (
   (winrs -r:"%%A" echo WINRS command worked on %%computername%% || echo WINRS failed.) >> winrs_results.txt
   (wmic /node:"%%A" process call create "cmd.exe /c echo WMIC connection worked on %%computername%%" || echo WMIC failed.) >> wmic_results.txt
)


The script we come up with will probably use a combination of the following commands. Take a look at the documentation for each of them (type command /?). You might come up with your own solution.
  • shutdown
  • winrm
  • winrs
  • wmic

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file that runs whatever command you type on multiple command prompts

#5 Post by foxidrive » 26 Jun 2016 15:00

I get the impression that this is operating over the internet, but I know nothing about minecraft.

It seems like it's a console ability within minecraft...

roem99
Posts: 3
Joined: 23 Jun 2016 11:08

Re: Batch file that runs whatever command you type on multiple command prompts

#6 Post by roem99 » 26 Jun 2016 17:02

I am sorry for wasting you guys time. i just setup my own script with the link bridge between all the command prompts and i appreciate all you guys support on this thanks :D

Post Reply