You are previewing Essential PowerShell.
O'Reilly logo
Essential PowerShell

Book Description

Use PowerShell to Streamline and Automate Your Most Time-Consuming Windows Administration Tasks

With Windows PowerShell, Microsoft brings innovative console-based system administration and scripting to Windows clients and servers. PowerShell combines the best features of traditional UNIX shells with the immense power of the .NET Framework, offering a consistent and easy-to-use replacement for both the Windows command line and the Windows Script Host. In Essential PowerShell, noted expert Holger Schwichtenberg gives Windows sysadmins all the knowledge and sample scripts they need to successfully administer production systems with PowerShell.

Schwichtenberg begins by introducing the innovative PowerShell architecture, along with crucial PowerShell concepts such as pipelining and navigation. Next, he introduces the PowerShell Script Language, shows the secure execution of command sequences as scripts, and demonstrates how to use PowerShell to access essential operating system interfaces such as COM, WMI, ADSI, and ADO.NET.

After you’ve mastered the fundamentals, Schwichtenberg presents an extensive collection of PowerShell solutions for virtually every area of day-to-day administration. For each topic, he presents dozens of self-contained examples, all downloadable from a companion Web site. He covers both Microsoft’s standard PowerShell commandlets and the free commandlets available as PowerShell Community Extensions. He also presents a full chapter of tips, tricks, and troubleshooting solutions. Coverage includes

  • Understanding PowerShell’s components and features

  • Setting up your PowerShell scripting environment

  • Creating and using commandlets

  • Using object-oriented pipelining for filtering, sorting, grouping, comparisons, calculations, and more

  • Mastering PowerShell’s uniform navigation model and using it with all types of data

  • Programming command sequences with the PowerShell Script Language

  • Making the most of the PowerShell console and third-party tools

  • Managing processes, event logs, and registry entries

  • Controlling networking, printing, and software installation

  • Manipulating Active Directory and other enterprise services with PowerShell

  • Using PowerShell to enforce stronger system and network security

  • Storing data in files, documents, and databases

  • Table of Contents

    1. Copyright
      1. Dedication
    2. Preface
      1. What Does This Book Cover?
      2. Who Should Read This Book?
      3. How This Book Is Structured
      4. This Book’s Website
    3. Acknowledgments
    4. About the Author
    5. I. Getting Started with PowerShell
      1. 1. First Steps with Windows PowerShell
        1. What Is Windows PowerShell?
          1. A Little Bit of History
          2. Why Use WPS?
          3. Downloading and Installing WPS
          4. Taking WPS for a Test Run
            1. WPS in Interactive Mode
            2. WPS in Script Mode
        2. Downloading and Installing PowerShell Community Extensions
        3. Testing the PowerShell Extensions
        4. Downloading and Installing the PowerShellPlus
        5. Testing the PowerShell Editor
        6. Summary
      2. 2. Commandlets
        1. Introducing Commandlets
          1. Parameters
          2. Calculated Parameters
          3. More Examples
          4. Placeholders
          5. Other Aspects of Commandlets
        2. Aliases
          1. Enumerating Aliases
          2. Create a New Alias
        3. Expressions
        4. External Commands
          1. Filenames
        5. Getting Help
          1. Getting a list of Available Commands
          2. Getting Commandlet Help
          3. Documentation of .NET Classes
        6. Summary
      3. 3. Pipelining
        1. Pipelining Basics
          1. Object Orientation
          2. Object Types and Data Members
            1. Executing Methods
            2. Pipelining of Parameters
            3. Pipelining of Classic Command
        2. Pipeline Processor
        3. Complex Pipelines
        4. Output
          1. Standard Output
            1. Pagewise Output
            2. Restricting the Output
          2. Output of Single Values
            1. Suppressing the Output
          3. Other Output Functions
        5. Getting User Input
          1. Input Dialog
          2. Dialog Boxes
          3. Authentication Dialog Box
        6. Summary
      4. 4. Advanced Pipelining
        1. Analyzing Pipeline Content
          1. Get-PipelineInfo
            1. Get-Member
            2. More Information about Get-Member
          2. Extended Type System (ETS)
        2. Filtering Objects
          1. Aggregation of Pipeline Content
        3. Castrating Objects
        4. Sorting Objects
        5. Grouping Objects
        6. Calculations
        7. Intermediate Steps in the Pipeline
        8. Comparing Objects
        9. Ramifications
        10. Summary
      5. 5. The PowerShell Navigation Model
        1. Navigation through the Registry
        2. Providers and Drives
        3. Navigation Commandlets
        4. Paths
        5. Defining Drives
        6. Summary
      6. 6. The PowerShell Script Language
        1. Getting Help
        2. Command Separation
        3. Comments
        4. Variables
          1. Set the Type
        5. Available Types
          1. Getting the Type
          2. Predefined Variables
          3. Example
          4. Constant Values
          5. Variable Resolution
        6. Numbers
        7. Random Numbers
        8. Strings
          1. Working with Strings
          2. Splitting and Joining Strings
        9. Date and Time
          1. Remote Computers
          2. Changing the Date and Time
        10. Arrays
        11. Associative Arrays (Hash Tables)
        12. Operators
        13. Control Structures
          1. Loops
          2. Conditions
          3. Subroutines
        14. Summary
      7. 7. PowerShell Scripts
        1. A First PowerShell Script Example
        2. Start a PowerShell Script
        3. Including Scripts
        4. Scripting Security
          1. Security Policy
        5. Signing of Scripts
        6. Letting a Script Sleep
        7. Errors and Error Treatment
          1. Example
          2. Individual Reactions to Errors
          3. Further Options
        8. Summary
      8. 8. Using Class Libraries
        1. Using .NET Classes
          1. Creating Instances
          2. Constructors with Parameters
          3. Static Members in .NET Objects/Static .NET Classes
          4. Loading Additional Assemblies
          5. Object Analysis
          6. Enumerations
        2. Using COM Classes
          1. Create an Instance
          2. Get an Existing Instance
          3. Using COM Objects
        3. Using WMI Classes
          1. System.Management
          2. WMI Support in WPS
          3. Accessing WMI Objects
          4. Type Indicators
          5. The WMI Object Adapter
          6. Analyzing WMI Objects
          7. Accessing WMI Members
            1. Static Class Members
        4. Date and Time
          1. Accessing WMI Collections
          2. Filtering and Selecting
          3. Filtering with Get-WmiObject
          4. WQL Queries
          5. List of All WMI Classes
          6. Creating New Instances of WMI Classes
        5. Summary
      9. 9. PowerShell Tools
        1. PowerShell Console
          1. Console Functions
          2. Tab Completion
          3. Command Mode Versus Interpreter Mode
          4. User Account Control in Windows Vista
        2. PowerTab
        3. PowerShell IDE
        4. Windows PowerShellPlus
        5. PowerShell Analyzer
        6. PrimalScript
        7. PowerShell Help
        8. Summary
      10. 10. Tips, Tricks, and Troubleshooting
        1. Debugging and Tracing
          1. Verbose Execution
          2. Single-Step Mode
          3. Measuring Execution Time
          4. Tracing
        2. Commandlet Extensions
          1. Adding Snap-Ins
          2. DLL Registration
            1. Loading of Snap-Ins to the PowerShell Console
          3. Listing Snap-Ins
          4. List of Available Commandlets
          5. Ambiguous Commandlets
          6. Available Commandlet Extensions
          7. PowerShell Community Extensions
          8. www.IT-Visions.de PowerShell Extensions
          9. Quest Management Shell for Active Directory
          10. Microsoft Exchange Server 2007
          11. System Center Virtual Machine Manager 2007
        3. Command History
        4. System and Host Information
          1. Example
        5. PowerShell Profiles
        6. Graphical User Interfaces
          1. Input Dialog
          2. Displaying Objects
          3. Windows Clipboard
        7. Summary
    6. II. Windows PowerShell in Action
      1. 11. File Systems
        1. Available Commandlets for File System Administration
        2. Drives
          1. Free Space
          2. Drive Labels
          3. Network Drives
        3. Directory Content
          1. File System Operations
        4. Reading and Writing File Properties
          1. Times
        5. Properties of Executables
        6. File System Links
          1. Explorer Links
          2. Hardlinks
          3. Junction Points
          4. Symbolic Links in Windows Vista
        7. Compression
        8. File Shares
          1. Enumerating File Shares
          2. Creating File Shares
          3. Setting Permissions on File Shares
          4. Mass Creation of Shares
        9. Summary
      2. 12. Managing Documents
        1. Text Files
          1. Writing Files
          2. Searching
        2. Binary Files
        3. CSV Files
          1. CSV Export
          2. CSV Import
        4. XML Files
          1. Reading XML Documents
          2. Checking XML Documents
          3. Formatting
          4. XPath
          5. Modifying XML Documents
          6. Exporting Pipeline Objects to XML
          7. Transforming XML Documents
        5. HTML Files
        6. Summary
      3. 13. Registry and Software
        1. Registry
          1. Reading Keys
          2. Creating and Deleting Keys
          3. Defining Drives
          4. Reading Values
          5. Creating and Deleting Values
          6. Example
        2. Software Administration
          1. Software Inventory
            1. Additional Information about Software
            2. Non-MSI Applications
            3. Autostart Applications
          2. Installing Software
          3. Uninstalling Software
          4. Testing Installations
        3. Summary
      4. 14. Processes and Services
        1. Processes
          1. Enumerating Processes
          2. Filtering
          3. Starting Processes
          4. Ending Processes
          5. Waiting for Process Ending
        2. Windows Services
          1. Enumerating Services
          2. Dependent Services
          3. Starting and Stopping Services
          4. Changing Service Attributes
          5. Installation of New Windows Services
          6. Change Service Configuration
        3. Summary
      5. 15. Computers and Hardware
        1. Computer Settings
        2. Hardware
          1. Printers and Print Jobs
          2. Printer Connections
          3. Print Jobs
        3. Event Logs
          1. Event Log Names
          2. Event Log Entries
        4. Performance Counters
        5. Summary
      6. 16. Networking
        1. Pinging Computers
        2. Network Configuration
        3. Name Resolution
        4. Retrieving Files from an HTTP Server
        5. E-Mail
        6. Microsoft Exchange Server 2007
          1. Basic Operations
          2. Reading Information
          3. Managing Mailboxes
          4. Managing Public Folders
        7. Internet Information Services
          1. List of All Virtual Web Servers
          2. Add New Virtual Web Servers
          3. Delete Virtual Web Servers
        8. Summary
      7. 17. Directory Services
        1. Overview of Directory Services Access
        2. Managing Users and Groups Using WMI
        3. System.DirectoryServices and the ADSI Adapter
          1. Architecture
          2. Integration with ADSI
          3. Object Model
          4. Class DirectoryEntry
          5. Class DirectoryEntries
          6. Class for the Execution of Search Queries
          7. Comparison of System.Directoryservices and ADSI
        4. Deficiencies in the ADSI Adapter
        5. Object Identification in Directory Services (Directory Services Paths)
          1. Object Identification in the Active Directory
        6. Overview of the Common Programming Tasks
          1. Binding to Directory Entries
          2. Impersonation
          3. Checking the Existence of Directory Entries
          4. Reading Directory Entry Attributes
          5. ADSI Property Cache
          6. Writing Directory Entry Attributes
          7. Common Properties
          8. Accessing Container Objects
          9. Creating Directory Entries
          10. Deleting Directory Entries
        7. Summary
      8. 18. User and Group Management in the Active Directory
        1. Directory Class User
        2. Creating a User Account
          1. Example
          2. Setting the Password
        3. Authentication
        4. Deleting Users
        5. Renaming User Accounts
          1. Example
        6. Moving User Accounts
          1. Example for Moving a User Account
        7. Group Management
          1. Creating and Filling a Group
          2. Assigning Group Members
        8. Organizational Units
        9. Summary
      9. 19. Searching in the Active Directory
        1. LDAP Query Syntax
          1. Example for an LDAP Query
        2. LDAP Queries in PowerShell
          1. Example for an LDAP Query in PowerShell
          2. Searching a User with Its Login Name
        3. Search Tips and Tricks
          1. Use Indexed Attributes
          2. Avoid Multivalued Attributes
          3. Avoid the Star Operator
          4. Search Results Restrictions
        4. LDAP Query Examples
        5. Using the Commandlet Get-ADObject
        6. Summary
      10. 20. Additional Libraries for Active Directory Administration
        1. Navigating the Active Directory Using the PowerShell Community Extensions
        2. Using the www.IT-Visions.de Active Directory Extensions
          1. Example
        3. Using the Quest Active Directory Extensions
        4. Getting Information about the Active Directory Structure
          1. Example 1: Domains and Forests
          2. Example 2: Domain Controllers and Roles
        5. Group Policies
          1. Classes
          2. Enumerating Policies
          3. Create a New Group Policy Link
          4. Delete a Group Policy Link
        6. Summary
      11. 21. Databases
        1. Introducing ADO.NET
          1. Data Providers
          2. Enumerating the Installed Providers
          3. List of Available SQL Servers
          4. DataReader versus DataSet
        2. Example Database
        3. Data Access with PowerShell
          1. Connections
          2. Examples
          3. Provider-Independent Data Access
          4. Executing Commands
          5. Example
          6. Data Access Using a Data Reader
          7. Example for Using a Data Reader
        4. Summary
      12. 22. Advanced Database Operations
        1. Data Access Using a DataSet
          1. DataSet Object Model
          2. Data Adapter
          3. Provider-Specific Example
          4. Provider-Independent Example
          5. XML Export and Import
        2. Data Access with the www.IT-Visions.de PowerShell Extensions
          1. Example
        3. Summary
      13. 23. Security Settings
        1. Windows Security Basics
          1. Accounts
          2. Security Descriptors
          3. Access Control Lists
          4. Access Masks
        2. Classes
          1. Members of the Class Object Security
          2. Resource Classes
          3. User Accounts and SIDs
        3. Reading ACLs
        4. Reading ACEs
        5. Summary
      14. 24. Advanced Security Administration
        1. Account Identifier Translation
          1. Converting between Username and Security Identifier
          2. Using Well-Known SIDs
          3. SDDL Names
        2. Reading the Owner
        3. Adding a New ACE to an ACL
        4. Removing an ACE from an ACL
          1. Example 1
          2. Example 2
        5. Transferring ACLs
        6. Setting ACLs Using SDDL
          1. Example
        7. Summary
    7. III. Appendices
      1. A. PowerShell Commandlet Reference
      2. B. PowerShell 2.0 Preview
      3. C. Bibliography