Psexec with Batch File

March 22, 2009 at 20:04:26
Specs: Windows XP
Hi All I'm a application support guy , I'm just getting into my job now and I really want to create a batch file that will save time.

We spend alot of time remoting onto machines in our network (quite often over seas) via psexec to pull system logs etc. The goal is to remote to the machine, change directory, run zip to compress the log file we require, then copy this zipped up file to a local drive.

This process is quite simple just typing into the command prompt window, however its extemely boring. So far I've created a batch while which prompts the user to enter the machine name they wish to connect to, then once entered the psexec command connects succesfully. However I can get any further than that, my next step would be to change the dir, however this doesnt seem to be working. I've tried putting in a pause after the psexec, however its just not working.


So Far I have

@echo off

echo "enter machine"
set /p machine=
psexec \\%machine% cmd

:: This works a treat, however once connected I want to change the Dir to c:\tmi\files\applications:

I've put in a pause after the psexec, the entered the cd c:\xxxxxxx , however the batch doesnt do anything.

Please could someone assist me it'd be very much appreciated.


See More: Psexec with Batch File

Report •


#1
March 22, 2009 at 21:20:05
It might help to post your script.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#2
March 22, 2009 at 21:59:09
I thought I sort of had

So Far I have

@echo off

echo "enter machine"
set /p machine=
echo"which log do you want to compress"
set/p log_name=
echo"what do you want to call the compressed log, it must be a .zip"
set/p comprssed_log=

psexec \\%machine% cmd :: this is where I get to, I want the script to continue to do the following::

cd c:\tmi\programxx\xxx :this is the directory where the logs are always stored:

zip -9 %comprssed_log% %log_name%


From here on I want to copy the %comprssed_log% to a predetermined location.

I know this desciption is quite patchy, please be gentle with me as I'm just getting into coding. Any advice would be great.


Report •

#3
March 22, 2009 at 22:16:06
You need to upload a second script to the remote machine, then use psexec to run that uploaded script.

Report •

Related Solutions

#4
March 22, 2009 at 23:28:00
I don't have a network to try it but don't you need to precede the CD with:

psexec \\%machine%


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#5
March 23, 2009 at 00:37:44
Wouldn't work; there is no CD.EXE.

Report •

#6
March 23, 2009 at 03:17:57
what wouldnt work? My whole idea is impossible or just the comment from mechanix2go?


Report •

#7
March 23, 2009 at 03:25:29
try this and see if it works:
psexec \\%machine% cmd /k "cd c:\tmi\programxx\xxx & zip -9 %comprssed_log% %log_name%"

untested, i have trouble connecting to other computers via psexec.


Report •

#8
March 24, 2009 at 06:36:58
pegzyisdeed: what wouldnt work? My whole idea is impossible or just the comment from mechanix2go?
Ya know it's your thread. You, of all people, should be paying attention.

Report •

#9
March 24, 2009 at 18:01:57
Reno, Many thanks this is working well now. I'm now able to remotley connect, change dir and zip the appropriate log file. The only issue I have now is I want to copy the newly zipped file to a local directory.

So far I believe I need to kill the psexec session and envoke the copy command which makes the most sense to me. However I'm not sure how to kill the psexec session without the batch file stopping.


psexec \\%machine% cmd /k "cd c:\tmi\programxx\xxx & zip -9 %comprssed_log% %log_name%"

: I want to now copy %compresed_log% to my local machine c:\temp. I'v been told that I should exit psexec before trying to copy back as the rights I have on the psexec session are limited:

Any pointers would of course be great. Cheers



Report •

#10
March 25, 2009 at 01:04:45
i just got psexec to work today, you need to disable simple file sharing.
aftter few test run, i'm not sure if this is the right and acceptable solution.
net share temp$=%cd%
psexec \\%machine% cmd /c "cd c:\tmi\programxx\xxx & zip -9 %comprssed_log% %log_name% & copy %compressed_log% \\%computername%\temp$\"
net share temp$ /delete

on testing i found error though:

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


Logon failure: the user has not been granted the requested logon type at this computer.
        0 file(s) copied.
cmd exited on a with error code 1.



Report •

#11
March 25, 2009 at 13:36:20
As educational as this exercise is, I think it's time we take a step back and look at what psexec does.
- It copies its server part, PSEXESVC.EXE, to the remote's admin$ share.
- It installs psexesvc.exe as the service PSEXESVC as a LocalService.
- It runs the service PSEXESVC.
- PSEXESVC downloads your program, if so commanded.
- PSEXESVC runs your program
- At this point, PSEXEC and PSEXESVC just sit there, sending any output/input between themselves, much like Telnet would.
- Your program exits; PSEXEC reports the return code, and exits as well.
- PSEXESVC deletes the program, if it downloaded it.
- I'm not sure when PSEXESVC is uninstalled, if ever; presumably on the remote's next reboot.

So what do we take away from this? Well, you can't use network shares; PSEXESVC just doesn't have the privileges. However, you have access to the remote's administrative shares; psexec used them in step one. Also, as long as you don't use psexec's -d switch, our script will wait for whatever we're running to quit (again, like telnet). The solution? Have your script grab them from \\remotePC\c$\tmi\programxx\xxx\%log_name% after using psexec to compress them.


Report •

#12
March 25, 2009 at 18:20:18
Thanks both for you input thus far, I now have the code working, I'll need some fine tuning to check for other things, like the file size of the zipped file etc, however I want to share with you what worked for me.

psexec \\%machine% zip -9 c:\tmi\mfws\logs\%new_log%.zip c:\tmi\mfws\logs\%log%

:I want to put in a condition here which will dispaly the size of the %new_log% and ask the user to agree to the copy first :

copy \\%machine%\c$\tmi\mfws\logs\%new_log%.zip "c:\documents and settings\gjohnston\desktop"

Again your thoughts are greatly appreciated.


Report •

#13
March 25, 2009 at 23:15:28
if default share c$ is not disabled and accessible, and no remote execution is needed.

pushd \\%machinename%\c$\tmi\mfws\logs && (
zip -9 %comprssed_log% %log_name%
copy %compressed_log% c:\
popd
)

only tested the default share c$ part and it works.
but accessing the admin$ is not allowed/denied.


Report •

#14
March 26, 2009 at 13:07:16
pegzyisdeed: :I want to put in a condition here which will dispaly the size of the %new_log% and ask the user to agree to the copy first :
for %%a in ("\\%machine%\c$\tmi\mfws\logs\%new_log%.zip") do @echo %%~NXa is %%~Za bytes


Report •


Ask Question