Windows PowerShell Cookbook
for Windows, Exchange 2007, and MOM V3
By
Lee Holmes
October 2007
Pages: 584
| Table of Contents
| Index
| Sample Chapter
| Colophon
Table of Contents
-
Tour
-
A Guided Tour of Windows PowerShell
- Introduction
- An Interactive Shell
- Structured Commands (Cmdlets)
- Deep Integration of Objects
- Administrators As First-Class Users
- Composable Commands
- Techniques to Protect You from Yourself
- Common Discovery Commands
- Ubiquitous Scripting
- Ad Hoc Development
- Bridging Technologies
- Namespace Navigation Through Providers
- Much, Much More
-
Fundamentals
-
Chapter 1 The Windows PowerShell Interactive Shell
- Introduction
- Run Programs, Scripts, and Existing Tools
- Run a PowerShell Command
- Customize Your Shell, Profile, and Prompt
- Find a Command to Accomplish a Task
- Get Help on a Command
- Program: Search Help for Text
- Invoke a PowerShell Script From Outside PowerShell
- Program: Retain Changes to Environment Variables Set by a Batch File
- Get the System Date and Time
- Determine the Status of the Last Command
- Measure the Duration of a Command
- Customize the Shell to Improve Your Productivity
- Program: Learn Aliases for Common Commands
- Access and Manage Your Console History
- Store the Output of a Command into a File
- Add Information to the End of a File
- Record a Transcript of Your Shell Session
- Display the Properties of an Item As a List
- Display the Properties of an Item As a Table
- Manage the Error Output of Commands
- Configure Debug, Verbose, and Progress Output
- Extend Your Shell with Additional Snapins
- Use Console Files to Load and Save Sets of Snapins
-
Chapter 2 Pipelines
- Introduction
- Filter Items in a List or Command Output
- Program: Simplify Most Where-Object Filters
- Program: Interactively Filter Lists of Objects
- Work with Each Item in a List or Command Output
- Automate Data-Intensive Tasks
-
Chapter 3 Variables and Objects
- Introduction
- Store Information in Variables
- Access Environment Variables
- Control Access and Scope of Variables and Other Items
- Work with .NET Objects
- Create an Instance of a .NET Object
- Program: Create Instances of Generic Objects
- Reduce Typing for Long Class Names
- Use a COM Object
- Learn About Types and Objects
- Get Detailed Documentation About Types and Objects
- Add Custom Methods and Properties to Objects
- Add Custom Methods and Properties to Types
-
Chapter 4 Looping and Flow Control
- Introduction
- Make Decisions with Comparison and Logical Operators
- Adjust Script Flow Using Conditional Statements
- Manage Large Conditional Statements with Switches
- Repeat Operations with Loops
- Add a Pause or Delay
-
Chapter 5 Strings and Unstructured Text
- Introduction
- Create a String
- Create a Multiline or Formatted String
- Place Special Characters in a String
- Insert Dynamic Information in a String
- Prevent a String from Including Dynamic Information
- Place Formatted Information in a String
- Search a String for Text or a Pattern
- Replace Text in a String
- Convert a String to Upper/Lowercase
- Trim a String
- Format a Date for Output
- Program: Convert Text Streams to Objects
- Generate Large Reports and Text Streams
-
Chapter 6 Calculations and Math
- Introduction
- Perform Simple Arithmetic
- Perform Complex Arithmetic
- Measure Statistical Properties of a List
- Work with Numbers As Binary
- Simplify Math with Administrative Constants
- Convert Numbers Between Bases
-
Common Tasks
-
Chapter 7 Simple Files
- Introduction
- Get the Content of a File
- Search a File for Text or a Pattern
- Parse and Manage Text-Based Logfiles
- Parse and Manage Binary Files
- Create a Temporary File
- Search and Replace Text in a File
-
Chapter 8 Structured Files
- Introduction
- Access Information in an XML File
- Perform an XPath Query Against an XML File
- Modify Data in an XML File
- Easily Import and Export Your Structured Data
- Store the Output of a Command in a CSV File
- Import Structured Data from a CSV File
- Use Excel to Manage Command Output
-
Chapter 9 Internet-Enabled Scripts
- Introduction
- Download a File from the Internet
- Download a Web Page from the Internet
- Program: Get-PageUrls
- Program: Connect-WebService
- Export Command Output As a Web Page
- Program: Send an Email
- Program: Interact with Internet Protocols
-
Chapter 10 Code Reuse
- Introduction
- Write a Script
- Write a Function
- Write a Script Block
- Return Data from a Script, Function, or Script Block
- Place Common Functions in a Library
- Access Arguments of a Script, Function, or Script Block
- Access Pipeline Input
- Write Pipeline-Oriented Scripts with Cmdlet Keywords
- Write a Pipeline-Oriented Function
-
Chapter 11 Lists, Arrays, and Hashtables
- Introduction
- Create an Array or List of Items
- Create a Jagged or Multidimensional Array
- Access Elements of an Array
- Visit Each Element of an Array
- Sort an Array or List of Items
- Determine Whether an Array Contains an Item
- Combine Two Arrays
- Find Items in an Array That Match a Value
- Remove Elements from an Array
- Find Items in an Array Greater or Less Than a Value
- Use the ArrayList Class for Advanced Array Tasks
- Create a Hashtable or Associative Array
- Sort a Hashtable by Key or Value
-
Chapter 12 User Interaction
- Introduction
- Read a Line of User Input
- Read a Key of User Input
- Program: Display a Menu to the User
- Display Messages and Output to the User
- Provide Progress Updates on Long-Running Tasks
- Write Culture-Aware Scripts
- Program: Invoke a Script Block with Alternate Culture Settings
- Access Features of the Host's User Interface
- Program: Add a Graphical User Interface to Your Script
-
Chapter 13 Tracing and Error Management
- Introduction
- View the Errors Generated by a Command
- Handle Warnings, Errors, and Terminating Errors
- Output Warnings, Errors, and Terminating Errors
- Debug a Script
- Collect Detailed Traces of a Script or Command
- Program: Analyze a Script's Performance Profile
-
Chapter 14 Environmental Awareness
- Introduction
- View and Modify Environment Variables
- Access Information About Your Command's Invocation
- Program: Investigate the InvocationInfo Variable
- Find Your Script's Name
- Find Your Script's Location
- Find the Location of Common System Paths
- Program: Search the Windows Start Menu
- Get the Current Location
- Safely Build File Paths Out of Their Components
- Interact with PowerShell's Global Environment
-
Chapter 15 Extend the Reach of Windows PowerShell
- Introduction
- Access Windows Management Instrumentation Data
- Program: Determine Properties Available to WMI Filters
- Program: Search for WMI Classes
- Use .NET to Perform Advanced WMI Tasks
- Convert a VBScript WMI Script to PowerShell
- Automate Programs Using COM Scripting Interfaces
- Program: Query a SQL Data Source
- Access Windows Performance Counters
- Program: Invoke Native Windows API Calls
- Program: Add Inline C# to Your PowerShell Script
- Access a .NET SDK Library
- Create Your Own PowerShell Cmdlet
- Add PowerShell Scripting to Your Own Program
-
Chapter 16 Security and Script Signing
- Introduction
- Enable Scripting Through an Execution Policy
- Sign a PowerShell Script or Formatting File
- Program: Create a Self-Signed Certificate
- Manage PowerShell Security in an Enterprise
- Verify the Digital Signature of a PowerShell Script
- Securely Handle Sensitive Information
- Securely Request Usernames and Passwords
- Program: Start a Process As Another User
- Securely Store Credentials on Disk
- Access User and Machine Certificates
- Program: Search the Certificate Store
-
Administrator Tasks
-
Chapter 17 Files and Directories
- Introduction
- Find All Files Modified Before a Certain Date
- Clear or Remove a File
- Manage and Change the Attributes of a File
- Get the Files in a Directory
- Find Files That Match a Pattern
- Manage Files That Include Special Characters
- Program: Get Disk Usage Information
- Determine the Current Location
- Monitor a File for Changes
- Program: Get the MD5 or SHA1 Hash of a File
- Create a Directory
- Remove a File or Directory
- Rename a File or Directory
- Move a File or Directory
- Get the ACL of a File or Directory
- Set the ACL of a File or Directory
- Program: Add Extended File Properties to Files
- Program: Create a Filesystem Hard Link
- Program: Create a ZIP Archive
-
Chapter 18 The Windows Registry
- Introduction
- Navigate the Registry
- View a Registry Key
- Modify or Remove a Registry Key Value
- Create a Registry Key Value
- Remove a Registry Key
- Add a Site to an Internet Explorer Security Zone
- Modify Internet Explorer Settings
- Program: Search the Windows Registry
- Get the ACL of a Registry Key
- Set the ACL of a Registry Key
- Work with the Registry of a Remote Computer
- Program: Get Registry Items from Remote Machines
- Program: Get Properties of Remote Registry Keys
- Program: Set Properties of Remote Registry Keys
- Discover Registry Settings for Programs
-
Chapter 19 Comparing Data
- Introduction
- Compare the Output of Two Commands
- Determine the Differences Between Two Files
- Verify Integrity of File Sets
-
Chapter 20 Event Logs
- Introduction
- List All Event Logs
- Get the Newest Entries from an Event Log
- Find Event Log Entries with Specific Text
- Retrieve a Specific Event Log Entry
- Find Event Log Entries by Their Frequency
- Back Up an Event Log
- Create or Remove an Event Log
- Write to an Event Log
- Access Event Logs of a Remote Machine
-
Chapter 21 Processes
- Introduction
- List Currently Running Processes
- Launch a Process
- Stop a Process
- Program: Invoke a PowerShell Expression on a Remote Machine
-
Chapter 22 System Services
- Introduction
- List All Running Services
- Manage a Running Service
- Access Services on a Remote Machine
-
Chapter 23 Active Directory
- Introduction
- Test Active Directory Scripts on a Local Installation
- Create an Organizational Unit
- Get the Properties of an Organizational Unit
- Modify Properties of an Organizational Unit
- Get the Children of an Active Directory Container
- Create a User Account
- Search for a User Account
- Get and List the Properties of a User Account
- Modify Properties of a User Account
- Create a Security or Distribution Group
- Search for a Security or Distribution Group
- Get the Properties of a Group
- Find the Owner of a Group
- Modify Properties of a Security or Distribution Group
- Add a User to a Security or Distribution Group
- Remove a User from a Security or Distribution Group
- List a User's Group Membership
- List the Members of a Group
- List the Users in an Organizational Unit
- Search for a Computer Account
- Get and List the Properties of a Computer Account
-
Chapter 24 Enterprise Computer Management
- Introduction
- Program: List Logon or Logoff Scripts for a User
- Program: List Startup or Shutdown Scripts for a Machine
- Enable or Disable the Windows Firewall
- Open or Close Ports in the Windows Firewall
- Program: List All Installed Software
- Uninstall an Application
- Manage Scheduled Tasks on a Computer
- Retrieve Printer Information
- Retrieve Printer Queue Statistics
- Manage Printers and Print Queues
- Determine Whether a Hotfix Is Installed
- Program: Summarize System Information
- Renew a DHCP Lease
- Assign a Static IP Address
- List All IP Addresses for a Computer
- List Network Adapter Properties
-
Chapter 25 Manage an Exchange 2007 Server
- Introduction
- Experiment with Exchange Management Shell
- Automate Wizard-Guided Tasks
- Manage Exchange Users
- Manage Mailboxes
- Manage Distribution Groups
- Manage Transport Rules
- Manage Outlook Web Access
-
Chapter 26 Manage an Operations Manager 2007 Server
- Introduction
- Experiment with the Command Shell
- Manage Operations Manager Agents
- Schedule a Maintenance Window
- Get, Install, and Uninstall Management Packs
- Enable or Disable Rules
- List and Start Tasks
- Manage Alerts
-
References
-
Appendix PowerShell Language and Environment
- Commands and Expressions
- Comments
- Variables
- Booleans
- Strings
- Numbers
- Arrays and Lists
- Hashtables (Associative Arrays)
- XML
- Simple Operators
- Comparison Operators
- Conditional Statements
- Looping Statements
- Working with the .NET Framework
- Writing Scripts, Reusing Functionality
- Managing Errors
- Formatting Output
- Capturing Output
- Tracing and Debugging
- Common Customization Points
-
Appendix Regular Expression Reference
-
Appendix PowerShell Automatic Variables
-
Appendix Standard PowerShell Verbs
-
Appendix Selected .NET Classes and Their Uses
-
Appendix WMI Reference
-
Appendix Selected COM Objects and Their Uses
-
Appendix .NET String Formatting
- String Formatting Syntax
- Standard Numeric Format Strings
- Custom Numeric Format Strings
-
Appendix .NET DateTime Formatting
- Custom DateTime Format Strings
-
Colophon
Return to Windows PowerShell Cookbook