programming4us
programming4us
DESKTOP

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

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

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
Free Mobile And Desktop Apps For Accessing Restricted Websites
MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
TOYOTA CAMRY 2; 2.5 : Camry now more comely
KIA SORENTO 2.2CRDi : Fuel-sipping slugger
How To Setup, Password Protect & Encrypt Wireless Internet Connection
Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
Backup & Restore Game Progress From Any Game With SaveGameProgress
Generate A Facebook Timeline Cover Using A Free App
New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
- Messages forwarded by Outlook rule go nowhere
- Create and Deploy Windows 7 Image
- How do I check to see if my exchange 2003 is an open relay? (not using a open relay tester tool online, but on the console)
- Creating and using an unencrypted cookie in ASP.NET
- Directories
- Poor Performance on Sharepoint 2010 Server
- SBS 2008 ~ The e-mail alias already exists...
- Public to Private IP - DNS Changes
- Send Email from Winform application
- How to create a .mdb file from ms sql server database.......
programming4us programming4us
programming4us
 
 
programming4us