Sams Teach Yourself Windows PowerShell In 24 Hours - Pearsoncmg

Transcription

Timothy WarnerWindowsPowerShell

Sams Teach Yourself Windows PowerShell in 24 HoursCopyright 2015 by Pearson Education, Inc.All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, ortransmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, withoutwritten permission from the publisher. No patent liability is assumed with respect to the use ofthe information contained herein. Although every precaution has been taken in the preparation ofthis book, the publisher and author assume no responsibility for errors or omissions. Nor is anyliability assumed for damages resulting from the use of the information contained herein.ISBN-13: 9780672337284ISBN-10: 0672337282Library of Congress Control Number: 2015900973Printed in the United States of AmericaFirst Printing May 2015TrademarksAll terms mentioned in this book that are known to be trademarks or service marks have beenappropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Useof a term in this book should not be regarded as affecting the validity of any trademark or servicemark.Warning and DisclaimerEvery effort has been made to make this book as complete and as accurate as possible, but nowarranty or fitness is implied. The information provided is on an “as is” basis. The author and thepublisher shall have neither liability nor responsibility to any person or entity with respect to anyloss or damages arising from the information contained in this book.Special SalesFor information about buying this title in bulk quantities, or for special sales opportunities (whichmay include electronic versions; custom cover designs; and content particular to your business,training goals, marketing focus, or branding interests), please contact our corporate sales department at corpsales@pearsoned.com or (800) 382-3419.For government sales inquiries, please contact governmentsales@pearsoned.com.For questions about sales outside the U.S., please contact international@pearsoned.com.Editor-in-ChiefGreg WiegandAcquisitions EditorJoan MurrayDevelopmentEditorSondra ScottManaging EditorKristy HartProject EditorAndy BeasterCopy EditorKeith ClineIndexerCheryl LenserProofreaderKatie MatejkaTechnical EditorJeff WoutersPublishingCoordinatorCindy TeetersCover DesignerMark ShirarCompositorGloria Schurick

Contents at a GlanceIntroduction . 1Part I: Introducing Windows PowerShellHOUR 1 Getting to Know Windows PowerShell . 72 Installing and Configuring Windows PowerShell. 253 Mastering the Windows PowerShell Help System . 474 Finding and Discovering Windows PowerShell Commands . 69Part II: Understanding Objects and the PipelineHOUR 5 Thinking in Terms of Objects . 916 Mastering the Windows PowerShell Pipeline . 1097 Sorting, Filtering, and Measuring Windows PowerShell Output. 133Part III: Extending the Reach of Windows PowerShellHOUR 8 Managing Windows PowerShell Providers. 1539 Formatting, Exporting, and Converting WindowsPowerShell Output . 175Part IV: Managing Computers Remotely with Windows PowerShellHOUR 10 Implementing One-to-One Windows PowerShell Remoting . 20111 Implementing One-to-Many Windows PowerShell Remoting . 22112 Deploying PowerShell Web Access . 237Part V: Putting Windows Powershell to WorkHOUR 13 Multitasking Windows PowerShell . 25514 Harnessing Windows PowerShell Workflow . 27515 Introducing WMI and CIM . 29316 Searching and Filtering with Regular Expressions . 313Part VI: Enterprise-Class Windows PowerShellHOUR 17 Managing Software with Windows PowerShell OneGet . 33118 Desired State Configuration Basics . 355

ivSams Teach Yourself Windows PowerShell in 24 HoursPart VII: Scripting with Windows PowerShellHOUR 19 Introduction to Windows PowerShell Scripting . 37720 Making PowerShell Code Portable with Modules . 399Part VIII: Administering Microsoft Enterprise Servers withWindows PowerShellHOUR 21 Managing Active Directory with Windows PowerShell . 41722 Managing SQL Server with Windows PowerShell . 43723 Managing SharePoint Server with Windows PowerShell. 45324 Managing Microsoft Azure with Windows PowerShell. 471Index . 493

Table of ContentsIntroduction1Who Should Read This Book . 1How This Book Is Organized . 2Conventions Used in This Book . 4System Requirements. 5Part I: Introducing Windows PowerShellHOUR 1: Getting to Know Windows PowerShell7Why You Should Learn Windows PowerShell . 8Brief History of Windows PowerShell . 10Understanding the Windows PowerShell Components . 13Investigating the Power and Simplicity of Windows PowerShell. 18Summary . 21Q&A . 22Workshop. 23HOUR 2: Installing and Configuring Windows PowerShell25Determining Your Windows PowerShell Environment . 26Installing the Latest Version of Windows PowerShell . 29Customizing the Windows PowerShell Console . 32Customizing the Windows PowerShell ISE . 39Summary . 42Q&A . 42Workshop. 44HOUR 3: Mastering the Windows PowerShell Help System47Anatomy of a Windows PowerShell Cmdlet. 47Updating the Windows PowerShell Help Library . 50Understanding Windows PowerShell Help Syntax . 54

viSams Teach Yourself Windows PowerShell in 24 HoursAccessing Additional Command Help. 60Summary . 66Q&A . 66Workshop. 67HOUR 4: Finding and Discovering Windows PowerShell Commands69How Windows PowerShell Commands Are Packaged . 69Installing RSAT Tools on Windows 8.1. 73Locating Windows PowerShell Commands . 75Running External Commands . 83Summary . 86Q&A . 87Workshop. 88Part II: Understanding Objects and the PipelineHOUR 5: Thinking in Terms of Objects91The Problem with UNIX/Linux . 91What Is an Object? . 93Discovering Object Members. 96Putting Objects into Action . 102Summary . 105Q&A . 105Workshop. 106HOUR 6: Mastering the Windows PowerShell Pipeline109Understanding How the Pipeline Worksfrom a High Level. 109Understanding in Depth How thePipeline Works . 113Passing Data Through the Pipeline . 118“Forcing” Objects Through the Pipeline . 125Summary . 129Q&A . 130Workshop. 131

ContentsHOUR 7: Sorting, Filtering, and Measuring Windows PowerShell Outputvii133Sorting Output . 134Filtering Output . 137Measuring Objects . 144Summary . 148Q&A . 148Workshop. 150Part III: Extending the Reach of Windows PowerShellHOUR 8: Managing Windows PowerShell Providers153What Are Providers? . 153Introduction to Default PSDrives . 155Using the FileSystem Provider . 159Using the Alias Provider . 166Using the Registry Provider . 167Using Extended Providers . 169Summary . 171Q&A . 171Workshop. 172HOUR 9: Formatting, Exporting, and Converting Windows PowerShell Output175How the PowerShell FormattingSubsystem Works . 176Formatting PowerShell Output . 180Exporting PowerShell Output . 187Converting PowerShell Output . 193Summary . 197Q&A . 198Workshop. 198

viiiSams Teach Yourself Windows PowerShell in 24 HoursPart IV: Managing Computers Remotely with Windows PowerShellHOUR 10: Implementing One-to-One Windows PowerShell Remoting201Understanding Classic Windows PowerShell Remote Access . 201Introducing “True” PowerShell Remoting . 203Enabling Windows PowerShell Remoting . 205Creating a Windows PowerShell Remote Session . 209Sending Scripts over the Network . 214Summary . 217Q&A . 218Workshop. 218HOUR 11: Implementing One-to-Many Windows PowerShell Remoting221One-to-Many Remote Access in theClassic Scenario . 221One-to-Many Remoting with Persistent Sessions . 224Managing Session Configurations . 225One-to-Many Remoting with the Windows PowerShell ISE . 228Passing Input to Remote Commands . 231Summary . 233Q&A . 233Workshop. 235HOUR 12: Deploying Windows PowerShell Web Access237Introducing Windows PowerShell Web Access . 237Setting Up the Windows PSWA Gateway . 239Testing the Windows PSWA User Experience . 244Managing the Gateway . 249Summary . 251Q&A . 252Workshop. 252

ContentsixPart V: Putting Windows Powershell to WorkHOUR 13: Multitasking Windows PowerShell255Investigating the PowerShell Job Architecture . 255Controlling Job Behavior . 259Understanding Parent and Child Jobs . 261Introducing the –AsJob Parameter. 263Scheduling Jobs . 266Reviewing What We’ve Learned . 270Summary . 272Q&A . 273Workshop. 273HOUR 14: Harnessing Windows PowerShell Workflow275Understanding How Windows PowerShell Workflow Works . 276Writing Your First Windows PowerShell Workflow . 279Running a Workflow as a Job . 282Understanding Workflow Activities . 283Tying Everything Together . 288Summary . 289Q&A . 290Workshop. 291HOUR 15: Introducing WMI and CIM293Defining WMI and CIM . 293Getting Comfortable with WMI . 296Using Windows PowerShell WMI Commands . 300Using Windows PowerShell CIM Commands . 305Summary . 308Q&A . 308Workshop. 310HOUR 16: Searching and Filtering with Regular Expressions313Revisiting the Wildcard Operators . 313Understanding Regular Expressions . 315Using the –Match Parameter . 317

xSams Teach Yourself Windows PowerShell in 24 HoursUsing Select-String . 324Using the RegEx Type Accelerator . 327Summary . 327Q&A . 328Workshop. 329Part VI: Enterprise-Class Windows PowerShellHOUR 17: Managing Software with Windows PowerShell OneGet331Understanding IT-Related Terminology . 332Preparing Your Environment . 334Browsing Package Repositories . 336Installing Software from the Command Line. 340Managing Providers and Packages . 346Hosting a Private OneGet Repository . 349Summary . 350Q&A . 351Workshop. 352HOUR 18: Desired State Configuration Basics355Historical Background of DSC. 355Basic Tenets of DSC . 357DSC Authoring Environment . 358Configuring the DSC Environment . 359Writing Your First Configuration Script . 364A Word on DSC Push Configuration . 372Summary . 373Q&A . 373Workshop. 375Part VII: Scripting with Windows PowerShellHOUR 19: Introduction to Windows PowerShell Scripting377Managing Execution Policy . 377Writing Our First Script: The User Profile . 379Writing a PowerShell Function . 383

ContentsxiAdding Programming Logic . 385Running Scripts . 388Pointers to Master PowerShell Scripting . 395Summary . 395Q&A . 396Workshop. 397HOUR 20: Making PowerShell Code Portable with Modules399Understanding Snap-Ins. 399Introducing PowerShell Modules . 401Creating Your First PowerShell Script Module . 403Using Module Manifests . 407Adding Comment-Based Help . 410Finding Modules Easily . 413Summary . 414Q&A . 414Workshop. 415Part VIII: Administering Microsoft Enterprise Servers with WindowsPowerShellHOUR 21: Managing Active Directory with Windows PowerShell417Installing Active Directory . 417Creating Common Active Directory Objects . 423Understanding Various AD Administrative Tasks . 430Summary . 433Q&A . 433Workshop. 434HOUR 22: Managing SQL Server with Windows PowerShell437Running PowerShell Using SQL Server Tools . 438Interacting with SQL Server Using PowerShell . 442Automating Common SQL Server DBA Tasks . 443Summary . 449Q&A . 450Workshop. 451

xiiSams Teach Yourself Windows PowerShell in 24 HoursHOUR 23: Managing SharePoint Server with Windows PowerShell453Understanding the Environment . 453Deploying a Service Application. 457Deploying a Web Application . 459Deploying a Site Collection . 460Setting Permissions on a Site Collection . 462Reporting on a SharePoint Farm . 463Summary . 468Q&A . 468Workshop. 468HOUR 24: Managing Microsoft Azure with Windows PowerShell471Defining Microsoft Azure . 472Preparing Your Azure-PowerShell Environment . 474Working with Azure Virtual Machines . 477Managing Office 365 and SharePoint Online with Azure . 485Summary . 488Q&A . 489Workshop. 490INDEX493

About the AuthorTimothy Warner is an IT professional and technical trainer based in Nashville, Tennessee.Tim became acquainted with information technology in 1982 when his dad bought thefamily a Timex Sinclair 1000 home computer and he taught himself BASIC programming. Today he works as an author/evangelist for Pluralsight and shares WindowsPowerShell knowledge with anyone who’ll listen at his Two Minute PowerShell blog:http://2minutepowershell.com. You can reach Tim directly via LinkedIn:http://linkedin.com/in/timothywarner.

DedicationTo all my students, past and present.Thank you for giving me a professional calling,and I hope that my work helps you attain your goals.AcknowledgmentsThe Windows PowerShell community is a terrific group of people. Thank you, Jeffrey Snover,Bruce Payette, and Lee Holmes et al. for giving the world Windows PowerShell. Thanks to allthe PowerShell experts in the world for being so kind and willing to share your knowledge. Iseek to emulate your actions every day.It may take a village to raise a child, but I know that it takes a large office full of talentedprofessionals to publish a book. To that end, I want to thank my wonderful editor JoanMurray for having faith in my abilities. Thanks to my publisher, Greg Wiegand, for being soreceptive to my ideas.Editorial and production staff rarely receive the credit they deserve. Thanks so much toWindows PowerShell MVP Jeff Wouters, my technical editor, for being so thorough with themanuscript. Truly, this book is at least twice as good as it originally was thanks to you.Thanks to Keith Cline, my copyeditor, for making my writing easier to follow. Keith knowsthat I gave his editing skills quite a workout, for sure. Sorry, Keith!I extend my gratitude as well to Andy Beaster, my production editor, and to the ever-helpfulCindy Teeters for streamlining the entire book publishing process. Andy and Cindy are professionals in every sense of the word.Thanks to my family, friends, and co

The Windows PowerShell community is a terrific group of people. Thank you, Jeffrey Snover, Bruce Payette, and Lee Holmes et al. for giving the world Windows PowerShell. Thanks to all the PowerShell experts in the world for being so kind and willing to share your knowledge. I seek to emulate your actions every day.