Tải bản đầy đủ (.pdf) (30 trang)

Tài liệu Windows PowerShell Programming P1 ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (689.16 KB, 30 trang )


Kumaravel ffirs.tex V1 - 01/09/2008 8:43pm Page iii
Professional
Windows PowerShell

Programming
Snap-ins, Cmdlets, Hosts, and Providers
Arul Kumaravel
Jon White
Michael Naixin Li
Scott Happell
Guohui Xie
Krishna C. Vutukuri
Wiley Publishing, Inc.
Kumaravel ffirs.tex V1 - 01/09/2008 8:43pm Page ii
Kumaravel ffirs.tex V1 - 01/09/2008 8:43pm Page i
Professional
Windows PowerShell

Programming
Preface xvii
Introduction xix
Chapter 1: Introduction to PowerShell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Chapter 2: Extending Windows PowerShell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Chapter 3: Understanding the Extended Type System . . . . . . . . . . . . . . . . . . . . . . 29
Chapter 4: Developing Cmdlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Chapter 5: Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Chapter 6: Hosting the PowerShell Engine in Applications . . . . . . . . . . . . . . . . . 165
Chapter 7: Hosts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Chapter 8: Formatting&Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Appendix A: Cmdlet Verb Naming Guidelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257


Appendix B: Cmdlet Parameter Naming Guidelines . . . . . . . . . . . . . . . . . . . . . . . . 263
Appendix C: Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Appendix D: Provider Base Classes and Overrides/Interfaces.. . . . . . . . . . . . . 283
Appendix E: Core Cmdlets for Provider Interaction . . . . . . . . . . . . . . . . . . . . . . . . . 303
Index 307
Kumaravel ffirs.tex V1 - 01/09/2008 8:43pm Page ii
Kumaravel ffirs.tex V1 - 01/09/2008 8:43pm Page iii
Professional
Windows PowerShell

Programming
Snap-ins, Cmdlets, Hosts, and Providers
Arul Kumaravel
Jon White
Michael Naixin Li
Scott Happell
Guohui Xie
Krishna C. Vutukuri
Wiley Publishing, Inc.
Kumaravel ffirs.tex V1 - 01/09/2008 8:43pm Page iv
Windows PowerShell

Programming:
Snap-ins, Cmdlets, Hosts, and Providers
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright  2008 by Wiley Publishing, Inc., Indianapolis, Indiana

Published simultaneously in Canada
ISBN: 978-0-470-17393-0
Manufactured in the United States of America
10987654321
Library of Congress Cataloging-in-Publication Data
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any
means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections
107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or
authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood
Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be
addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317)
572-3447, fax (317) 572-4355, or online at
/>.
Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties
with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties,
including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended
by sales or promotional materials. The advice and strategies contained herein may not be suitable for every
situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting,
or other professional services. If professional assistance is required, the services of a competent professional person
should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an
organization or Website is referred to in this work as a citation and/or a potential source of further information
does not mean that the author or the publisher endorses the information the organization or Website may provide
or recommendations it may make. Further, readers should be aware that Internet Websites listed in this work may
have changed or disappeared between when this work was written and when it is read.
For general information on our other products and services please contact our Customer Care Department within the
United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related trade dress
are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and
other countries, and may not be used without written permission. Windows PowerShell is a trademark of Microsoft
Corporation in the United States and/or other countries. All other trademarks are the property of their respective

owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be
available in electronic books.
Kumaravel fauth.tex V2 - 01/07/2008 10:17pm Page v
About the Author
Arul Kumaravel is currently the Development Manager of the Windows PowerShell team. He has
worked with this team since its early days and led the team in shipping of version 1 of the product,
and is presently leading the development of next version of PowerShell. Fascinated by computers from
an early age, when he first learned programming using BASIC, he went on to get his Master of Science
degree in Computer Science from both the College of Engineering, Madras, India, and the University
of Iowa. As a Microsoft intern, he wrote the first JavaScript/VBScript debugger for Internet Explorer 3,
and was impressed by the potential to make a difference in millions of people’s lives by working for
Microsoft. He has been working at Microsoft for the past 11 years in various groups, shipping multiple
versions of products, including Internet Explorer, the Windows operating system, and Content Manage-
ment Server, and has even dabbled with Software as a Service with small business online services. More
recently, attracted by the business side of technology, Arul has taken on the arduous task of pursuing his
M.B.A. at the Wharton Business School. He can be reached at

.
Jon White is a software engineer who lives and works in the idyllic surroundings of Seattle’s eastern
suburbs. An original member of the PowerShell team at Microsoft, his professional career started in
the Administrative Tools group in Windows Server. As a hobbyist, Jon learned programming in his
early teens after his father bought an 8088-based PC clone at a second-hand shop. The PC came with
MS-DOS 2.0, which featured
debug.exe
with a 16-bit disassembler, but no assembler. As a result, Jon’s
first dive into programming was disassembling long tables of bytes to create a reverse-lookup dictionary
for manually converting assembly programs into executable binary code. Coincidentally, later in life he
filed the bug which removed
debug.exe

from 64-bit Windows. As a member of the PowerShell team,
he wrote the language’s first production script, when he converted the team’s test harness from Perl to
PowerShell script in 2004. When he’s not working (or writing about work) he’s either sailing or playing
with fire in the backyard. You can contact him at

.
Michael Naixin Li is the Senior Test Lead working on the Windows PowerShell team and currently
oversees the testing of Windows PowerShell 2.0. Before Windows PowerShell, Michael worked on vari-
ous major projects at Microsoft, including the development of MSN 1.x and 2.x, quality management for
the COM Services component in Windows 2000, NetDocs Web Client Access, Web Services in Hailstorm,
and Software Licensing Service in Windows Vista. Before joining Microsoft, Michael was an assistant
professor at Shanghai University of Science and Technology (now called ShanghaiUniversity).Heholds
a Ph.D. in Computer Science from Colorado State University.
Scott Happell has been working as a software engineer and tester for 10 years. Three of those years have
been on the Windows PowerShell team, which was what brought him to Microsoft from New Jersey,
where he worked at an Internet startup that went belly-up. Scott recently left Microsoft to become a
recording engineer/rock star and is trying to find cool ways to use PowerShell to help him create music.
George Xie was a Senior Developer in the Windows PowerShell team for three years, mainly focusing
in the area of snap-in model and scripting language. Recently George joined Windows Mobile organi-
zation for the Mobile Device Management product. Before joining Microsoft, George worked for Siebel
Systems Inc. for several years.
Krishna Chythanya Vutukuri is a Software Developer working on the Windows PowerShell team. Before
Windows PowerShell, Krishna worked on various projects at Microsoft, which included the development
of Windows Presentation Foundation. Before joining Microsoft, Krishna held various product develop-
ment positions at Hewlett-Packard India Software Operations and Wipro Technologies. He holds a M.Sc
(Tech.) in Information Systems from Birla Institute of Technology and Science, Pilani, India.
Kumaravel fauth.tex V2 - 01/07/2008 10:17pm Page vi
Kumaravel fcredit.tex V1 - 01/07/2008 10:18pm Page vii
Credits
Executive Editor

Chris Webb
Development Editor
Howard Jones
Technical Editor
Marco Shaw
Production Editor
Rachel McConlogue
Copy Editor
Luann Rouff
Editorial Manager
Mary Beth Wakefield
Production Manager
Tim Tate
Vice President and Executive Group Publisher
Richard Swadley
Vice President and Executive Publisher
Joseph B. Wikert
Project Coordinator, Cover
Lynsey Osborn
Kumaravel fcredit.tex V1 - 01/07/2008 10:18pm Page viii
Kumaravel ftoc.tex V2 - 01/08/2008 12:04am Page ix
Contents
Preface xvii
Introduction xix
Chapter 1: Introduction to PowerShell 1
Windows PowerShell Design Principles 1
Preserve the Customer’s Existing Investment 2
Provide a Powerful, Object-Oriented Shell 2
Extensibility, Extensibility, Extensibility 2
Tear Down the Barriers to Development 2

A Quick Tour of Windows PowerShell 3
Cmdlets 3
High-Level Architecture of Windows PowerShell 9
Host Application 9
Windows PowerShell Engine 10
Windows PowerShell Snap-ins 10
Summary 11
Chapter 2: Extending Windows PowerShell 13
Types of PowerShell Snap-ins 13
Creating a Standard PowerShell Snap-in 14
Writing a PowerShell Snap-in 14
Registering Your PowerShell Snap-in 17
Listing Available PowerShell Snap-ins 19
Loading a PowerShell Snap-in to a Running Shell 19
Removing a PowerShell Snap-in from a Running Shell 20
Unregistering a PowerShell Snap-in 20
Registering a PowerShell Snap-in without Implementing a Snap-in Clas 21
Saving Snap-in Configuration 22
Starting PowerShell with a Saved Snap-in Configuration 22
Using a Profile to Save a Snap-in Configuration 23
Creating a Custom PowerShell Snap-in 23
Writing a Custom PowerShell Snap-in 23
Using a Custom PowerShell Snap-in 25
Summary 27

×