Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with a batch file "goto" statement 1

Status
Not open for further replies.

hgate73

IS-IT--Management
Feb 22, 2008
80
US
I've been using this batch file for a while to remotely update Java on some computers. However, for some reason it just started breaking, and I'm not sure why. Whenever I make the menu selection and then hit 'enter' to confirm and start the patch process, I get the error "goto was unexpected at this time." I can't find anything wrong with the GOTO statement; it's the same as in many of my other batch files.

This happens on Vista SP2 and XP SP3 computers.

Here's the error, and below is the full script.
Okay, ready to attempt mass installation.

Target: All
New Java: jre-6u20-windows-i586-s.exe
Options: /quiet /promptrestart
Names File: java.txt
PSEXEC: 4 second connection timeout

Ready?? Press [enter] to go, or "return" to go back to the menu

Confirm?:
goto was unexpected at this time.


This is the script, the offending lines are highlighted.

Code:
:: Author:          [removed]
:: Purpose:         Nukes then updates Java to the latest version
:: Requirements:    1. Run this script with a network admin account
::                  2. psexec.exe (from Microsoft) is required, and may be in any of these locations on your local computer:
::                      a) the directory you run this script from
::                      b) c:\windows\system32\    (preferred)
::                      c) in the PATH variable
::                  3. Put the names of your computers, one per line, in the "names" file in the same directory as this script

:: Prep our variables
@echo off
title Mass Java Updater
set VERSION=1.2
set NAMES_FILE=java.txt
set TIMEOUT=4
set TARGET=

:: SET THE JAVA VERSION YOU WANT TO INSTALL HERE
set NEW_JAVA=jre-6u20-windows-i586-s.exe
set OPTION1=/quiet
set OPTION2=/promptrestart
set OPTION3=
set OPTION4=
set OPTION5=

:: Welcome screen
:start
set TARGET=
title Mass Java Updater - %NEW_JAVA% %OPTION1% %OPTION2% %OPTION3% %OPTION4% %OPTION5%
color 07
cls
echo.
echo  Mass Java Updater v%VERSION%
echo  Requires psexec.exe
echo.
echo  - Enter IP or name of a remote computer to update Java on.
echo  - Enter "all" to attempt a mass-installation for all computers
echo    listed in %NAMES_FILE%. This doesn't always work very well...
:info
echo.
echo  Target:     %TARGET%
echo  New Java:   %NEW_JAVA%
echo  Options:    %OPTION1% %OPTION2% %OPTION3% %OPTION4% %OPTION5%
echo  Names File: %NAMES_FILE%
echo  PSEXEC:     %TIMEOUT% second connection timeout
echo.

:loop
echo.
set /P TARGET= Target computer or "all": 
	if %TARGET%==exit goto end
	if %TARGET%==all goto auto
	if %TARGET%==info goto info
goto ready

:ready
cls
echo.
echo  Okay, ready to attempt installation using the following settings:
echo.
echo   Target:     %TARGET%
echo   New Java:   %NEW_JAVA%
echo   Options:    %OPTION1% %OPTION2% %OPTION3% %OPTION4% %OPTION5%
echo   Names File: %NAMES_FILE%
echo   PSEXEC:     %TIMEOUT% second connection timeout
echo.
echo  Press [enter] to confirm, or "return" to go back to the menu
echo.
set CHOICE=go
set /P CHOICE= Confirm?: 
	if %CHOICE%==exit goto end
	if %CHOICE%==return goto start
	if %CHOICE%==go goto execute
:execute
echo.
echo  **Step 1**  Copying javakiller payload to %TARGET%...
copy javakiller.vbs \\%TARGET%\C$\javakiller.vbs
echo.
echo  **Step 2**  Executing javakiller payload on %TARGET%. Mu ha ha!
echo.
echo           This can take up to 3 minutes.
psexec \\%TARGET% cscript %SystemDrive%\javakiller.vbs //B
del \\%TARGET%\C$\javakiller.vbs
echo.
echo  **Step 4**  Installing %NEW_JAVA% on %TARGET%...
psexec -n %TIMEOUT% \\%TARGET% -c %NEW_JAVA% %OPTION1% %OPTION2% %OPTION3% %OPTION4% %OPTION5%
echo.
echo  **STEP 5** RESULTS (You can ignore any "error code 0" messages)
psexec \\%TARGET% "%ProgramFiles%\Java\jre6\bin\java.exe" -version
echo.
echo  Done at %TIME%. Returning to menu...
echo.
title Mass Java Updater
set TARGET=
goto loop


:: HIROSHIMA! this is the "mass" version
:auto
set TARGET=all
set CHOICE=
cls
echo.
echo  Okay, ready to attempt mass installation. 
echo.
echo  Target:     All
echo  New Java:   %NEW_JAVA%
echo  Options:    %OPTION1% %OPTION2% %OPTION3% %OPTION4% %OPTION5%
echo  Names File: %NAMES_FILE%
echo  PSEXEC:     %TIMEOUT% second connection timeout
echo.
echo  Ready?? Press [enter] to go, or "return" to go back to the menu
echo.
set /P CHOICE= Confirm?: 
[b][highlight][COLOR=blue] --- REM Script breaks here --- [/color][/highlight][/b]
	if %CHOICE%==exit goto end
	if %CHOICE%==return goto start
	if %CHOICE%=='' goto auto_execute

:auto_execute
echo Alright! Commencing now...
echo.
:: LEGEND:
:: -d               Don't wait for the process to terminate (non-interactive), "gun and run"
:: -belownormal     Runs the process with the "low" priority.
:: \\*              Would run the command for all domain computers for which we have access
:: -n 7             Timeout for connection - wait 7 seconds before moving on
:: -c				Copy the specified program to the remote system for execution
:: -f               Force the copy even if the remote program already exists
echo  **Step 1** Copying the javakiller payload to targets, please wait...
echo.
for /F %%i in (%NAMES_FILE%) do echo %%i && copy /Y javakiller.vbs \\%%i\C$\javakiller.vbs
echo.
echo  **Step 2** Executing the javakiller payload on targets.
echo             This can take up to 2 minutes.
echo.
:: we do a ping first to create an ARP entry and lower the chance of a connection timeout
for /F %%i in (%NAMES_FILE%) do ping %%i -n 1 >NUL && psexec -n %TIMEOUT% -d \\%%i cscript C:\javakiller.vbs //B
echo.
echo  Waiting for javakiller to finish running on remote hosts, recommend
echo  waiting at least 4 minutes here before continuing.
pause
echo.
echo  **Step 3** Installing %NEW_JAVA% on targets, please wait...
echo.
psexec -n %TIMEOUT% -d @%NAMES_FILE% -c %NEW_JAVA% %OPTION1% %OPTION2% %OPTION3% %OPTION4% %OPTION5%
echo.
echo  Java Updater - Complete @ %TIME%
echo.
goto loop

:end
title %USERNAME%

I was previously posting under "GeneralDzur" but hgate73 is my new username.
 
Okay, after talking to a buddy of mine, we found a workaround.

For some reason the command-prompt doesn't like a blank variable (maybe pressing 'Enter' sends a control character?), so I altered the script to pre-load the CHOICE variable with 'y', and altered the script to check for 'y' instead of being blank, and it worked.

Old way:
Code:
:: HIROSHIMA! this is the "mass" version
:auto
set TARGET=all
[COLOR=blue]set CHOICE=[/color]
cls
echo.
echo  Okay, ready to attempt mass installation.
echo.
echo  Target:     All
echo  New Java:   %NEW_JAVA%
echo  Options:    %OPTION1% %OPTION2% %OPTION3% %OPTION4% %OPTION5%
echo  Names File: %NAMES_FILE%
echo  PSEXEC:     %TIMEOUT% second connection timeout
echo.
echo  Ready?? Press [enter] to go, or "return" to go back to the menu
echo.
set /P CHOICE= Confirm?:
    if %CHOICE%==exit goto end
    if %CHOICE%==return goto start
[COLOR=blue]    if %CHOICE%=='' goto auto_execute[/color]

New way:
Code:
:: HIROSHIMA! this is the "mass" version
:auto
set TARGET=all
[COLOR=blue]set CHOICE=y[/color]
cls
echo.
echo  Okay, ready to attempt mass installation.
echo.
echo  Target:     All
echo  New Java:   %NEW_JAVA%
echo  Options:    %OPTION1% %OPTION2% %OPTION3% %OPTION4% %OPTION5%
echo  Names File: %NAMES_FILE%
echo  PSEXEC:     %TIMEOUT% second connection timeout
echo.
echo  Ready?? Press [enter] to go, or "return" to go back to the menu
echo.
set /P CHOICE= Confirm?:
    if %CHOICE%==exit goto end
    if %CHOICE%==return goto start
[COLOR=blue]    if %CHOICE%==y goto auto_execute[/color]


I was previously posting under "GeneralDzur" but hgate73 is my new username.
 
if %CHOICE%=='' goto auto_execute
was redundant as script dropped into
:auto_execute
anyway!

Or maybe I don't quite follow? But I would have thought your code needed to be something like this

Code:
:retry
echo  Ready?? Press [enter] to go, or "return" to go back to the menu
echo.
set /P CHOICE= Confirm?:
    if %CHOICE%==exit goto end
    if %CHOICE%==return goto start
    if %CHOICE%==y goto auto_execute
goto retry
 
No, you're correct :p We noticed that too while re-writing it. It's supposed to say "goto auto" at the end of the 'if' statements. Thanks for the catch

I was previously posting under "GeneralDzur" but hgate73 is my new username.
 
The problem is that "variables" are replaced by their values before the statement is processed. So
Code:
if %target%=='' goto auto_execute
becomes
Code:
 if =='' goto auto_execute
As you can see, this is invalid syntax and the interpreter (CMD.EXE) gets confused. COMMAND.COM would simply say "invalid syntax" or something simular.
 
BTW, I always add an "extra" character after variables to eliminate the possibity of their being blank:
Code:
If %target%x==x goto target_is_blank
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top