Replaceable parameters in batch file

This article is from our Febooti archive, it was relevant then, and I think that it is still relevant today (a few details changed).
Previous article: Stopping a DOS batch file.

console-wishmesh
Photo: freeimages.com

With the information covered so far we are able to use batch files to make our lives easier, and help our day to day computing operations. However, we could make our batch scripts more sophisticated by avoiding the big limitation that we have been faced with so far. That is – our files are hard-coded (written in stone, so to speak) and if we want to manipulate a file, we have to change the batch script again and again.

What if we wanted to perform a particular operation on a different file each time? We can use replaceable parameters to help us with this task. The two important factors in using replaceable parameters are – how to code them in the batch file, and second – how to give the values for the replaceable parameters to the batch file. The latter is rather quite easy. Replaceable parameters are to be entered after the batch file name and before pressing the Enter key. For example:

C:\>batch-params.bat Par1 Par2 Par3 Par4 Par5

Either a space or a comma character separates each parameter. Both are treated the same. Now, how many replaceable parameters does the above line have? The obvious answer would be five. This would be wrong. DOS counts the name of the batch file itself as a parameter, so there are 6 replaceable parameters on this line. Much like other programming languages DOS names them %0, %1, %2, %3, %4, %5 rather than %1 through %5.

DOS uses replaceable parameters as if they do not exist – for example, everywhere you see a replaceable parameter, the DOS sees the value of that parameter. When a batch script executes with ECHO ON, the DOS precedes each command in the batch file prompt. DOS treats the commands in the batch file exactly like it would if you entered them from the DOS prompt.

:: batch-params.bat
:: Example with Replaceable Parameters.
ECHO %1
ECHO %2
SHIFT
ECHO %2

The first 2 lines are remark statements that show the name of the file and a brief description of the program. The third line echo’s the first replaceable parameter entered on the command line. If replaceable parameter was not entered, the command will display the Echo is on message (just like with simple ECHO command). The fourth line shows the 2nd parameter. The fifth line shifts the argument. The line 6 acts like line 4, but because of SHIFT, it and echo’s third argument. SHIFT command allows to use more than 10 parameters / arguments in the batch file:

SHIFT changes the values of the batch parameters %0 through %9 by copying each parameter into the previous one. In other words, the value of %1 is copied to %0, the value of %2 is copied to %1, and so on. More in MS Help site.

The SHIFT command discards the first parameter %0, moves the remaining parameters down one value, and brings in a new value if one exists. Therefore after the SHIFT command gets read, the value in %1 is moved into %0, and the value in %2 moves into %1 and so on and so forth.

The SHIFT command really has two major purposes. First, by moving the parameters down into a lower replaceable parameter, a single replaceable parameter may be used for all coding by forcing the batch script to loop through the code. Second, it allows the batch file to handle more than nine replaceable parameters. The total number of replaceable parameters is constrained by the 127-character command line limitation (for old OSes), and starting Windows XP or later you can use longer command line – up to 8191 characters. A space, comma or semicolon is also a requirement to separate each replaceable parameter.

If you start our example batch file with the parameters: text1, text2, text3 and text4, you will see the following output:
C:\>batch-params.bat text1 text2 text3 text4
C:\>ECHO text1
text1
C:\>ECHO text2
text2
C:\>SHIFT
C:\>ECHO text3
text3
C:\>

And, note: we do not access fourth parameter (%4), so it is not shown in the console. Also, see the screenshot from Windows 8.1 CMD.exe:

Showing output example from the batch file with parameters
Showing output example from the batch file with parameters

Next article: Loops in the Batch files with examples.

This article is from our Febooti archive, it was relevant then, and I think that it is still relevant today (a few details changed).

Update Feb 21, 2015. Added link to the next article.

VMware vSphere Client could not connect to vCenter version 5.1.0

Recently we had an issue, where we were unable to connect to one of our VMware ESXi vCenter’s (v5.1.0) with vSphere Client. Everything worked, all virtual machines were up, except vCenter.

Digging deeper, the log files revealed the following:
2014-09-19T09:01:09.494+03:00 [02156 warning 'Default'] [VdbStatement] SQL execution failed: cleanup_events_tasks_proc
2014-09-19T09:01:09.494+03:00 [02156 warning 'Default'] [VdbStatement] Execution elapsed time: 69482 ms
2014-09-19T09:01:09.494+03:00 [02156 warning 'Default'] [VdbStatement] Diagnostic data from driver is 42000:1:9002:[Microsoft][SQL Server Native Client 10.0][SQL Server]The transaction log for database 'VIM_VCDB' is full. To find out why space in the log cannot be reused, see the log_reuse_wait_desc column in sys.databases
2014-09-19T09:01:09.494+03:00 [02156 warning 'Default'] [VdbStatement] Bind parameters:
2014-09-19T09:01:09.494+03:00 [02156 error 'Default'] [Vdb::IsRecoverableErrorCode] Unable to recover from 42000:9002
2014-09-19T09:01:09.494+03:00 [02156 error 'Default'] [VdbStatement] SQLError was thrown: "ODBC error: (42000) - [Microsoft][SQL Server Native Client 10.0][SQL Server]The transaction log for database 'VIM_VCDB' is full. To find out why space in the log cannot be reused, see the log_reuse_wait_desc column in sys.databases" is returned when executing SQL statement "cleanup_events_tasks_proc"
2014-09-19T09:01:09.494+03:00 [02156 error 'vpxservicesvpxdEventTaskCleanup'] [VpxdEventTaskCleanup] Error during Event/Task Cleanup: "ODBC error: (42000) - [Microsoft][SQL Server Native Client 10.0][SQL Server]The transaction log for database 'VIM_VCDB' is full. To find out why space in the log cannot be reused, see the log_reuse_wait_desc column in sys.databases" is returned when executing SQL statement "cleanup_events_tasks_proc"
2014-09-19T09:01:09.494+03:00 [02156 warning 'VpxProfiler'] VpxUtil_InvokeWithOpId [TotalTime] took 69482 ms

So we need to clean up the Transaction log for the MS SQL server, or clean up some tables… so we found an article from VMware support – KB1025914.

From VMware, to purge the data in the VPX_EVENT table:

1. Connect to Servername\SQL Database and log in with the appropriate credentials.
2. Click databases to expand and select VIM_VCDB > Tables.
3. Right-click the dbo.VPX_PARAMETER table and select Open.

Note: If you are using SQL Server 2008, right-click the dbo.VPX_PARAMETER table and click Edit Top 200 Rows.
4. Modify event.maxAge to 30, and modify the event.maxAgeEnabled value to true.
5. Modify task.maxAge to 30, and modify the task.maxAgeEnabled value to true.

Note: To improve the time of the data cleanup, run the preceding steps in several intervals. To do this, ensure to keep the default value of event.maxAge and task.maxAge and perform step 6 to run the cleanup. Then, reduce the event.maxAge andtask.maxAge value by 60 and run the cleanup. Repeat the steps until the value is reached to 30 for the final cleanup process.
6. Run the built-in stored procedure:
a. Go to VIM_VCDB > Programmability > Stored Procedures.
b. Right-click dbo.cleanup_events_tasks_proc and select Execute Stored Procedure.

This purges the data from the vpx_event, vpx_event_arg, and vpx_task tables based on the date specified for maxAge.
c. When this has successfully completed, close SQL Management Studio and start the VMware Virtual Center Server service.
To ensure that the default Statistics Level is set to 1:

1. Using the vSphere Client, log into vCenter Server as an administrator.
2. Go to Administration > vCenter Server Settings > Statistics.
3. Under Statistics Intervals, ensure the Statistics Level column is set to 1.
4. To change the value, select the Interval Duration, click Edit and select Level 1 from the dropdown

To download SQL Management Studio 2008 R2 follow this Microsoft SQL Server 2008 R2 RTM – Management Studio Express.

Everything went smooth, except, we didn’t know the SQL server name, and SQL Server 2008 R2 didn’t help much. Later we found, that the name is “.\VIM_SQLEXP”, see the screenshot below.

Stopping a DOS batch file

This article is from our Febooti archive, it was relevant then, and I think that it is still relevant today (a few details changed).
Previous article: First cmd / DOS batch script example.

Usually, OS (Windows, MS-DOS) executes a batch file without stopping. DOS stops processing the batch file while the program runs when the batch file loads a program. As soon as you exit the program, or an automatic termination occurs, OS immediately continues processing the batch file from where it left off. Typically this is what you want, but sometimes you want to give the user time to think, react, and then take action. An example would be giving the user a group of choices that you want them to select from before proceeding.

PAUSE is the command we are looking for. PAUSE stops the batch file until almost any key is pressed. A key that would produce a character on the screen needs to be pressed. Therefore pressing a key like CTRL or ALT or SHIFT will not exit out of the pause state. Earlier we used this command above to see the output on the console. Now we will use this to pause the execution of the batch script.

Though the PAUSE command is really quite helpful, it lacks in power. The keystroke that you press is not capable of being used as a menu selection nor can it be saved in the program in any other way. Below is an example to illustrate how the PAUSE command works along with the ways of terminating a program with the keyboard.

:: PAUSE-IT.bat
:: Example of the PAUSE command
:: and the ways to exit out of a program.
@ECHO OFF
ECHO The next command is a PAUSE
ECHO Do NOT press Ctrl+C or Ctrl+Break
PAUSE
ECHO Press Ctrl+C or Ctrl+Break on your keyboard
PAUSE

The first line is a comment with the name of the file. The second and third line comments are description of the program (batch file). The fourth line turns command-echoing off. Line 5 and 6 gives the first message about which keys you can press for the first example. Line 7 is the PAUSE command that stops the batch file until a key is pressed. The next line (8) displays the message about aborting the program using the keyboard controls. The last line (9) is the final PAUSE that finishes the program.

You can terminate this script in many ways, however, if you follow the ECHO instructions, then you will have the following output:

Batch file: press CTRL+Break CTRL+C
Batch file: press CTRL+Break CTRL+C

As you see in the screenshot, after pressing Ctrl+Break or Ctrl+C, the command interpreter asks you to “Terminate batch job (Y/N)?”. By pressing Y followed by the Enter, the batch file terminates.

Next article: Replaceable parameters in batch file.

UPDATE: Jan 15, 2015… added link to the next article.

This article is from our Febooti archive, it was relevant then, and I think that it is still relevant today (a few details changed).