You are previewing How to Cheat at Windows System Administration Using Command Line Scripts.
O'Reilly logo
How to Cheat at Windows System Administration Using Command Line Scripts

Book Description

If you want to streamline the administration of your Windows servers and workstations by using command line scripts, this is the book for you! With every new version of Windows, Microsoft attempts to ease administrative tasks by adding more and more layers of graphical user interface (GUI) or configuration wizards (dialog boxes). While these "wizards" are interactive and make the administrator's job simpler, they are nowhere near as quick or efficient as a well-crafted command-line script or batch file. This next title in Syngress' best-selling "How to Cheat" series teaches system administrators hundreds of powerful, time-saving tips for automating daily system administration tasks using Windows command line scripts. The book is divided into five sections.

The first section covers the basics of command line scripts and batch files including, usage of filters, conditional processing and command redirection. The readers will learn to create and schedule tasks to automating administration jobs from the Task Scheduler and from the command prompt.

Part two discusses basic Windows administration including disk check, disk defragmentation, converting basic disks to dynamic, and file system commands as well as administering partitions and volumes.

Part three focuses on scripting Active Directory including administering user and group accounts, and administering computers and domains. Part four moves on to Windows networking where the reader will learn to troubleshooting command-line tools, manage network printers, manage services for DHCP, DNS, WINS services as well as manage and monitor security including Microsoft Baseline Security Analyzer and certificate services.

In the final part of the book, the reader will learn to manage Windows systems including starting and stopping services, configuring service startup types, managing event log files, setting log filters, configuring system monitor, starting a performance log, setting objects, and configuring performance alerts.

Table of Contents

  1. Copyright
  2. Lead Author
  3. Technical Editor and Reviewer
  4. Contributing Authors
  5. Companion Web Site
  6. Introduction
  7. I. Getting Started with Command Line
    1. 1. Basics of the Command Line
      1. Introduction
      2. Basics of the Windows Command Shell
        1. The MS-DOS Command Shell
      3. Starting the Windows Command Shell
        1. Customizing the Command Shell Startup
        2. Customizing the Command Shell Window
          1. The Options Tab
          2. The Font Tab
          3. The Layout Tab
          4. The Colors Tab
      4. Internal Commands for the Command Shell
        1. Getting Help for Commands
      5. Command History
        1. Displaying Previous Commands with Arrow Keys
        2. Viewing the Command History in a Pop-Up Window
        3. Using Function Keys
      6. Accessing the Windows Command Reference
      7. Installing Windows Support Tools
      8. Summary
    2. 2. Using Batch Files
      1. Introduction
      2. Working Safely with the Command Line
      3. Configuring the Command Path
        1. Using the Path Command
        2. Using the Set and Setx Commands
          1. Using the Set Command
          2. Using the Setx Command
        3. Changing Environment Variables in System Properties
      4. Using Command Redirection
        1. Command Redirection Operators
        2. Input Redirection
        3. Output Redirection
        4. Redirecting Output to Other Commands
        5. Error Handling with Redirection Operators
      5. Using Groups of Commands
        1. Using & for Sequential Processing
        2. Using && and | | for Conditional Processing
        3. Grouping Sets of Commands with Parentheses
      6. Creating Batch Files
        1. Batch File Commands
          1. Call
          2. Choice
          3. Echo
          4. Endlocal
          5. For
            1. Group of Files
            2. Directories
            3. Range of Values
            4. Parsing Text Strings
          6. Goto
          7. If
          8. Pause
            1. Terminating a Batch Job
          9. Rem
          10. Setlocal
          11. Shift
        2. Batch File Parameters
          1. Using Variable Modifiers
            1. Using a Combination of Modifiers
      7. Summary
    3. 3. Managing Scheduled Tasks
      1. Introduction
      2. Scheduling Tasks
        1. The Task Scheduler
          1. schtasks
      3. The Task Scheduler Service
        1. Accessing the Task Scheduler Service
          1. Creating a Custom MMC for Services
        2. Configuring the Properties of the Task Scheduler Service
          1. The General Tab
          2. The Log On Tab
          3. The Recovery Tab
          4. The Dependencies Tab
      4. Managing Tasks Using the Task Scheduler
        1. Managing the Properties of Scheduled Tasks
          1. The General Tab
          2. The Schedule Tab
          3. The Settings Tab
          4. The Security Tab
        2. Monitoring Tasks in the Scheduled Tasks Window
          1. Viewing the Scheduled Task Logs
        3. Creating New Tasks
        4. Deleting a Scheduled Task
        5. Running a Scheduled Task Immediately
        6. Enabling or Disabling a Scheduled Task
        7. Ending a Running Task
        8. Event-Based Tasks
      5. The schtasks Command-Line Utility
        1. Creating and Running Tasks Using schtasks/Create
          1. /sc
          2. \tn
          3. /tr
          4. /s Computer
          5. /u [Domain\]User /p Password
          6. /ru {[Domain\]User | System}
          7. /rp Password
          8. /mo Modifier
          9. /m Month
          10. /i IdleTime
          11. /st StartTime
          12. /ri Interval
          13. /et EndTime | /du Duration
          14. /sd StartDate
          15. /ed EndDate
          16. Examples of Using schtasks /Create Effectively
            1. Scheduling a Task That Runs Only Once
            2. Scheduling a Task That Runs When a User Logs On
            3. Scheduling a Task That Runs When the System Starts
            4. Scheduling a Task That Runs When the System Is Idle for 15 Minutes
            5. Scheduling a Task That Runs Every 10 Minutes
            6. Scheduling a Task That Runs Every Four Hours
            7. Scheduling a Task That Runs Every Day
            8. Scheduling a Task That Runs Every 10 Days
            9. Scheduling a Task That Runs Every Week on Wednesday
            10. Scheduling a Task That Runs Every Alternate Week on Monday and Friday
            11. Scheduling a Task That Runs on the First Day of Every Month
            12. Scheduling a Task That Runs on the Fifteenth of Every Month
            13. Scheduling a Task That Runs on the Last Day of Every Month
            14. Scheduling a Task That Runs on the Second and Fourth Friday of Every Month
            15. Scheduling a Task That Runs on the Second Friday in March, June, September, and December
            16. Scheduling a Task That Runs on a Remote Computer with a User Account
          17. Different Forms of the /d Parameter
      6. Managing Tasks with schtasks
        1. Using schtasks /Query to Query a Scheduled Task
          1. /fo {TABLE | LIST | CSV}
          2. /nh
          3. /V
          4. Displaying the schtasks /Query Results Onscreen
          5. Using Log Files for schtasks Queries
        2. Modifying a Scheduled Task with schtasks /Change
        3. Running a Scheduled Task with schtasks/Run
        4. Ending Running Tasks with schtasks /End
        5. Deleting Scheduled Tasks with schtasks/Delete
      7. Summary
  8. II. Basic Windows Administration
    1. 4. Managing Files and Directories
      1. Introduction
      2. Using Wildcards in Commands
      3. File and Folder Attributes
        1. Viewing Attributes
        2. Changing Attributes
      4. Basic File and Folder Operations
        1. Copying Files with the Copy Command
          1. Copying a Single File
          2. Choosing the ASCII (/a) or Binary (/b) Format
          3. Appending Multiple Files to a Single File
          4. Copying Files to and from Devices
            1. CON
            2. PRN
            3. LPT
            4. COM
            5. NUL
        2. Copying Files and Directories with the Xcopy Command
          1. Specifying Source and Destination
          2. Excluding Files from the Copy Process
          3. Examples of Using the Xcopy Command
          4. Xcopy Exit Codes
            1. Example Batch File with Xcopy Exit Codes
        3. Renaming Files with the Rename (Ren) Command
        4. Moving Files Using the Move Command
        5. Deleting Files with the Del (Erase) Command
        6. Comparing Files with the Comp Command
        7. Comparing Files with the FC Command
          1. Performing ASCII File Comparisons
          2. Performing Binary File Comparisons
        8. Sorting Files with the Sort Command
          1. Using the Sort Command to Sort File Input
          2. Using the Sort Command to Sort Command Output
        9. Recovering Files with the Recover Command
        10. Decompressing Files with the Expand Command
      5. Duplicating and Comparing Disks
        1. Duplicating Disks with the Diskcopy Command
          1. Using Two Drives for Source and Destination
          2. Using a Single Drive as Source and Destination
          3. Using Unformatted Disks as Destinations
          4. Diskcopy Exit Codes
        2. Comparing Two Disks with the Diskcomp Command
          1. Exit Codes for the Diskcomp Command
      6. Directory-Specific Commands
        1. Displaying the Directory Structure (Tree)
        2. Creating a New Directory with MD or Mkdir
        3. Removing a Directory with RD or Rmdir
          1. Deleting Subdirectories of the Root Directory
        4. Removing a Directory Tree with the Deltree Command
      7. Summary
    2. 5. Maintaining Hard Disks
      1. Introduction
      2. Physical and Logical Disks
        1. Physical Disks
        2. Logical Disks
      3. Understanding Basic and Dynamic Disks
        1. Basic Disks
          1. Partitions and Logical Drives
          2. Tasks Associated with Basic Disks
        2. Dynamic Disks
          1. Tasks Associated with Dynamic Disks
          2. Volume Types on Dynamic Disks
        3. Tasks Common to Basic and Dynamic Disks
      4. Supported File Systems
        1. Formatting a Disk or Partition with the Format Command
          1. Allocation Unit Size
          2. Examples of the Format Command
          3. Exit Codes for the Format Command
      5. Converting File Systems
        1. Converting a File System with the Convert Command
          1. Specifying a File with the /cvtarea Parameter
        2. Examining Volume Serial Numbers with the Vol Command
        3. Managing Volume Labels with the Label Command
          1. Changing the Existing Label
          2. Viewing and Deleting the Existing Label
        4. Maintaining Disks and File Systems
        5. Using the Fsutil Utility for Advanced Disk Management
          1. Behavior
          2. Dirty
          3. File
          4. Fsinfo
          5. Hardlink
          6. ObjectID
          7. Quota
          8. Reparsepoint
          9. Sparse
          10. USN
          11. Volume
          12. Examples of Using the Fsutil Command Set
            1. Getting File Information
            2. Getting Detailed Information on a Volume
            3. Getting Detailed NTFS-Specific Information
            4. Finding Files Owned by a User
            5. Specifying a Short Name for a File
            6. Getting Information on Free Disk Space
        6. Checking Available Disk Space with the Freedisk Command
        7. Saving Disk Space with the Compact Command
        8. Managing Mounted Volumes with the Mountvol Command
        9. Checking and Fixing Bad Sectors with the Chkdsk Command
          1. Checking the Disk without Fixing Errors
          2. Checking Drives and Fixing Errors
          3. Running Chkdsk on Mounted Volumes
          4. Exit Codes for the Chkdsk Command
        10. Defragmenting Disks with the Defrag Command
          1. Defragmenting Dirty Volumes
        11. Checking Autocheck Status with the Chkntfs Command
      6. Summary
    3. 6. Managing Hard Disks with the Diskpart Utility
      1. Introduction
      2. The Diskpart Utility
        1. The Object in Focus
      3. Diskpart Commands
        1. Active
        2. Add Disk
        3. Assign
        4. Automount
        5. Break Disk
        6. Clean
        7. Convert
          1. Convert Basic
          2. Convert Dynamic
          3. Convert GPT
          4. Convert MBR
        8. Create
          1. Create Partition Primary
          2. Create Partition Extended
          3. Create Partition Logical
          4. Create Partition EFI
          5. Create Partition MSR
          6. Create Volume Simple
          7. Create Volume Stripe
          8. Create Volume RAID
        9. Delete
          1. Delete Disk
          2. Delete Partition
          3. Delete Volume
        10. Detail
          1. Detail Disk
          2. Detail Partition
          3. Detail Volume
        11. Exit
        12. Extend
        13. GPT Attributes
        14. Help
        15. Import
        16. Inactive
        17. List
          1. List Disk
          2. List Partition
          3. List Volume
        18. Online
        19. Rem
        20. Remove
        21. Repair Disk
        22. Rescan
        23. Retain
        24. Select
          1. Select Disk
          2. Select Partition
          3. Select Volume
      4. Scripting with Diskpart
        1. Diskpart Error Codes
          1. Using the Noerr Parameter
      5. Obtaining Volume Information
        1. Understanding Volume Status
      6. Managing Dynamic Volumes
        1. Simple Volumes
          1. Creating a Simple Volume
          2. Expanding a Simple Volume
        2. Striped Volumes
      7. Managing Fault-Tolerant Volumes
        1. Mirrored Volumes
          1. Resolving Mirrored Volume Problems
        2. RAID 5 Volumes
          1. Repairing a Failed RAID 5 Volume
      8. Summary
  9. III. Managing Windows Systems and Printers
    1. 7. System Services, Drivers, and the Registry
      1. Introduction
      2. Obtaining System Information
        1. Determining the Operating System Version
        2. Locating Files with the Where Command
        3. Checking the System Date and Time
          1. Changing the System Date with the Date Command
          2. Changing the System Clock with the Time Command
        4. Obtaining Information on the Logged-On User
        5. Obtaining System Configuration Information
        6. Checking Protected System Files with the SFC Command
      3. Shutting Down and Restarting the System
        1. Specifying Reasons for the Event Tracker
        2. Using the Shutdown Command on the Local Computer
        3. Using the Shutdown Command on the Remote Computer
        4. Differences between the Windows XP and Windows Server 2003 Shutdown Commands
      4. Managing System Services
        1. Obtaining Information about Services
        2. Starting, Stopping, Pausing, and Resuming Services
        3. Configuring a Service’s Startup Type
        4. Managing Service Failures
        5. Configuring the Service Logon Type
        6. SC Subcommands That Affect All Services
          1. SC Boot
          2. SC Lock
          3. SC QueryLock
      5. Obtaining Driver Information
      6. Managing the Windows Registry
        1. Data Types Supported in the Windows Registry
        2. Examining Values Stored in a Subkey
        3. Comparing Subkeys
        4. Adding and Deleting Subkeys
        5. Saving and Restoring Registry Keys
        6. Copying Registry Keys
      7. Summary
    2. 8. Monitoring System Events, Processes, and Performance
      1. Introduction
      2. Managing Event Logs from the Command Line
      3. Creating New Events
      4. Working with Event Triggers
        1. Creating Event Triggers
        2. Deleting Event Triggers
        3. Querying Event Triggers
      5. Viewing Logged Events
        1. Using Filters to View Specific Events
          1. Filter Names and Filter Operators
          2. Range of Events
          3. Examples of the Eventquery Command
      6. Monitoring Application Processes and Tasks
        1. Viewing Running Processes and Applications
          1. Viewing Service Information
          2. Viewing Process Modules
        2. Terminating Applications and Processes
      7. Using Filters with the TaskList and TaskKill Commands
      8. Working with System Performance
        1. Viewing Performance Data
          1. Specifying the Counter Path
            1. Using Wildcards with Counter Paths
        2. Adding New Performance Counters
        3. Removing Performance Counters
        4. Extracting Performance Counters from Existing Logs
      9. Summary
    3. 9. Managing Printing Services
      1. Introduction
      2. Working with Printer Commands
        1. Working with Remote Computers
      3. Installing a Local Printer
        1. Listing All Printers Installed on a Computer
        2. Adding a Local Printer
        3. Deleting an Installed Printer
        4. Displaying All Printers Configured on a Computer
        5. Displaying the Default Printer
        6. Setting the Default Printer
      4. Configuring and Renaming Printers
        1. Displaying the Printer Configuration
        2. Configuring Printer Properties
          1. Configuring Basic Printer Properties
          2. Sharing a Printer
          3. Configuring Advanced Printer Properties
          4. Publishing a Printer in Active Directory
        3. Renaming a Printer
      5. Managing Printer Drivers
        1. Displaying Driver Information for All Printers on a Computer
        2. Installing a Printer Driver
        3. Deleting a Printer Driver
        4. Deleting All Printer Drivers from a Computer
      6. Creating and Configuring TCP/IP Printer Ports
        1. Viewing TCP/IP Printing Ports Configured on a Computer
          1. Viewing the Configuration of a TCP/IP Printing Port
        2. Creating and Configuring a Standard TCP/IP Printing Port
        3. Deleting a Standard TCP/IP Printing Port
      7. Managing Print Queues and Print Jobs
        1. Printing a Test Page
        2. Pausing and Resuming a Printer
        3. Canceling All Print Jobs in the Print Spooler
        4. Listing All the Print Jobs in a Print Queue
        5. Pausing, Resuming, and Canceling a Print Job
      8. Summary
  10. IV. Working with Active Directory
    1. 10. Overview of Directory Services Commands
      1. Introduction
      2. Getting Started with DS Commands
      3. Types of Objects for DS Commands
        1. Working on Remote Computers
      4. Querying the Directory Database with DSQuery
        1. Parameters Common to All DSQuery Commands
        2. DSQuery Computer
        3. DSQuery Contact
        4. DSQuery Group
        5. DSQuery OU
        6. DSQuery Site
        7. DSQuery Server
        8. DSQuery User
        9. DSQuery Quota
        10. DSQuery Partition
        11. DSQuery *
      5. Adding New Objects with DSAdd
        1. Parameters Common to All DSAdd Commands
        2. DSAdd Computer
        3. DSAdd Contact
        4. DSAdd Group
        5. DSAdd OU
        6. DSAdd User
        7. DSAdd Quota
      6. Displaying Object Properties with DSGet
        1. Parameters Common to All DSGet Commands
        2. DSGet Computer
        3. DSGet Contact
        4. DSGet Group
        5. DSGet OU
        6. DSGet Server
        7. DSGet User
        8. DSGet Subnet
        9. DSGet Site
        10. DSGet Quota
        11. DSGet Partition
      7. Modifying Objects with DSMod
        1. DSMod Computer
        2. DSMod Contact
        3. DSMod Group
        4. DSMod OU
        5. DSMod Server
        6. DSMod User
        7. DSMod Quota
        8. DSMod Partition
      8. Moving and Renaming Objects with DSMove
      9. Deleting Directory Objects with DSRm
      10. Summary
    2. 11. Managing Active Directory Users, Groups, and Computers
      1. Introduction
      2. Managing User Accounts
        1. Searching for Users in Active Directory
        2. Searching for Disabled User Accounts
        3. Determining Group Memberships of Users
        4. Creating New User Accounts
        5. Setting and Modifying User Account Properties
        6. Moving and Renaming User Accounts
        7. Resetting User Passwords
        8. Enabling and Disabling User Accounts
        9. Deleting User Accounts
      3. Managing Group Accounts
        1. Searching for Group Accounts in Active Directory
        2. Creating New Group Accounts
        3. Managing Membership of Groups
        4. Modifying Group Account Properties
        5. Moving and Renaming Group Accounts
        6. Deleting Group Accounts
      4. Managing Computer Accounts
        1. Searching for Computer Accounts in Active Directory
        2. Creating New Computer Accounts
        3. Managing Properties of Computer Accounts
        4. Resetting Computer Accounts
        5. Moving and Renaming Computer Accounts
        6. Enabling and Disabling Computer Accounts
        7. Deleting Computer Accounts
      5. Managing Domain Controller Accounts
        1. Searching for Domain Controllers in Active Directory
        2. Searching for Domain Controllers with an Operations Master Roles
        3. Searching for GC Servers
        4. Managing Roles of GC Servers
      6. Summary
  11. V. Windows Networking
    1. 12. Basic TCP/IP Networking Commands
      1. Introduction
      2. Overview of the Net Commands
      3. Starting and Stopping TCP/IP Services
      4. Troubleshooting Commands for TCP/IP
        1. Arp
        2. IPConfig
        3. Finger
        4. Getmac
        5. Hostname
        6. Netstat
        7. NBTStat
        8. NSLookup
        9. Pathping
        10. Ping
      5. Commands for Remote Computers
        1. FTP
        2. TFTP
        3. RCP
        4. RSH and REXEC
        5. LPR
        6. LPQ
      6. Summary
    2. 13. Administering Network Services
      1. Introduction
      2. Overview of the NETSH Commands
        1. Commands Available within the NETSH Prompt
        2. List of Subcommands Available within the NETSH Prompt
          1. NETSH Available Information
      3. NETSH Commands for Troubleshooting
        1. Using Ping to Verify Connectivity
      4. Managing Interfaces with NETSH
        1. Managing IP Addressing
          1. NETSH Interface IP Show Commands
          2. NETSH Interface IP Set Commands
            1. NETSH Interface IP Set Address
            2. NETSH Interface IP Add Address
            3. NETSH Interface IP Delete Address
        2. Managing the DNS Settings of an Interface
          1. NETSH Interface IP Show DNS
          2. NETSH Interface IP Set DNS
          3. NETSH Interface IP Add DNS
          4. NETSH Interface IP Delete DNS
        3. Managing Interface IP WINS Settings
          1. NETSH Interface IP Show WINS
          2. NETSH Interface IP Set WINS
          3. NETSH Interface IP Add WINS
          4. NETSH Interface IP Delete WINS
      5. Managing Automatic Addressing Using DHCP Services
        1. NETSH DHCP
          1. Server
          2. Show Server
          3. Add Server
          4. Delete Server
          5. Dump
        2. NETSH DHCP SERVER
          1. Viewing Configuration Settings
          2. Configuring the DHCP Server
        3. NETSH DHCP SERVER SCOPE
          1. Viewing the Scope Configuration
          2. Configuring a Scope
      6. NETSH Commands for AAAA
        1. Show Version
        2. Show Config
        3. Set Config
        4. Dump
      7. Summary
  12. A. MS-DOS Commands Not Supported in Windows XP and Windows 2003
    1. Introduction
    2. MS-DOS Commands Not Supported in Windows XP/2003 32-Bit Operating Systems
      1. assign
        1. Syntax
        2. Example
      2. backup
        1. Syntax
        2. Example
      3. choice
        1. Syntax
        2. Example
      4. ctty
        1. Syntax
        2. Example
      5. deltree
        1. Syntax
        2. Example
      6. emm386
        1. Syntax
        2. Example
      7. fdisk
        1. Syntax
        2. Example
      8. mscdex
        1. Syntax
        2. Example
      9. scandisk
        1. Syntax
        2. Example
      10. Smartdrv
        1. Syntax
        2. Example
      11. sys
        1. Syntax
        2. Example
    3. MS-DOS Commands Not Supported in Windows XP/2003 64-Bit Operating Systems
      1. debug
        1. Syntax
        2. Example
      2. edit
        1. Syntax
        2. Example
      3. edlin
        1. Syntax
        2. Example
      4. exe2bin
        1. Syntax
        2. Example
      5. expand
        1. Syntax
        2. Example
      6. fasthelp
        1. Syntax
        2. Example
      7. fastopen
        1. Syntax
        2. Example
      8. forcedos
        1. Syntax
        2. Example
      9. graphics
        1. Syntax
        2. Example
      10. loadfix
        1. Syntax
        2. Example
      11. loadhigh
        1. Syntax
        2. Example
      12. mem
        1. Syntax
        2. Example
      13. nlsfunc
        1. Syntax
        2. Example
      14. setver
        1. Syntax
        2. Example
      15. share
        1. Syntax
        2. Example