Link to home
Start Free TrialLog in
Avatar of Colin Rosario
Colin Rosario

asked on

Why are variables in my batch script not updating as expected?

I wrote the following batch file, but it does not work exactly as expected.

When the first record in the CSV file is processed, it displays the "echo" commands under :PRINTVARS, but subsequent loops don't. In addition, the _folderName variable is loaded with the correct value for the first record in the CSV file, but it doesn't update with the value of the next value in the CSV file...it keeps the value from the first record.

I could use some help. Any ideas?

@echo off

:: Temporarily add plink to PATH
set PATH=C:\Program Files\Plink;%PATH%

SetLocal EnableDelayedExpansion

:: Store input line into different variables
FOR /F "tokens=1-3 delims=," %%A IN (c7000-list.csv) DO (
	Set _enclName=!_enclName!%%A
	Set _folderName=!_folderName!%%B
	Set _oaIP=!_oaIP!%%C
	
	CALL :PRINTVARS
	CALL :MAKEFOLDERS
	CALL :STORESSHKEY
	CALL :OABACKUP
	CALL :VCBACKUP
)
GOTO :EOF

:PRINTVARS
:: Echo statements for troubleshooting & reviewing contents of variables
echo enclName=!_enclName!
echo folderName=!_folderName!
echo oaIP=!_oaIP!

:MAKEFOLDERS
:: Make directories for each c7000 chassis along with a folder with today's date to store the c7000 config backups
md ".\!_folderName!
md ".\!_folderName!\%date:~10,4%.%date:~4,2%.%date:~7,2%"

:STORESSHKEY
:: Connect to SSH host and store key in cache
SetLocal DisableDelayedExpansion
echo y | plink -ssh admin@%_oaIP% -pw <PWD> exit

:OABACKUP
:: Run Plink to backup OA config
plink.exe -m "D:\c7000_config_backups\ShowAllCmd.txt" -pw <PWD> admin@%_oaIP% >>".\%_folderName%\%date:~10,4%.%date:~4,2%.%date:~7,2%"\%_enclName%-OA-ShowAll.txt 2>>&1 

:VCBACKUP
:: Run VCSU to backup Virtual Connect config
"C:\Program Files (x86)\Hewlett-Packard Company\Virtual Connect Support Utility\vcsu.exe" -a configbackup -i %_oaIP% -u admin -p <PWD> -vcu admin -vcp <PWD> -l ".\%_folderName%\%date:~10,4%.%date:~4,2%.%date:~7,2%"\vcConfig-%_enclName%_vc_domain
SetLocal EnableDelayedExpansion

GOTO :EOF

Open in new window

Avatar of Gabriel Clifton
Gabriel Clifton
Flag of United States of America image

You are not calling the subscripts with the variables you want to use. Ie call :sub1 %%a
Avatar of Steve Knight
If you want to exit from each of the subroutines you are calling add to the end of each one before the next sub

goto :eof
or
exit /b

e.g.

:STORESSHKEY
:: Connect to SSH host and store key in cache
SetLocal DisableDelayedExpansion
echo y | plink -ssh admin@%_oaIP% -pw <PWD> exit
exit /b

Open in new window


as otherwise it will do the first call, then make it's way through all the other bits to the goto :eof then return to the next line in your for loop etc..  alternatively put all that code inside the loop, or just call it once if the routines aren't used anywhere else, i.e.

@echo off

:: Temporarily add plink to PATH
set PATH=C:\Program Files\Plink;%PATH%

SetLocal EnableDelayedExpansion

:: Store input line into different variables
FOR /F "tokens=1-3 delims=," %%A IN (c7000-list.csv) DO (
	Set _enclName=!_enclName!%%A
	Set _folderName=!_folderName!%%B
	Set _oaIP=!_oaIP!%%C
	
	CALL :PROCESSALL
)
GOTO :EOF

:PROCESSALL
:PRINTVARS
:: Echo statements for troubleshooting & reviewing contents of variables
echo enclName=!_enclName!
echo folderName=!_folderName!
echo oaIP=!_oaIP!

:MAKEFOLDERS
:: Make directories for each c7000 chassis along with a folder with today's date to store the c7000 config backups
md ".\!_folderName!
md ".\!_folderName!\%date:~10,4%.%date:~4,2%.%date:~7,2%"

:STORESSHKEY
:: Connect to SSH host and store key in cache
SetLocal DisableDelayedExpansion
echo y | plink -ssh admin@%_oaIP% -pw <PWD> exit

:OABACKUP
:: Run Plink to backup OA config
plink.exe -m "D:\c7000_config_backups\ShowAllCmd.txt" -pw <PWD> admin@%_oaIP% >>".\%_folderName%\%date:~10,4%.%date:~4,2%.%date:~7,2%"\%_enclName%-OA-ShowAll.txt 2>>&1 

:VCBACKUP
:: Run VCSU to backup Virtual Connect config
"C:\Program Files (x86)\Hewlett-Packard Company\Virtual Connect Support Utility\vcsu.exe" -a configbackup -i %_oaIP% -u admin -p <PWD> -vcu admin -vcp <PWD> -l ".\%_folderName%\%date:~10,4%.%date:~4,2%.%date:~7,2%"\vcConfig-%_enclName%_vc_domain

GOTO :EOF

Open in new window

                                 
Also not sure what "SetLocal EnableDelayedExpansion" was doing at the end of the VCBACKUP section?

Steve
What are you trying to do here, looks like appending each next variable onto the end of the last?

      Set _enclName=!_enclName!%%A
      Set _folderName=!_folderName!%%B
      Set _oaIP=!_oaIP!%%C

You also had a " missing on line 30

How about all in one simplifying by putting it all into the loop?

@echo off

:: Temporarily add plink to PATH
set PATH=C:\Program Files\Plink;%PATH%

SetLocal EnableDelayedExpansion

:: Process each input line:
FOR /F "tokens=1-3 delims=," %%A IN (c7000-list.csv) DO (

:: Make directories for each c7000 chassis along with a folder with today's date to store the c7000 config backups
   md ".\%%~B"
   md ".\%%~B\%date:~10,4%.%date:~4,2%.%date:~7,2%"

:: Connect to SSH host and store key in cache
   echo y | plink -ssh admin@%%C -pw <PWD> exit

:: Run Plink to backup OA config
   plink.exe -m "D:\c7000_config_backups\ShowAllCmd.txt" -pw <PWD> admin@%%C >>".\%%B\%date:~10,4%.%date:~4,2%.%date:~7,2%"\%_%%A-OA-ShowAll.txt 2>>&1 

:: Run VCSU to backup Virtual Connect config
   "C:\Program Files (x86)\Hewlett-Packard Company\Virtual Connect Support Utility\vcsu.exe" -a configbackup -i %_oaIP% -u admin -p <PWD> -vcu admin -vcp <PWD> -l ".\%%B\%date:~10,4%.%date:~4,2%.%date:~7,2%"\vcConfig-%%A_vc_domain

)

Open in new window

Avatar of Colin Rosario
Colin Rosario

ASKER

@Gabriel Clifton - I removed the function calls and placed the function code in the loop to simplify things.

@Steve Knight - I originally had all the code running within the FOR loop, but I was having problems with it, so I moved to calling functions. As suggested, I moved the functions back into the loop to simplify things. As for the initial token variables, I think you're right about the appending, so I've corrected it. I've also added the missing ".

Updated code posted below, although still having problems. The loop is now echoing all the variables on subsequent passes, but the variables are still not updating correctly. The first pass through the loop works fine, but the second produces the following output:

enclName=!_enclName!
folderName=!_folderName!
oaIP=!_oaIP!

Open in new window


Here's the updated script...
@echo off

:: Temporarily add plink to PATH
set "PATH=C:\Program Files (x86)\Plink;%PATH%"

SetLocal EnableDelayedExpansion

:: Store input line into different variables
FOR /F "tokens=1-3 delims=," %%A IN (c7000-list.csv) DO (
	Set "_enclName=%%A"
	Set "_folderName=%%B"
	Set "_oaIP=%%C"
	
	:: Echo statements for troubleshooting & reviewing contents of variables
	echo enclName=!_enclName!
	echo folderName=!_folderName!
	echo oaIP=!_oaIP!
	
	:: Make directories for each c7000 chassis along with a folder with today's date to store the c7000 config backups
	md ".\%_folderName%"
	md ".\%_folderName%\%date:~10,4%.%date:~4,2%.%date:~7,2%"
	
	:: Connect to SSH host and store key in cache
	SetLocal DisableDelayedExpansion
	echo y | plink -ssh admin@%_oaIP% -pw <PWD> exit
	
	:: Run Plink to backup OA config
	plink.exe -m "D:\c7000_config_backups\ShowAllCmd.txt" -pw <PWD> admin@%_oaIP% >>".\%_folderName%\%date:~10,4%.%date:~4,2%.%date:~7,2%"\%_enclName%-OA-ShowAll.txt 2>>&1 
	
	:: Run VCSU to backup Virtual Connect config
	"C:\Program Files (x86)\Hewlett-Packard Company\Virtual Connect Support Utility\vcsu.exe" -a configbackup -i %_oaIP% -u admin -p <PWD> -vcu admin -vcp <PWD> -l ".\%_folderName%\%date:~10,4%.%date:~4,2%.%date:~7,2%"\vcConfig-%_enclName%_vc_domain
)

Open in new window

You were turning off delayed expansion for some reason?  Also quotes around SET statements not needed.  Was only a quick look.. if still no good will look again.

Steve

@echo off

:: Temporarily add plink to PATH
set PATH=C:\Program Files (x86)\Plink;%PATH%

SetLocal EnableDelayedExpansion

:: Store input line into different variables
FOR /F "tokens=1-3 delims=," %%A IN (c7000-list.csv) DO (
	Set _enclName=%%A
	Set _folderName=%%B
	Set _oaIP=%%C
	
	:: Echo statements for troubleshooting & reviewing contents of variables
	echo enclName=!_enclName!
	echo folderName=!_folderName!
	echo oaIP=!_oaIP!
	
	:: Make directories for each c7000 chassis along with a folder with today's date to store the c7000 config backups
	md ".\%_folderName%"
	md ".\%_folderName%\%date:~10,4%.%date:~4,2%.%date:~7,2%"
	
	:: Connect to SSH host and store key in cache
	echo y | plink -ssh admin@%_oaIP% -pw <PWD> exit
	
	:: Run Plink to backup OA config
	plink.exe -m "D:\c7000_config_backups\ShowAllCmd.txt" -pw <PWD> admin@%_oaIP% >>".\%_folderName%\%date:~10,4%.%date:~4,2%.%date:~7,2%"\%_enclName%-OA-ShowAll.txt 2>>&1 
	
	:: Run VCSU to backup Virtual Connect config
	"C:\Program Files (x86)\Hewlett-Packard Company\Virtual Connect Support Utility\vcsu.exe" -a configbackup -i %_oaIP% -u admin -p <PWD> -vcu admin -vcp <PWD> -l ".\%_folderName%\%date:~10,4%.%date:~4,2%.%date:~7,2%"\vcConfig-%_enclName%_vc_domain
)

Open in new window

@Steve Knight - I overlooked your last comment where you posted an edited and simplified version of my script using just the token variables. I like it...very simple. Guess we over-think these things at times. I've run it on 2 records and it looks like it's working as desired. I'm going to perform a few more tests and if they're successful, I'll award the points. Thanks!
np, shout if not.

Steve
@Steve Knight - Your code works, with the exception of the following line. The file name of the 'Show All' text file is getting created as "A-OA-ShowAll.txt". It's missing the dynamic name value that should be pulled from the first column of the CSV file. I tried removing the leading "%_", but that didn't work either.

plink.exe -m "D:\c7000_config_backups\ShowAllCmd.txt" -pw <PWD> admin@%%C >>".\%%B\%date:~10,4%.%date:~4,2%.%date:~7,2%"\%_%%A-OA-ShowAll.txt 2>>&1 

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Works perfectly now. Many thanks for your guidance and expertise!
Great. Glad we got there in the end!