We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

# Run batch file on many machines using PSEXEC, How?

it-infra asked
on
Medium Priority
33,561 Views
Last Modified: 2012-05-11
I have a batch file available on LAN shared folder
\\Server\folder\File.BAT

I wanna run it on sing machine using PSexec.exe
usually i copy the batch file to C drive on any machine then I get into that machine using this command:

psexec \\computerName cmd
after getting into command of that machine I navigate to that file and run it.

now I wanna run it remotely on many machines and the batch file will be on LAN

what will be the command?

Thanks
Comment
Watch Question

## View Solutions Only

"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
psexec @computers.txt -d -n 5 cmd /c \\server\folder\file.BAT
You enlist the names or IPs of the PCs to contact in the computers.txt file. The -d will detach psexec, allowing to run the batch in-parallel on several machines - without psexec will wait until the batch file terminates, before switching to the next PC.

Commented:
@Qlemo

I added computers, it's able to started but the batch has no effect,
this is the output:

C:\PsTools>psexec @computers.txt -d -n 5 cmd /c \

PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010 Mark Russinovich
Sysinternals - www.sysinternals.com

\\it-dellimage:

cmd started on it-dellimage with process ID 4904.
\\it-dellimage2:

cmd started on it-dellimage2 with process ID 3984.


whats -n?
number 5?
/c

Thanks
"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
The output above is expected. Since psexec is detached from the cmd process, the only result you get back is the remote process ID, and nothing more. The batch file needs to store its output into a log file by itself, if needed.

-n 5 is "timeout 5 seconds", in case a computer is not reachable just that moment.

cmd /c makes sure the following line is considered to be input to cmd.exe, and after execution the cmd.exe will close. If you use cmd /k, the cmd.exe will remain open after doing whatever you told it to do.

Commented:
but nothing happened on those machines, i'm sure about the natch file because when I run it directly it does its job.

So it seems the command somehow doesnt execute the batch.
"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
Probably it is not finding the batch. Did you try to start the batch from the share when using psexec manually as you did before?

Commented:
Great, how can I add an administrator credential to that command?
"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
Well, if you just call psexec it will tell you how powerful it is, and which switches you need. For credentials, the switches are -u (user) and -p (password).

Commented:
It worked to access the machines which I dont need as my account is admin and it will access all machines on domain.

I need to know how to use the credentials after "CMD" to let the batch run on the remote machine as administrator. Hope you got my question
"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
The credentials used with -u and -p are what you want. cmd.exe is run with that credentials (or that of the account calling psexec, if -u is not provided).

I have asked you clearly whether you tried manually. Please test. That is, run:
psexec \\computerName cmd
\\server\folder\file.BAT


Commented:
ACCESS IS DENIED for the  2nd command although the folder is shared for all, I guess the restriction is for running the batch, normal users are not allowed to run batches.

"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
There is no means I know of to prevent users from being able to run batch files. And you told me that you are admin, so that restriction wouldn't apply anyway. There are several potential causes:
(1) You have no access to the share.
(2) One of the commands in the batch file has no access

To check for (1), use this:
psexec \\computerName cmd
type \\server\folder\file.BAT


If that outputs the contents, access privileges are ok. Then remove the @echo off command from your batch file to allow for echoing each command as it is executed, and try again.

Commented:
Before I try it, see what I was doing:

1- I copy the batch file to C drive to every machine (I can do it silently as i'm admin)
2- I run "psexec \\computername cmd"
I get into the machine then I navigate to the batch file and run it, it's working.

Limme try your steps and get back.

Commented:
I got the problem, the commands inside the batch file need to modify some windows folders, such actions need an administrative rights.

The file is accessible from the machines, now the question is how to execute a batch file as administrator?

Thanks
"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
I suppose you try to run the batch on Vista/W7?

Commented:
Yes Win 7

Commented:
it will be applied on all OS (XP - Vista and Win 7) but i'm testing it on 7 right now.
"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)

Commented:
"Use the real administrator account then"

You mean to do it manually for each machine? This what i dont want because I wanna make it automated for 10's of machines
"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
No, what I meant is to use "Administrator" instead of an arbitrary admin account.

Commented:
Local administrator on the machine? or domain administrator?

Sorry I didnt get that point.
"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
To tell the truth, I don't know if the domain Administrator account works the same - I would first try the local one.
Network Administrator
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
Network Administrator
CERTIFIED EXPERT

Commented:
Another way to run on all computers is

psexec \\* -c -f -s -d \\server\folder\file.BAT
"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
dstewartjr,

I've recommended that (http:#a35456801) already in http:#a35456268 .
Network Administrator
CERTIFIED EXPERT

Commented:
Yeah....so....the OP didnt seem to understand. I wrote it out

Also its unnecessary to add the "cmd /c"

Commented:
i would suggest as Qlemo said, Copy the file to local system and then run the batch ..may be a batch will do.

@echo off
for /f %%a in (computers.txt) do (
echo processing computer %%a
psexec \\%%a -u administrator -p password xcopy \\server\folder\file.BAT c:\file.bat
psexec \\%%a -s cmd.exe /c c:\file.bat


hopefully it will take one computer at a time copy the batch to c: as administrator and then run as system account.

Lets do not add -d switch for now and run it for 2-3 computers..if works then u can add -d switch if u like.

PS: if you get access denied then try copying the bat file (Xcopy command) to another folder then protected folders.

in vista and win7 C: root, programfiles,windows,users etc are protect folders..may be temp folder or something whch is comoun on all systems..

Commented:
the batch has a missing ")" at end..at the end of batch add a eempty line and then add this

)

Network Administrator
CERTIFIED EXPERT

Commented:
1. There is no need for a for/next routine when you use psexec(this what the @computers.txt is for)
2. psexec has the "-c" option built in to copy file to remote.

How about knowing how psexec works before making a comment on it

PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010 Mark Russinovich
Sysinternals - www.sysinternals.com

PsExec executes a program on a remote system, where remotely executed console
applications execute interactively.

Usage: psexec [\\computer[,computer2[,...] | @file]][-u user [-p psswd][-n s][-l
][-s|-e][-x][-i [session]][-c [-f|-v]][-w directory][-d][-<priority>][-a n,n,...
] cmd [arguments]
-a         Separate processors on which the application can run with
commas where 1 is the lowest numbered CPU. For example,
to run the application on CPU 2 and CPU 4, enter:
"-a 2,4"
-c         Copy the specified program to the remote system for
execution. If you omit this option the application
must be in the system path on the remote system.
-d         Don't wait for process to terminate (non-interactive).
-e         Does not load the specified account's profile.
-f         Copy the specified program even if the file already
exists on the remote system.
-i         Run the program so that it interacts with the desktop of the
specified session on the remote system. If no session is
specified the process runs in the console session.
-h         If the target system is Vista or higher, has the process
run with the account's elevated token, if available.
-l         Run process as limited user (strips the Administrators group
and allows only privileges assigned to the Users group).
On Windows Vista the process runs with Low Integrity.
-n         Specifies timeout in seconds connecting to remote computers.
-p         Specifies optional password for user name. If you omit this
you will be prompted to enter a hidden password.
-s         Run the remote process in the System account.
-u         Specifies optional user name for login to remote
computer.
-v         Copy the specified file only if it has a higher version number
or is newer on than the one on the remote system.
-w         Set the working directory of the process (relative to
remote computer).
-x         Display the UI on the Winlogon secure desktop (local system
only).
-priority  Specifies -low, -belownormal, -abovenormal, -high or
-realtime to run the process at a different priority. Use
-background to run at low memory and I/O priority on Vista.
computer   Direct PsExec to run the application on the remote
computer or computers specified. If you omit the computer
name PsExec runs the application on the local system,
and if you specify a wildcard (\\*), PsExec runs the
command on all computers in the current domain.
@file      PsExec will execute the command on each of the computers listed
in the file.
program    Name of application to execute.
arguments  Arguments to pass (note that file paths must be
absolute paths on the target system).

You can enclose applications that have spaces in their name with
quotation marks e.g. psexec \\marklap "c:\long name app.exe".
Input is only passed to the remote system when you press the enter
key, and typing Ctrl-C terminates the remote process.

If you omit a user name the process will run in the context of your
account on the remote system, but will not have access to network
resources (because it is impersonating). Specify a valid user name
in the Domain\User syntax if the remote process requires access
to network resources or to run in a different account. Note that
the password is transmitted in clear text to the remote system.

Error codes returned by PsExec are specific to the applications you
execute, not PsExec.

Commented:
@dstewartjr:

i do know how psexec works..might u seen that in the next post you commented.before your comment i suggested the questioner to use psexec.

https://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_26973869.html?cid=1573#a35457974

my question is : Did i objected on what you said ? or tell me if my approach is wrong ?

what i said is just an alternate approach which might help the OP.
What you just suggested is already a copy of what Qlemo and u said  (total 3 times)
Network Administrator
CERTIFIED EXPERT

Commented:
I use Psexec day in and day out

Your """Batch""" includes unnecessary steps (for /f ) this is done by the @File. (xcopy) this is done by the "-c" switch.

so that is why I questioned your experience with psexec.

"What you just suggested is already a copy of what Qlemo and u said  (total 3 times)"

Ahh... no I didnt. I only put it all together in one command so the OP can see the whole command together(and omitted cmd.exe /c)

Commented:
How to add a timeout parameter? I need it to abort if it cant connect the machine in 5 seconds.

Commented:
I dont wanna modify the batch file, I wanted to run it from command only.

Now it worked with:

psexec @computers.txt -c -f -s -d \\server\folder\file.BAT

Thanks for both of you guys.
"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
The timeout is set with  -n 5   (5 seconds).
Unlock the solution to this question.
##### Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the