logo资料库

PowerShell脚本编写手册.pdf

第1页 / 共687页
第2页 / 共687页
第3页 / 共687页
第4页 / 共687页
第5页 / 共687页
第6页 / 共687页
第7页 / 共687页
第8页 / 共687页
资料共687页,剩余部分请下载后查看
Cover
Copyright Page
Dedication
Contents at a Glance
Table of Contents
Acknowledgments
Introduction
Is This Book for Me?
About the Companion CD
System Requirements
Technical Support
Chapter 1: The Shell in Windows PowerShell
Installing Windows PowerShell
Verifying Installation with VBScript
Deploying Windows PowerShell
Interacting with the Shell
Introducing Cmdlets
Configuring Windows PowerShell
Creating a Windows PowerShell Profile
Configuring Windows PowerShell Startup Options
Security Issues with Windows PowerShell
Controlling the Execution of Cmdlets
Confirming Commands
Suspending Confirmation of Cmdlets
Supplying Options for Cmdlets
Working with Get-Help
Working with Aliases to Assign Shortcut Names to Cmdlets
Additional Uses of Cmdlets
Using the Get-ChildItem Cmdlet
Formatting Output
Using the Get-Command Cmdlet
Exploring with the Get-Member Cmdlet
Summary
Chapter 2: Scripting Windows PowerShell
Why Use Scripting?
Configuring the Scripting Policy
Running Windows PowerShell Scripts
Use of Variables
Use of Constants
Using Flow Control Statements
Adding Parameters to ForEach-Object
Using the Begin Parameter
Using the Process Parameter
Using the End Parameter
Using the For Statement
Using Decision-Making Statements
Using If … Elseif … Else
Using Switch
Working with Data Types
Unleashing the Power of Regular Expressions
Using Command-Line Arguments
Summary
Chapter 3: Managing Logs
Identifying the Event Logs
Reading the Event Logs
Exporting to Text
Export to XML
Perusing General Log Files
Examining Multiple Logs
Retrieving a Single Event Log Entry
Searching the Event Log
Filtering on Properties
Selecting the Source
Selecting the Severity
Selecting the Message
Managing the Event Log
Identifying the Sources
Modifying the Event Log Settings
Examining WMI Event Logs
Making Changes to the WMI Logging Level
Using the Windows Event Command-Line Utility
Writing to Event Logs
Creating a Source
Putting Cmdlet Output into the Log
Creating Your Own Event Logs
Summary
Chapter 4: Managing Services
Documenting the Existing Services
Working with Running Services
Writing to a Text File
Writing to a Database
Setting the Service Configuration
Accepting Command-Line Arguments
Stopping Services
Performing a Graceful Stop
Starting Services
Performing a Graceful Start
Desired Configuration Maintenance
Verifying Desired Services Are Stopped
Reading a File to Check Service Status
Verifying Desired Services Are Running
Confirming the Configuration
Producing an Exception Report
Summary
Chapter 5: Managing Shares
Documenting Shares
Documenting User Shares
Writing Shares to Text
Documenting Administrative Shares
Writing Share Information to a Microsoft Access Database
Auditing Shares
Modifying Shares
Using Parameters with the Script
Translating the Return Code
Creating New Shares
Creating Multiple Shares
Deleting Shares
Deleting Only Unauthorized Shares
Summary
Chapter 6: Managing Printing
Inventorying Printers
Querying Multiple Computers
Logging to a File
Writing to a Microsoft Access Database
Reporting on Printer Ports
Identifying Printer Drivers
Installing Printer Drivers
Installing Printer Drivers Found on Your Computer
Installing Printer Drivers Not Found on Your Computer
Summary
Chapter 7: Desktop Maintenance
Maintaining Desktop Health
Inventorying Drives
Writing Disk Drive Information to Microsoft Access
Working with Partitions
Matching Disks and Partitions
Working with Logical Disks
Monitoring Disk Space Utilization
Logging Disk Space to a Database
Monitoring File Longevity
Monitoring Performance
Using Performance Counter Classes
Identifying Sources of Page Faults
Summary
Chapter 8: Networking
Working with Network Settings
Reporting Networking Settings
Working with Adapter Configuration
Filtering Only Properties that Have a Value
Configuring Network Adapter Settings
Detecting Multiple Network Adapters
Writing Network Adapter Information to a Microsoft Excel Spreadsheet
Identifying Connected Network Adapters
Setting Static IP Address
Enabling DHCP
Configuring the Windows Firewall
Reporting Firewall Settings
Configuring Firewall Settings
Summary
Chapter 9: Configuring Desktop Settings
Working with Desktop Configuration Issues
Setting Screen Savers
Auditing Screen Savers
Listing Only Properties with Values
Reporting Secure Screen Savers
Managing Desktop Power Settings
Changing the Power Scheme
Summary
Chapter 10: Managing Post-Deployment Issues
Setting the Time
Setting the Time Remotely
Logging Results to the Event Log
Configuring the Time Source
Using the Net Time Command
Querying the Registry for the Time Source
Enabling User Accounts
Creating a Local User Account
Creating a Local User
Creating a Local User Group
Configuring the Screen Saver
Renaming the Computer
Shutting Down or Rebooting a Remote Computer
Summary
Chapter 11: Managing User Data
Working with Backups
Configuring Offline Files
Enabling the Use of Offline Files
Working with System Restore
Retrieving System Restore Settings
Listing Available System Restore Points
Summary
Chapter 12: Troubleshooting Windows
Troubleshooting Startup Issues
Examining the Boot Configuration
Examining Startup Services
Displaying Service Dependencies
Examining Startup Device Drivers
Investigating Startup Processes
Investigating Hardware Issues
Troubleshooting Network Issues
Summary
Chapter 13: Managing Domain Users
Creating Organizational Units
Creating Domain Users
Modifying User Attributes
Modifying General User Information
Modifying the Address Tab
Modifying the Profile Tab
Modifying the Telephone Tab
Modifying the Organization Tab
Modifying a Single User Attribute
Creating Users from a .csv File
Setting the Password
Enabling the User Account
Creating Domain Groups
Adding a User to a Domain Group
Adding Multiple Users with Multiple Attributes
Summary
Chapter 14: Configuring the Cluster Service
Examining the Clustered Server
Reporting Cluster Configuration
Reporting Node Configuration
Querying Multiple Cluster Classes
Managing Nodes
Adding and Evicting Nodes
Removing the Cluster
Summary
Chapter 15: Managing Internet Information Services
Enabling Internet Information Services Management
Reporting IIS Configuration
Reporting Site Information
Reporting on Application Pools
Reporting on Application Pool Default Values
Reporting Site Limits
Listing Virtual Directories
Creating a New Web Site
Creating a New Application Pool
Starting and Stopping Web Sites
Summary
Chapter 16: Working with the Certificate Store
Locating Certificates in the Certificate Store
Listing Certificates
Locating Expired Certificates
Identifying Certificates about to Expire
Managing Certificates
Inspecting a Certificate
Importing a Certificate
Deleting a Certificate
Summary
Chapter 17: Managing the Terminal Services Service
Configuring the Terminal Service Installation
Documenting Terminal Service Configuration
Disabling Logons
Modifying Client Properties
Managing Users
Enabling Users to Access the Server
Configuring Client Settings
Summary
Chapter 18: Configuring Network Services
Reporting DNS Settings
Configuring DNS Logging Settings
Reporting Root Hints
Querying “ A” Records
Configuring DNS Server Settings
Reporting DNS Zones
Creating DNS Zones
Managing WINS and DHCP
Summary
Chapter 19: Working with Windows Server 2008 Server Core
Initial Configuration
Joining the Domain
Setting the IP Address
Configuring the DNS Settings
Renaming the Server
Managing Windows Server 2008 Server Core
Monitoring the Server
Querying Event Logs
Summary
Appendix A: Cmdlet Naming Conventions
Appendix B: ActiveX Data Object Provider Names
Appendix C: Frequently Asked Questions
Appendix D: Scripting Guidelines
General Script Construction
Include Functions in the Script that Calls the Function
Use Full Cmdlet Names and Full Parameter Names
Use Get-Item to Convert Path Strings to Rich Types
General Script Readability
Formatting Your Code
Working with Functions
Creating Template Files
Writing Functions
Creating and Naming Variables and Constants
Appendix E: General Troubleshooting Tips
Index
About the Author
PUBLISHED BY Microsoft Press A Division of Microsoft Corporation One Microsoft Way Redmond, Washington 98052-6399 Copyright © 2008 by Ed Wilson All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission of the publisher. Library of Congress Control Number: 2007941089 Printed and bound in the United States of America. 1 2 3 4 5 6 7 8 9 QWT 3 2 1 0 9 8 Distributed in Canada by H.B. Fenn and Company Ltd. A CIP catalogue record for this book is available from the British Library. Microsoft Press books are available through booksellers and distributors worldwide. For further infor- mation about international editions, contact your local Microsoft Corporation office or contact Microsoft Press International directly at fax (425) 936-7329. Visit our Web site at www.microsoft.com/mspress. Send comments to mspinput@microsoft.com. Microsoft, Microsoft Press, Active Directory, ActiveX, Excel, Internet Explorer, MSDN, MSN, Outlook, SQL Server, Visual Basic, Windows, Windows NT, Windows PowerShell, Windows Server, Windows Vista, and Zune are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. Other product and company names mentioned herein may be the trademarks of their respective owners. The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious. No association with any real company, organization, product, domain name, e-mail address, logo, person, place, or event is intended or should be inferred. without any express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its resellers, or distributors will be held liable for any damages caused or alleged to be caused either directly or indirectly by this book. Acquisitions Editor: Martin DelRe Developmental Editor: Karen Szall Project Editor: Denise Bankaitis and Michelle Goodman Editorial Production: Custom Editorial Productions, Inc. Technical Reviewer: Bob Hogan; Technical Review services provided by Content Master, a member of CM Group, Ltd. Cover: Tom Draper Design Body Part No. X14-14922
A03D622791.fm Page iii Friday, December 14, 2007 10:52 AM This book is dedicated to Teresa. I am so glad you decided to share your life with me.
A03D622791.fm Page iv Friday, December 14, 2007 10:52 AM
A04G622791.fm Page v Friday, December 14, 2007 10:52 AM Contents at a Glance The Shell in Windows PowerShell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 Scripting Windows PowerShell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 2 3 Managing Logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 4 Managing Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 5 Managing Shares . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 6 Managing Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 7 Desktop Maintenance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 8 Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 9 Configuring Desktop Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 10 Managing Post-Deployment Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 11 Managing User Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 12 Troubleshooting Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 13 Managing Domain Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379 14 Configuring the Cluster Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405 15 Managing Internet Information Services . . . . . . . . . . . . . . . . . . . . . . . . 443 16 Working with the Certificate Store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473 17 Managing the Terminal Services Service. . . . . . . . . . . . . . . . . . . . . . . . . 509 18 Configuring Network Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541 19 Working with Windows Server 2008 Server Core . . . . . . . . . . . . . . . . . 583 Cmdlet Naming Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619 A B ActiveX Data Object Provider Names . . . . . . . . . . . . . . . . . . . . . . . . . . . 621 Frequently Asked Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623 C Scripting Guidelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631 D E General Troubleshooting Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639 v
A04G622791.fm Page vi Friday, December 14, 2007 10:52 AM
A05T622791.fm Page vii Friday, December 14, 2007 10:52 AM Table of Contents 1 Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Is This Book for Me?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix About the Companion CD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx System Requirements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Technical Support. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi The Shell in Windows PowerShell. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 Installing Windows PowerShell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Verifying Installation with VBScript. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Deploying Windows PowerShell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Interacting with the Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Introducing Cmdlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Configuring Windows PowerShell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Creating a Windows PowerShell Profile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Configuring Windows PowerShell Startup Options . . . . . . . . . . . . . . . . . . . . . . . . 6 Security Issues with Windows PowerShell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Controlling the Execution of Cmdlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Confirming Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Suspending Confirmation of Cmdlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Supplying Options for Cmdlets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Working with Get-Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Working with Aliases to Assign Shortcut Names to Cmdlets . . . . . . . . . . . . . . . . . . . . 15 Additional Uses of Cmdlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Using the Get-ChildItem Cmdlet. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Formatting Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Using the Get-Command Cmdlet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 What do you think of this book? We want to hear from you! Microsoft is interested in hearing your feedback so we can continually improve our books and learning resources for you. To participate in a brief online survey, please visit: www.microsoft.com/learning/booksurvey/ vii
A05T622791.fm Page viii Friday, December 14, 2007 10:52 AM viii Table of Contents 2 Exploring with the Get-Member Cmdlet. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Scripting Windows PowerShell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Why Use Scripting? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Configuring the Scripting Policy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Running Windows PowerShell Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Use of Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Use of Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Using Flow Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Adding Parameters to ForEach-Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Using the Begin Parameter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Using the Process Parameter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Using the End Parameter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Using the For Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Using Decision-Making Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Using If … Elseif … Else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Using Switch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Working with Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Unleashing the Power of Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Using Command-Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 3 Managing Logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Identifying the Event Logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Reading the Event Logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Exporting to Text. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Export to XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Perusing General Log Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Examining Multiple Logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Retrieving a Single Event Log Entry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Searching the Event Log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Filtering on Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Selecting the Source. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Selecting the Severity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Selecting the Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
分享到:
收藏