I am trying to loop through machines in our domain, execute a command
remotely and return the status. I am having problems with trapping a proper
exit status from the PSExec utility. Here is my code.
@echo off
for /f "tokens=1" %%a in ('net view ^| find "\\"') do (
copy /y debug_patch_1.0.inf %%a\c$\temp
psexec %%a -f -c assigndb.cmd
set debugstatus=%errorlevel%
echo %debugstatus%
set debugstatus=
)
assigndb.cmd executes the debug_patch file. Everything works except I don't
know which machines are offline, etc. The %debugstatus% is always 0, even
if the machine is offline. The echo is meant to test the trapping and after
I had that working I would redirect it to a text file.
Optionally, it would be nice for the routine to check if the host has
already been patched and skip it. I suppose that would involve doing a
findstr or something like that.
All suggestions greatly appreciated.
Use delayedexpansion or move the contents of the for loop to a sub:
@echo off
setlocal
for /f "tokens=1" %%a in ('net view ^| find "\\"') do (
copy /y debug_patch_1.0.inf %%a\c$\temp
Call :ProcessWS %%a
)
goto :eof
:ProcessWS
psexec %1 -f -c assigndb.cmd
set debugstatus=%errorlevel%
echo %debugstatus%
set debugstatus=
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
HTH
--
Greetings
Matthias________________________________________
For help on nt commands enter in a cmd window:
W2K>HH windows.chm::ntcmds.htm XP>HH ntcmds.chm
If memory serves, psexec will return the errorlevel of the last command
run in the script copied to the target, so not only are Matthias'
points valid, but you may see a return code of 0 even if one/some of
the commands in the script copied to the target fail, provided the last
command succeeds.
For example, if you have the following as the last two lines of
assigndb.cmd:
copy some.file C:\nonexistant_path
echo/Hello
I believe that psexec will exit with return code 0. Please correct me
if I am wrong - it certainly would not be the first time.
Unfortunately, I don't have another system here to verify that right
now.
--
Jim Robinson
set email=jim....@ix.netcom.com
echo %email:.n.=n%
<snip>
>If memory serves, psexec will return the errorlevel of the last command
>run in the script copied to the target, so not only are Matthias'
>points valid, but you may see a return code of 0 even if one/some of
>the commands in the script copied to the target fail, provided the last
>command succeeds.
>
>For example, if you have the following as the last two lines of
>assigndb.cmd:
>
>copy some.file C:\nonexistant_path
>echo/Hello
>
>I believe that psexec will exit with return code 0. Please correct me
>if I am wrong - it certainly would not be the first time.
>Unfortunately, I don't have another system here to verify that right
>now.
>
Good Point Jim.
AFAIK echo won't change the errorlevel. But to have valid codes there
should an error handling be incorporated to assigndb.cmd via
exit /B %stored errorlevel of failing command%
You are correct. I used this to my advantage. For the purpose of anyone
who may come across this thread and has a similar need, here is my solution:
----------------------
@echo off
for /f "tokens=1" %%a in (workstations.txt) do (
call :letsgo %%a
)
GOTO :EOF
:letsgo
findstr /c:"\\%1 completed successfully." debuglog.txt > nul
set findstatus=%errorlevel%
if %findstatus% EQU 0 echo Host \\%1 has already been fixed. && GOTO :EOF
echo Copying patch file to \\%1
copy /y debug_patch_1.0.inf \\%1\c$\temp
psexec \\%1 -f -c assigndb.cmd
if errorlevel 0 if not errorlevel 1 echo \\%1 completed successfully. >>
debuglog.txt
__________________
This will copy the patch template file to each host(1), use PSExec to copy
another batch file(2) to the host which installs the patch and write to a
log file when it succeeded. This is based on the exit code returned by the
other batch file which PSExec happily returns to you. On future iterations,
if the host has already been fixed it won't be fixed again.
This turned out to be a great way to change a right on XP machines in the
absence of Active Directory and when users only have user level rights on
the desktop, since I am executing PSExec in the context of my Administrator
account.
I'm sure there are other, more elegant ways to do this but with Matthias'
help this is what I eventually ended up with. Thanks for everyone's input.
I've learned a lot from the generosity of the people in this newsgroup.
(1). The host list was generated using Hyena from SystemTools.com, however
other utilities (including freeware) would have worked.
(2) The contents of the other batch file, assigndb.cmd, are as follows:
________________
@echo off
secedit /configure /db c:\temp\debug.db /areas USER_RIGHTS REGKEYS /cfg
c:\temp\debug_patch_1.0.inf /log c:\temp\secedit.log /verbose
del c:\temp\debug_patch_1.0.inf
del c:\temp\debug.db
___________________
"Seeker" <newsg...@minusthespam.pcuptime.com> wrote in message
news:Ab6nc.165580$M3.1...@twister.nyroc.rr.com...