Solved

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

Posted on 2014-11-07
11
260 Views
Last Modified: 2014-11-09
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

0
Comment
Question by:Colin Rosario
  • 6
  • 4
11 Comments
 
LVL 13

Expert Comment

by:Gabriel Clifton
ID: 40429967
You are not calling the subscripts with the variables you want to use. Ie call :sub1 %%a
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40429999
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
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40430002
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

0
 

Author Comment

by:Colin Rosario
ID: 40430402
@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

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40430432
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

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Colin Rosario
ID: 40430477
@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!
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40430620
np, shout if not.

Steve
0
 

Author Comment

by:Colin Rosario
ID: 40430825
@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

0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
ID: 40430986
I think this should correct any remaining issues?


@echo off

:: Temporarily add plink to PATH
set PATH=C:\Program Files\Plink;%PATH%
Set TODAY=%date:~10,4%.%date:~4,2%.%date:~7,2%

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\%today%"

:: 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\%today%\%%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 %%C -u admin -p <PWD> -vcu admin -vcp <PWD> -l ".\%%B\%today%\vcConfig-%%A_vc_domain"

)

Open in new window

You seem to be putting quite a few passwords in there.  Are they all the same?  If so for starters put

SET PASSWORD=whaterver

Open in new window


and then use %password% but you can also hide the passwords a little at least by using techniques like this:

http://scripts.dragon-it.co.uk/links/batch-prompt-password-save-it

e.g. this at the top of your script, then use %password% where you need to specify the password

@echo off
set /p password=<%~nx0:password

if not "%password%"=="" goto OK
set /p pwd=Enter password to store:
echo %pwd%>%~nx0:password
set password=%pwd%
exit /b
:OK

Open in new window


Steve
0
 

Author Closing Comment

by:Colin Rosario
ID: 40431988
Works perfectly now. Many thanks for your guidance and expertise!
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40432090
Great. Glad we got there in the end!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now