1.

Solve : Help with my Batch PLEASE!!?

Answer»

Hello,

so here is my script. issue im having is that it just basically ignores my "if errorlevel" i tried the various different ways

for /f %%F in (C:\IPLIST.txt) do (
pstools\psexec.exe \%%F -u administrator -p password net user administrator password1
if %errorlevel% NEQ 0 goto two
:two
pstools\psexec.exe \%%F -u administrator -p password3 net user administrator password2
)

So it goes thro the txt file list of IP addresses and it changes the LOCAL admin password. those 2 commands are to give it different authentication credentials, now i know i can do this with pspasswd but the "-u -p" switches wont work. but this works.. its just if the first command succeeds with errorcode 0 it still runs the SECOND command for the same IP. i want it to ignore the second command for that IP if the first ONE succeeds. and just continue down the IP list..

is this because im using net user or pstools thats why its ignoring it?

im still new to writing batch files so im sorry if im missing something obvious.
Anyone know how to fix this?


thank you.
It would seem from this site: http://www.robvanderwoude.com/errorlevel.php, you don't put a % around error level. Try and see what happens if you remove the %.

Quote from: Nexusfactor on December 15, 2015, 11:13:13 AM

It would seem from this site: http://www.robvanderwoude.com/errorlevel.php, you don't put a % around error level. Try and see what happens if you remove the %.

i have tried that. and also tried !errorlevel!... no luck. someone told me that i cannot use the "goto" function in a FOR command.. is this true? You should be able too. I did some Googling and it doesn't seem that it's illegal to do inside a for loop. According to this, if GOTO doesn't work, try this, call: Example here: http://stackoverflow.com/questions/11177419/batch-file-goto-in-for-loop

It seems to be the appropriate answer inside a for loop.Quote from: Arotin on December 15, 2015, 11:20:38 AM
i have tried that. and also tried !errorlevel!... no luck. someone told me that i cannot use the "goto" function in a FOR command.. is this true?

Did you try it in call caps without the percent?Quote from: Arotin on December 15, 2015, 11:20:38 AM
someone told me that i cannot use the "goto" function in a FOR command.. is this true?
Nearly right... what you can't have in a loop is a label. You need another way to use conditional logic in the loop

Here are 2 ways

1. Using errorlevel variable

Code: [Select]REM if you use the errorlevel variable
REM in a loop you need this and also
REM you use !errorlevel! not %errorlevel%
setlocal enabledelayedexpansion

for /f %%F in (C:\IPLIST.txt) do (
pstools\psexec.exe \%%F -u administrator -p password net user administrator password1
if !errorlevel! NEQ 0 pstools\psexec.exe \%%F -u administrator -p password3 net user administrator password2
)
2. Another way to test for a non-zero errorlevel is with a double pipe || (test for zero error level with &&)

Code: [Select]for /f %%F in (C:\IPLIST.txt) do (
pstools\psexec.exe \%%F -u administrator -p password net user administrator password1 || pstools\psexec.exe \%%F -u administrator -p password3 net user administrator password2
)
In fact you can put the second one all on one line

Code: [Select]for /f %%F in (C:\IPLIST.txt) do pstools\psexec.exe \%%F -u administrator -p password net user administrator password1 || pstools\psexec.exe \%%F -u administrator -p password3 net user administrator password2

Quote from: Salmon Trout on December 15, 2015, 12:04:07 PM
Nearly right... what you can't have in a loop is a label. You need another way to use conditional logic in the loop

Here are 2 ways

1. Using errorlevel variable

Code: [Select]REM if you use the errorlevel variable
REM in a loop you need this and also
REM you use !errorlevel! not %errorlevel%
setlocal enabledelayedexpansion

for /f %%F in (C:\IPLIST.txt) do (
pstools\psexec.exe \%%F -u administrator -p password net user administrator password1
if !errorlevel! NEQ 0 pstools\psexec.exe \%%F -u administrator -p password3 net user administrator password2
)
2. Another way to test for a non-zero errorlevel is with a double pipe || (test for zero error level with &&)

Code: [Select]for /f %%F in (C:\IPLIST.txt) do (
pstools\psexec.exe \%%F -u administrator -p password net user administrator password1 || pstools\psexec.exe \%%F -u administrator -p password3 net user administrator password2
)
In fact you can put the second one all on one line

Code: [Select]for /f %%F in (C:\IPLIST.txt) do pstools\psexec.exe \%%F -u administrator -p password net user administrator password1 || pstools\psexec.exe \%%F -u administrator -p password3 net user administrator password2

Wonderful!

Just out of curiousty, how did you become good with batch files?Quote from: Nexusfactor on December 15, 2015, 12:18:31 PM
Just out of curiousty, how did you become good with batch files?
Study the documentation, look for batch code sites and forums (ss64 and Stack Overflow are good, so are alt.msdos.batch & alt.msdos.batch.nt - you can get to these via Google Groups), practice writing scripts, don't be afraid to experiment.


Quote from: Salmon Trout on December 15, 2015, 12:04:07 PM
Nearly right... what you can't have in a loop is a label. You need another way to use conditional logic in the loop

Here are 2 ways

1. Using errorlevel variable

Code: [Select]REM if you use the errorlevel variable
REM in a loop you need this and also
REM you use !errorlevel! not %errorlevel%
setlocal enabledelayedexpansion

for /f %%F in (C:\IPLIST.txt) do (
pstools\psexec.exe \%%F -u administrator -p password net user administrator password1
if !errorlevel! NEQ 0 pstools\psexec.exe \%%F -u administrator -p password3 net user administrator password2
)
2. Another way to test for a non-zero errorlevel is with a double pipe || (test for zero error level with &&)

Code: [Select]for /f %%F in (C:\IPLIST.txt) do (
pstools\psexec.exe \%%F -u administrator -p password net user administrator password1 || pstools\psexec.exe \%%F -u administrator -p password3 net user administrator password2
)
In fact you can put the second one all on one line

Code: [Select]for /f %%F in (C:\IPLIST.txt) do pstools\psexec.exe \%%F -u administrator -p password net user administrator password1 || pstools\psexec.exe \%%F -u administrator -p password3 net user administrator password2


I am going to test these out now, thank you for the examples that really helps!
i will post the result,

thanksthat worked!!

if i wanted to add a third authentication it would just be?:


for /f %%F in (C:\IPLIST.txt) do (
pstools\psexec.exe \%%F -u administrator -p password net user administrator password1
if !errorlevel! NEQ 0 pstools\psexec.exe \%%F -u administrator -p password3 net user administrator password2
if !errorlevel! NEQ 0 pstools\psexec.exe \%%F -u administrator -p password4 net user administrator password3
)

and to log the ones that fail i can just do :


for /f %%F in (C:\IPLIST.txt) do (
pstools\psexec.exe \%%F -u administrator -p password net user administrator password1
if !errorlevel! NEQ 0 pstools\psexec.exe \%%F -u administrator -p password3 net user administrator password2
if !errorlevel! NEQ 0 pstools\psexec.exe \%%F -u administrator -p password4 net user administrator password3
if !errorlevel! NEQ 0 echo >>Failed.log
)
?


thank youThe reason your original batch file worked the way it did is because REGARDLESS of whether the errorlevel was 0 or not, :two was the next line encountered in the batch. In order for it to work, goto two would have needed to skip over batch lines that you didn't want executed.


Discussion

No Comment Found