Solved

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

Posted on 2011-04-24
8,275 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
0
Question by:it-infra
33 Comments

LVL 67

Expert Comment

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.
0

Author Comment

@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
0

LVL 67

Expert Comment

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.
0

Author Comment

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.
0

LVL 67

Expert Comment

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?
0

Author Comment

Great, how can I add an administrator credential to that command?
0

LVL 67

Expert Comment

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).
0

Author Comment

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
0

LVL 67

Expert Comment

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

0

Author Comment

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.

0

LVL 67

Expert Comment

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.
0

Author Comment

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.
0

Author Comment

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
0

LVL 67

Expert Comment

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

Author Comment

Yes Win 7
0

Author Comment

it will be applied on all OS (XP - Vista and Win 7) but i'm testing it on 7 right now.
0

LVL 67

Assisted Solution

Use the real administrator account then, because that is excempt from User Account Control, which prevents privileged command execution even for a member of the admin groups.

Or copy the file to each PC (e.g. with the -c option), and run the commands in LocalSystem account (-s) - LocalSystem doesn't have network access, but full privileges on the system itself; works only if you do not need any network location.
0

Author Comment

"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
0

LVL 67

Expert Comment

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

Author Comment

Local administrator on the machine? or domain administrator?

Sorry I didnt get that point.
0

LVL 67

Expert Comment

To tell the truth, I don't know if the domain Administrator account works the same - I would first try the local one.
0

LVL 47

Accepted Solution

Try the command this way

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

LVL 47

Expert Comment

Another way to run on all computers is

psexec \\* -c -f -s -d \\server\folder\file.BAT
0

LVL 67

Expert Comment

dstewartjr,

I've recommended that (http:#a35456801) already in http:#a35456268 .
0

LVL 47

Expert Comment

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

Also its unnecessary to add the "cmd /c"
0

LVL 8

Expert Comment

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..
0

LVL 8

Expert Comment

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

)

0

LVL 47

Expert Comment

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.
0

LVL 8

Expert Comment

@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.

http://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)
0

LVL 47

Expert Comment

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)

0

Author Comment

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

Author Closing Comment

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.
0

LVL 67

Expert Comment

The timeout is set with  -n 5   (5 seconds).
0

## Write Comment

Please enter a first name

Please enter a last name

We will never share this with anyone.

## Featured Post

### Suggested Solutions

Title # Comments Views Activity
Rename files on Windows 10 34
Windows Bulk File Rename 3 76
DOS Database Program in Windows 10 5 85
CMD shell elevation.js 4 52
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

#### 761 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!