1

I need help - I am trying to output any ErrorLevel that is 1 or greater into a log file. When I issue the command the log file never get's generated. Any help would be greatly appreciated.

Script:

for /f "delims=" %%i in (C:\_\Restart\Computer.txt) do (
start "%%i" \_\PStools\psexec \\%%i -u Administrator -p Password -i c:\restart.cmd
if not %errorlevel%==0 echo %errorlevel% > error.log
)

This script allows me to use PSEXEC and issue a restart command to all the computer at once. However several of them fail and I'd like to know which ones fail.

Thanks!

Is this the format I should use?

setlocal EnableDelayedExpansion

for /f "delims=" %%i in (C:\_\Restart\Computer.txt) do (
start "%%i" \_\PStools\psexec \\%%i -u Administrator -p Password -i c:\restart.cmd
if errorlevel 1 echo !errorlevel! > error.log
)
12
  • 2
    You need delayed expansion for !errorlevel!... Commented Sep 6, 2016 at 15:13
  • What will that look like in the code? I'm trying it several ways and nothing is working so far. Commented Sep 6, 2016 at 15:25
  • Place setlocal EnableDelayedExpansion on top of your script and exchange %errorlevel% by !errorlevel!. Alternatively, simply replace if not %errorlevel%==0 by if errorlevel 1 (meaning equal to or greater than 1)... Commented Sep 6, 2016 at 16:19
  • The script will not execute - please see revised code above. Commented Sep 6, 2016 at 17:13
  • 1
    You probably also need to use >> instead of >. You're overwriting the file. Commented Sep 6, 2016 at 17:21

1 Answer 1

2

Script V3:

setlocal EnableDelayedExpansion

for /f "delims=" %%i in (C:\temp\list.txt) do (
start "" "shutdown" /m \\%%i -r -f -t 900
echo !errorlevel! && echo %%i
if errorlevel 1 echo !errorlevel! >> c:\temp\log.txt && echo %%i >> c:\temp\log.txt
)

-m = use remote computer

-r = reboot

-f = Force reboot

-t = delay of time before rebooting

you can use shutdown -? for more help on argument that can be passed to the reboot command.

Script v4 without the start command:

setlocal EnableDelayedExpansion

for /f "delims=" %%i in (C:\temp\list.txt) do (
shutdown /m \\%%i -r -f -t 900
echo !errorlevel! && echo %%i
if errorlevel 1 echo !errorlevel! >> c:\temp\log.txt && echo %%i >> c:\temp\log.txt
)
Sign up to request clarification or add additional context in comments.

10 Comments

This does not work. When i execute the script the cmd opens then closes very quickly with no errors. All this script is trying to do is look into a text file with hostnames and use PSEXEC to execute the restart.cmd script remotely all at the same time. I am trying to capture the error code for when the command didn't execute (for whatever reason). Do you think there's a better way of doing the script?
I guess you have admin right on the remote computer so you could use the built in windows command "shutdown" instead of calling psexec maybe this would help.code for /f "delims=" %%i in (C:\Restart\Computer.txt) do ( shutdown /m \\%%i >> c:\temp\log.txt)
I edited my answer to reflect the new code without using psexec.
The script executed. However the script would only restart 1 computer out of the list of 100 that I have. For the script I have to be able to restart all computers at the same time. Is that possible? Also the ErrorLevel, even though computer successfully restarted it still shows Error 53, same error that is used when i put a bogus hostname in.
I tested with a liste of 4 computer, one per line and it returned me 0 for the computer with success and 53 for computer it could not reach. Also I just checked with another workstation that has windows firewall enable and the operation was not successfull so windows firewall might be an issue. The script as is will display on screen those with success and in the log file those with error.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.