?
Solved

Run batch file on many machines using PSEXEC, How?

Posted on 2011-04-24
33
Medium Priority
?
16,242 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
Comment
Question by:it-infra
  • 13
  • 12
  • 5
  • +1
33 Comments
 
LVL 71

Expert Comment

by:Qlemo
ID: 35456015
  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

by:it-infra
ID: 35456058
@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.

Open in new window



whats -n?
number 5?
/c

Thanks
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 35456071
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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 

Author Comment

by:it-infra
ID: 35456079
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 71

Expert Comment

by:Qlemo
ID: 35456087
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

by:it-infra
ID: 35456135
Great, how can I add an administrator credential to that command?  
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 35456147
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

by:it-infra
ID: 35456166
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 71

Expert Comment

by:Qlemo
ID: 35456171
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

Open in new window

0
 

Author Comment

by:it-infra
ID: 35456191
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 71

Expert Comment

by:Qlemo
ID: 35456209
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

Open in new window


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

by:it-infra
ID: 35456218
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

by:it-infra
ID: 35456228
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 71

Expert Comment

by:Qlemo
ID: 35456241
I suppose you try to run the batch on Vista/W7?
0
 

Author Comment

by:it-infra
ID: 35456252
Yes Win 7
0
 

Author Comment

by:it-infra
ID: 35456253
it will be applied on all OS (XP - Vista and Win 7) but i'm testing it on 7 right now.
0
 
LVL 71

Assisted Solution

by:Qlemo
Qlemo earned 1000 total points
ID: 35456268
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

by:it-infra
ID: 35456289
"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 71

Expert Comment

by:Qlemo
ID: 35456329
No, what I meant is to use "Administrator" instead of an arbitrary admin account.
0
 

Author Comment

by:it-infra
ID: 35456375
Local administrator on the machine? or domain administrator?

Sorry I didnt get that point.
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 35456552
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

by:
Donald Stewart earned 1000 total points
ID: 35456801
Try the command this way


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

Expert Comment

by:Donald Stewart
ID: 35456805
Another way to run on all computers is

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

Expert Comment

by:Qlemo
ID: 35456811
dstewartjr,

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

Expert Comment

by:Donald Stewart
ID: 35456829
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

by:subhashchy
ID: 35457901
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

Open in new window


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

by:subhashchy
ID: 35457906
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

by:Donald Stewart
ID: 35457952
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

by:subhashchy
ID: 35458004
@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

by:Donald Stewart
ID: 35458037
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

by:it-infra
ID: 35458043
How to add a timeout parameter? I need it to abort if it cant connect the machine in 5 seconds.
0
 

Author Closing Comment

by:it-infra
ID: 35458050
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 71

Expert Comment

by:Qlemo
ID: 35458822
The timeout is set with  -n 5   (5 seconds).
0

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Integration Management Part 2
Suggested Courses
Course of the Month17 days, 13 hours left to enroll

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

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

Join & Ask a Question