DESKTOP

Windows Server 2008 : Creating Batch Files (part 1) - Using Notepad, Giving Feedback with echo, Using Parameters

4/9/2013 3:54:01 AM

1. Using Notepad

Most students who have attended one of my classes on the command prompt, batch files, or PowerShell have heard me say this: “The difference between a good administrator and a great administrator is that great administrators have learned to script.”

You don’t even have to be a great scripter, but when you can automate some of your tasks, you can do much more than the good administrator who does tasks manually. You can start scripting by creating basic batch files in Notepad. One of the great things about the command prompt is that anything you can execute at the command prompt, you can embed as a script in a batch file, and anything you can script, you can automate.

Although there are more advanced tools you can use to create batch files, the simplest tool can meet most of your needs. Notepad is a simple text editor, and you can easily use it to create batch files. As a simple example, you can use the following steps to create a batch file in Notepad.

StepsComments
1. Type c:\>md c:\scriptsCreates a folder named scripts at the root of c:.
2. Type c:\>cd c:\scriptsChanges the path to the c:\scripts folder.
3. Type c:\scripts>notepad sync.batLaunches Notepad and creates the batch file named sync.bat. When prompted to create the file, click Yes.
4. Type the following line in the myfirstcript. bat file repadmin /syncallCreates a one-line script in the batch file.
5. Press Ctrl+S to save the batch file.Saves the batch file. You can now execute it from the command line.
6. Type c:\scripts>syncExecutes the script. The command repadmin /syncall forces replication between all domain controllers. This includes zone information in Active Directory Integrated zones.

You can also create a batch file by just launching Notepad with the GUI or by typing Notepad without specifying the batch file name. The only difference is that when you save it, you need to ensure that you save it with a .bat extension. You do this by entering the filename as scriptname.bat. Notepad will still save it as a text file but with a .bat extension.

Tip

If you don’t add the .bat extension, Notepad defaults to a .txt extension and any file with a .txt extension does not execute as a batch file.

2. Giving Feedback with echo

The echo command can be used to display messages to users from within a batch file.  For example, if you just want to know the path to the Program Files folder, you can use this command:

echo %programfiles%

However, echo has a quirk that can be confusing. If you use echo with a message from within a batch file, you’ll see the echo command with the message and then the message again on the same line. For example, consider a file named synctest.bat with this line as you look at the following table:

echo This will synchronize DCs

Batch File ContentsResult When Executed at Command PromptComments
echo This will
synchronize DCs

c:\>synctest
c:\>echo This will
synchronize DCs This
will synchronize DCs

Notice that the echo line shows, and then the result of the echo line shows.
echo off
echo This will
synchronize DCs

c:\>synctest
c:\>echo off
This will synchronize
DCs

The echo off command shows but the echo command isn’t shown.
@echo off
echo This will
synchronize DCs

c:\>synctest
This will synchronize
DCs

The @echo off command turns off echo and also does not display the line.
@echo off
echo.
echo This will
synchronize DCs

c:\>synctest
This will synchronize
DCs

The echo. command displays a blank line (when a period is placed after the word echo as in echo.).

Tip

If you enter echo off at the command prompt, the command prompt (c:\>) disappears, but you can still enter commands as normal. If you turn off echo from within a batch file, it does not affect the command prompt.


Note

If you want to include the pipe (|), the less-than character (<), the greater-than character (>), or the caret (^) symbol in an echo line, you need to precede the character with a caret (^). For example, if you want to send the output of the command to a text file with the > symbol, you would have to list it as ^>.

3. Using Parameters

Windows Server 2008 supports the use of parameters in batch files. You define the parameter in the batch file, and then you can pass data to the batch file as a parameter. You can use as many as nine parameters, and they are defined as %1 through %9.

As an example, consider the whoami command. You can enter the following command to view the SID of the currently logged-on user:

whoami /user

You see information similar to the following:

USER INFORMATION
----------------

User Name      SID
============== ==============================================
pearson\darril S-1-5-21-4285671909-4150961583-1987988917-1000

You can create a one-line batch file named who.bat with the following line:

whoami /%1

who.bat Batch File ContentsResult When Executed at Command PromptComments
whoami /%1
c:/>who user
USER INFORMATION
----------------
User Name      SID
=====================================
pearson\darril S-1-5-21-4285671909-
4150961583-987988917-1000

This command (whoami /%1) actually runs the following command by substituting user for %1:whoami /user

Although the previous example is simplistic, it does show how a parameter is used. The following example shows a more usable batch file that can be used to set the IP address, subnet mask, and default gateway of a Windows Server 2008 Server Core system from the command line. Note that even though it runs to two lines in the text, it is entered on a single line.

netsh interface ipv4 set address name = "local area connection"
  static 10.10.0.10 255.0.0.0 10.10.0.1

The previous command sets the IP address to 10.10.0.10, the subnet mask to 255.0.0.0, and the default gateway to 10.10.0.1.

You can create a batch file named setip.bat with the following line using parameters. The batch file accepts three parameters identified as %1, %2, and %3:

netsh interface ipv4 set address name = "local area connection"
static %1 %2 %3

setip.bat Batch File ParametersExecuting setip.bat at Command PromptComments
%1 %2 %3

c:/>setip 10.10.0.10
255.0.0.0 10.10.0.1

This command sets the IP address, subnet mask, and default gateway using the setip.bat file.

You can add a second line to your batch file to also set the address of the DNS server. Notice that the %4 parameter is the actual IP address of the DNS server.

netsh interface ipv4 set address name="Local Area Connection"
  static %1 %2 %3
netsh interface ipv4 set dnsserver "Local Area Connection" static %4

setip.bat Batch File ParametersExecuting setip.bat at Command PromptComments
%1 %2 %3 %4
c:/>setip 10.10.0.10
255.0.0.0 10.10.0.1
10.10.0.5

This command sets the IP address, subnet mask, default gateway, and DNS server address using the setip.bat file.
Other  
 
Top 10
3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
3 Tips for Maintaining Your Cell Phone Battery (part 1) - Charge Smart
OPEL MERIVA : Making a grand entrance
FORD MONDEO 2.0 ECOBOOST : Modern Mondeo
BMW 650i COUPE : Sexy retooling of BMW's 6-series
BMW 120d; M135i - Finely tuned
PHP Tutorials : Storing Images in MySQL with PHP (part 2) - Creating the HTML, Inserting the Image into MySQL
PHP Tutorials : Storing Images in MySQL with PHP (part 1) - Why store binary files in MySQL using PHP?
Java Tutorials : Nested For Loop (part 2) - Program to create a Two-Dimensional Array
Java Tutorials : Nested For Loop (part 1)
REVIEW
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
VIDEO TUTORIAL
- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
Popular Tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS