Gutsygibbon
Gutsygibbon

Reputation: 733

How to remotely execute a command via a batch file?

So basically, all I want to do is execute an "sc" command on 100 servers whose names are mentioned in a text file "server.txt". I tried the following and it doesnt work as it returns empty notepad file.

for /f %%a in (servers.txt) do sc \\server_name query | find "SERVICE_NAME">>servicecontroller.txt

I tried the above command locally and it worked just fine. I know the issue is in the "\server_name". Not sure what about it is wrong. Do i need to use the hidden shares? Like this maybe?

\\server_name\c$ /user:domain\username password    

Or like this?

 \\server_name\c$

Upvotes: 0

Views: 13204

Answers (3)

Mark
Mark

Reputation: 3690

If you've hardcoded \\server_name there rather than %%a, then that's your problem. Try this instead:

for /f %%a in (servers.txt) do sc \\%a query | find "SERVICE_NAME">>servicescontroller.txt

EDIT : The following line helps in getting admin access consistently on all the servers. This is the line I(OP) was looking for.

net use \\servername\ipc$ /u:domain\user password

Upvotes: 1

wmz
wmz

Reputation: 3685

As alternative way, you could use wmic for that:

Execute against many machines at once and save list as nicely formatted html table (among other formats):

wmic /node:@servers.txt /output:services.html service get caption /format:htable 

Of course you may query many more attributes besides names/captions.

Upvotes: 1

marapet
marapet

Reputation: 56446

You can execute commands on remote machines in a network using PsExec. I use it to execute batch files and pass parameters to them, as well as checking for running services. There's even a special syntax to execute on a number of computers listed in a text file.

Alternatively, if powershell is an option, you may consider using powershell remoting.

Upvotes: 2

Related Questions