Showing posts with label Commanf Prompt Overview. Show all posts
Showing posts with label Commanf Prompt Overview. Show all posts

Monday, 30 June 2014

Managing Windows Systems with Command

Managing Windows Systems

As an administrator, it’s your job to plan, organize, and track the details that keep the network running. If you’re to survive without just muddling through, you need to learn how to do those jobs quickly and efficiently. Fortunately, Windows supplies plenty of command-line tools to help you with these tasks and this chapter discusses some of the more important tools for daily systems management.

Examining System Information

Often when you are working with a user’s computer or a remote server, you’ll want to examine some basic system information, such as who is logged on, the current system time, or the location of a certain file. Commands that help you gather basic system information include
  • NOW  Displays the current system time and date using a 24-hour clock, such as Sat May 9 12:30:45 2003. Available in the Windows Server 2003 Resource Kit only.
  • WHOAMI  Displays the name of the user currently logged on the system, such as adatum\administrator.
  • WHERE  Searches for files using a search pattern and returns a list of matching results.
To use NOW or WHOAMI, simply type the command in a command shell window and press Enter. With WHERE, the most common syntax you’ll use is
where /r baseDir filename
Here, /r is for a recursive search starting from the specified directory (\BaseDir) and including all subdirectories, and filename is the name or partial name of the file to search for, which can include wildcards. Use ? as a wildcard to match a single character and * as a wildcard to match multiple characters, such as data???.txt or data*.*. In the following example, you search the C:\ directory and all subdirectories for text files that begin with data, as follows:
where /r C:\ data*.txt
You can also search for files of all types that begin with data, as in this example:
where /r C:\ data*.*
Sometimes when you are working with a computer, you’ll want to obtain information on the system configuration or the system environment. With mission-critical systems, you may want to save or print this information for easy reference. Commands that help you gather system information include
  • DRIVERQUERY  Displays a list of all installed device drivers and their properties, including module name, display name, driver type, and driver link date. With verbose output, the command also lists the driver status, state, start mode, memory usage, and file system path. Use the /V parameter to get verbose output of all unsigned drivers.
  • SYSTEMINFO  Displays detailed system configuration information, including operating system version, system type, system manufacturer, processor, BIOS version, memory size, local setting, time zone setting, and network card configuration.
  • NLSINFO  Displays detailed locale information, including default language, system locale, windows code page, time and number formats, time zone, and installed code pages. Available in the Windows Server 2003 Resource Kit only.
To use these commands on a local computer, simply type the command name in a command shell window and press Enter. With DRIVERQUERY, use the /V parameter to get verbose output and the /Si parameter to display properties of signed drivers, such as
driverquery /v /si
With the DRIVERQUERY and SYSTEMINFO commands, you can also specify the remote computer to query and the Run As permissions. To do this, you must use the expanded syntax, which includes the following parameters:
/S Computer /U [Domain\]User [/P Password]
where Computer is the remote computer name or IP address, Domain is the optional domain name in which the user account is located, User is the name of the user account whose permissions you want to use, and Password is the optional password for the user account. If you don’t specify the domain, the current domain is assumed. If you don’t provide the account password, you are prompted for the password.
To see how the computer and user information can be added to the syntax, consider the following examples:
Use the account adatum\wrstanek when querying MAILER1 for driver settings:
driverquery /s mailer1 /u adatum\wrstanek
Use the account adatum\administrator when querying CORPSERVER01 for system information:
systeminfo /s corpserver01 /u adatum\administrator
Tip 
The basic output of these commands is in table format. You can also format the output as a list or lines of comma-separated values using /Fo List or /Fo Csv, respectively. You may wonder why you should use the various formats. That’s a good question. I recommend using the verbose list format (/Fo List /V) when you want to see all details about tasks configured on a system and when you are troubleshooting. I recommend using comma-separated values when you want to store the output in a file that may later be exported to a spreadsheet or flat-file database. Remember you can redirect the output of the DRIVERQUERY and SYSTEMINFO commands to a file using output redirection (> or >>).

Saturday, 28 June 2014

Common Statements and Commands for Scripts in Windows CMD

Common Statements and Commands for Scripts

So far in this book, I’ve discussed commands but haven’t really introduced what a statement is. While these terms are often used interchangeably, the term statement technically refers to the keyword for a command, such as the rem statement, but it can also refer to a line of code that includes all the command text on that line. In some programming languages, such as Java, each statement must be terminated with a specific character. With Java, the terminator is a semicolon. The command line doesn’t look for a specific terminator, other than the end of the line, which is assumed when the command interpreter reads any of the following:
  • Line break (such as when you press Shift+Enter)
  • Carriage return and line break (such as when you press Enter)
  • End-of-file marker
Now that we’ve discussed how to create scripts, let’s look at common statements and commands you’ll use in scripts, including
  • Cls  Clears the console window and resets the screen buffer
  • Rem  Creates comments in scripts
  • Echo  Displays messages at the command line and turns command echoing on or off
  • @  Controls command echo on a line-by-line basis
  • Title  Sets the title for the command shell window
  • Color  Sets the text and background colors used in the command shell window

Clearing the Command-Shell Window

Clearing the command-shell window before writing script output is usually a good idea. You clear the command-shell window using the CLS command. Why not try it? At the command line, type cls and press Enter. The console window clears and the cursor is positioned in the top left corner of the window. All the text in the screen buffer is cleared as well.
You could add the CLS command to the sample script listed previously, as shown in this example:
cls
hostname
ver
ipconfig -all

Adding Comments to Scripts

You use the rem statement to add comments to your scripts. Every script you create should have comments detailing
  • When the script was created and last modified
  • Who created the script
  • What the script is used for
  • How to contact the script creator
  • Whether and where script output is stored
Not only are the answers to these Who, What, How, Where, and When questions important for ensuring that the scripts you create can be used by other administrators, they can also help you remember what a particular script does, especially if weeks or months have passed since you last worked with the script. An example of a script that uses comments to answer these questions is shown as Listing 3-2.

Listing 3-2: Updated Sample Script with Comments
Start example
rem ************************
rem Script: SystemInfo.bat
rem Creation Date: 2/2/2004
rem Last Modified: 3/15/2004
rem Author: William R. Stanek
rem E-mail: williamstanek@aol.com
rem ************************
rem Description: Displays system configuration information
rem including system name, IP configuration
rem and Windows version.
rem ************************
rem Files: Stores output in c:\current-sys.txt.
rem ************************
hostname > c:\current-sys.txt
ver >> c:\current-sys.txt
ipconfig -all >> c:\current-sys.txt
End example
Later in this chapter, in the section titled “Passing Arguments to Scripts,” I’ll show you how to use your comments as automated help documentation. Before we get to that, however, keep in mind that rem statements can also be used to
  • Insert explanatory text within scripts, such as documentation on how a procedure works.
  • Prevent a command from executing. On the command line, add rem before the command to comment it out.
  • Hide part of a line from interpretation. Add rem within a line to block interpretation of everything that follows the rem statement.

Managing Text Display and Command Echoing

The echo command has two purposes. You use the ECHO command to write text to the output, which can be the command shell or a text file. You also use the ECHO command to turn command echoing on or off. Normally, when you execute commands in a script, the commands as well as the resulting output of the command are displayed in the console window. This is called command echoing.
To use the ECHO command to display text, enter echo followed by the text to display, such as
echo The system host name is:
hostname
To use ECHO to control command echoing, type echo off or echo on as appropriate, such as
echo off
echo The system host name is:
hostname
Use output redirection to send output to a file rather than the command shell, as follows:
echo off
echo The system host name is: > current.txt
hostname >> current.txt
To experiment with suppressing command echoing, start a command shell, type echo off, and then enter other commands. You’ll find that the command prompt is no longer displayed. Instead, you see only what you type into the console window and the resulting output from the commands you’ve entered. In scripts, the ECHO OFF command turns off command echoing as well as the command prompt. By adding the command ECHO OFF to your scripts, you keep the command-shell window or the output file from getting cluttered with commands when all you care about is the output from those commands.
Tip 
By the way, if you want to determine whether command echoing is enabled or disabled, type the ECHO command by itself. Give it a try. If command echoing is on, you’ll see the message Echo Is On. Otherwise, you’ll see the message Echo Is Off. Experiment with the ECHO OFF command in your scripts and you may detect a bit of a problem here. If the ECHO OFF command turns off command echoing, how do you prevent the ECHO OFF command itself from echoing? Don’t worry; that’s discussed in the next section.
Real World 
One question that other command-line programmers frequently ask me is, How do you get a blank line to echo in the command shell? You might think that putting the ECHO command on a line by itself would do the job, but it doesn’t. Typing echo on a line by itself displays the status of command echoing as mentioned in the previous tip. Typing echo followed by a space doesn’t work either, because the Windows command line treats spaces (in this situation) as meaningless and you get the same results as typing echo followed by nothing at all. To get ECHO to display a blank line, you must enter echo. (Note that there is no space between the period and the ECHO command.)

Fine-Tuning Command Echo with @

The @ command prevents commands from echoing to the output on a line-by- line basis and you can think of it as a line-specific echo off statement. You could use @ to turn off command echoing like this:
@echo The system host name is:
@hostname
Using @, the output that shows the command prompt and commands like this:
C:\>echo The system host name is:
The system host name is:
C:\>hostname
mailer1
becomes
The system host name is:
mailer1
But the real value of @ is that it allows you to tell the command shell not to display the command prompt or ECHO OFF command, and thereby ensures that the only output of your scripts is the output of the commands you enter. Here is an example of a script that uses @ to hide the ECHO OFF command so that it isn’t displayed in the output:
@echo off
echo The system host name is:
hostname
The output from this script is
The system host name is:
mailer1
Tip 
I recommend using @echo off at the beginning of all your command-line scripts. By the way, if you start a command shell and type @echo off, you can turn off the display of the command prompt as well.

Setting the Console Window Title and Colors

If you’re going to take the time to write a script, you might as well add a few special features to jazz it up. Some of the basic techniques that I’ve already discussed are using the ECHO OFF command and clearing the console window before you write output. You may also want to set a title for the window or change the colors the window uses.
The title bar for the command shell is located at the top of the console window. Normally, this title bar displays “Command Prompt” or the file path to the command shell. You can customize the title using the TITLE command. This command works much like the ECHO command in that it displays whatever text follows it on the console’s title bar. For example, if you wanted to set the title of the current console to “System Information,” you could do this by entering the following at the command line:
title System Information
You can not only use the TITLE command to show the name of the script that is running; you can also use TITLE to show the progress of the script as it executes, such as
rem add blocks of work commands
title Gathering Information
rem add blocks of logging commands
title Logging System Information
By default, the console window displays white text on a black background. As you learned in Chapter 1, “Overview of the Windows Command Line,” you can modify this behavior using the Colors tab of the Command Prompt Properties dialog box. You can also set console colors by using the COLOR command. You do this by passing the command a 2-digit hexadecimal code. The first digit corresponds to the background color and the second digit corresponds to the text color, such as
color 21
which sets the text to blue and the background color to green.
The color codes you can use with the COLOR command are shown in Table 3-1. Keep in mind that you can’t set the text and background colors to the same value. If you try to do this, the color doesn’t change. Additionally, you can restore the default colors at any time by using the COLOR command without any arguments, such as
color
Table 3-1: Color Codes for the Command Shell Window
Code
Color
Code
Color
0
Black
8
Gray
1
Blue
9
Bright Blue
2
Green
A
Bright Green
3
Aqua
B
Bright Aqua
4
Red
C
Bright Red
5
Purple
D
Bright Purple
6
Yellow
E
Bright Yellow
7
White
F
Bright White

Creating Command Line Scripts in Windows

Creating Command Line Scripts

Command line scripts are text files containing the commands you want to execute. These are the same commands you would normally type into the Windows command shell. However, rather than enter the commands each time you want to use them, you create a script to store the commands for easy execution.
Because scripts contain standard text characters, you can create and edit scripts using a standard text editor, such as Notepad. When you enter commands, be sure to place each command or group of commands that should be executed together on a new line. This ensures proper execution of the commands. When you have finished creating a command-line script, save the script file using the .bat or .cmd extension. Both extensions work with command-line scripts in the same way. For example, if you wanted to create a script to display the system name, Windows version and IP configuration, you could enter these three commands into a file called SysInfo.bat or SysInfo.cmd:

hostname
ver
ipconfig -all
Once you save the script, you can execute it as if it were a Windows utility; simply type the name of the script in a command shell and press Enter. When you do this, the command shell reads the script file and executes its commands one by one. It stops executing the script when it reaches the end of the file or reads an EXIT command. For the example script, the command line would display output similar to Listing 3-1.
Listing 3-1: Output of Sample Script
Start example
C:\>hostname
mailer1
C:\>ver
Microsoft Windows [Version 5.2.3790]
C:\>ipconfig -all
Windows IP Configuration
Host Name . . . . . . . . . . . . : mailer1
Primary Dns Suffix . . . . . . . : adatum.com
Node Type . . . . . . . . . . . . : Unknown
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No
DNS Suffix Search List. . . . . . : adatum.com
Ethernet adapter Local Area Connection:
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Intel(R) PRO/100 VE Network
Connection
Physical Address. . . . . . . . . : X0-EF-D7-AB-E2-1E
DHCP Enabled. . . . . . . . . . . : No
IP Address. . . . . . . . . . . . : 192.168.10.50
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.10.1
DNS Servers . . . . . . . . . . . : 192.168.10.155
End example
If you examine the listing, you’ll see that the command prompt and the actual commands are displayed as well as the output of the commands themselves. The reason for this is that the command shell does some extra work behind the scenes while executing scripts in the default processing mode. First, the command shell displays the command prompt. Next, it reads a line from the script, displays it, and then interprets it. If the command shell reaches the end of the file or reads an EXIT command, execution stops. Otherwise, the command shell starts this process all over again by displaying the prompt and preparing to read the next line in the script.
Although the default processing mode with command echoing on can be useful for troubleshooting problems in scripts, you probably don’t want to use this display mode with scripts you’ll use regularly. Fortunately, you can change the default behavior by turning command echo off, as I’ll show you later in the chapter in the section titled “Managing Text Display and Command Echoing.”

Using Windows Support Tools

Using Windows Support Tools

The Windows Support Tools are a collection of utilities for handling everything from system diagnostics to network monitoring. These tools can be installed on, and used with, all versions of Windows Server 2003 and Windows XP Professional. You can install the support tools by completing the following steps:
  1. Insert the appropriate operating system CD-ROM (Windows Server 2003 or Windows XP Professional) into the CD-ROM drive.
    Caution 
    Because the Support Tools installation modifies the Help And Support Center, you should close any instances of this console that are running before you start the installation process. If you don’t do this, the installation will fail.
  2. When the Autorun screen appears, click Perform Additional Tasks, and then click Browse This CD. This starts Windows Explorer.
  3. In Windows Explorer, double-click Support and then double-click Tools.
    Note 
    Throughout this book, I refer to double-clicking, which is the most common technique used for accessing folders and for running programs. With a double click, the first click selects the item and the second click opens/runs the item. In both Windows Server 2003 and in Windows XP Professional, you can also configure single-click open/ run. Here, moving the mouse over the item selects it and a single click opens/runs the item. You can change the mouse click options with the Folder Options utility in the Control Panel. To do this, select the General tab, and then choose Single-Click To Open An Item or Double-Click To Open An Item, as desired.
  4. Double-click Suptools.msi. This starts the Windows Support Tools Setup Wizard. Click Next.
  5. Read the End User License Agreement and then, if you agree and want to continue, click I Agree and then click Next.
  6. Enter your user information, and then click Next.
  7. In Windows XP Professional, you then see the Select An Installation Type page. Select Complete and then click Next.
  8. Select the destination directory for the support tools. The default location is %ProgramFiles%\Support Tools. If you don’t want to use the default location, type a new directory path or click Browse to search for a location. In Windows Server 2003, the support tools use about 24 megabytes (MB) of disk space. Selecting the Complete option when installing the Windows XP Professional version of the support tools requires about 12 MB of disk space.
  9. Click Install Now.
  10. Click Finish on the Completing The Windows Support Tools Setup Wizard page.
After installation you can access the support tools through the Help And Support Center(This figure shows the Windows Server 2003 window, though the Windows XP Professional window is very similar.) Click Start, click Programs or All Programs as appropriate, click Windows Support Tools, and then select Support Tools Help. As the figure shows, the tools are organized by file name, tool name, and category. Clicking a tool name accesses a help page that displays the online help documentation for the tool and that you can also use to run the tool.


Use support tools to perform such tasks as system diagnostics and network monitoring.
Most of the support tools have extensions that allow you to run them from the command line. You will find the executables for the support tools in the installation directory, which by default is %SystemDrive%\Program Files\Support Tools.
Because the system path is updated to include the support tools installation directory, you do not need to be in this directory to execute the support tools. You can, in fact, run the tools at any command prompt regardless of the current directory. As with other Windows utilities and commands, you can display the syntax for a particular support tool by typing the command name following by a space and /?, such as spcheck /?.

Overview of the Windows Command Line

Overview


The command line is built into the Microsoft Windows operating system and is accessed through the command shell window. Every version of Windows has had a built-in command line, used to run built-in commands, utilities, and scripts. Although the command line is powerful and versatile, some Windows administrators never use it. If you are happy using the graphical administration tools, you may be able to use them forever without ever having to do anything more than point and click.
For proficient Windows administrators, skilled support staff and committed power users, however, the Windows command line is inescapable. Knowing how to use the command line properly, including which command-line tools to use when and how to put the tools to work effectively, can mean the difference between smooth-running operations and frequent problems. And if you’re responsible for multiple domains or networks, learning the time-saving strategies that the command line offers is not only important, but essential, for sustaining day-to-day operations.
In this chapter, I’ll explain command line essentials, how to use built-in commands, how to run command-line utilities, and how to work with support and resource kit tools.


REAL WORLD

As you read this chapter, and the rest of the book, keep in mind that this book is written for Windows Server 2003 and Windows XP Professional. Techniques that you learn in this book can be used on both operating systems, unless otherwise noted. In some cases, you might be able to use the techniques discussed with other Windows operating systems, though the options or functions may vary. In any case, you should always test commands, options, and scripts before using them. The best way to do this is in a development or test environment where the systems with which you are working are isolated from the rest of the network.