Automating Microsoft Windows Server 2008 R2 with Windows PowerShell 2.0 Matthew Hester Sarah Dutkiewicz WILEY Wiley Publishing. Inc.
TABLE OF CONTENTS Introduction xvii Chapter 1 What Is PowerShell, and Why Do You Need It? 1 Why PowerShell? 2 Overview of PowerShell 3 The Power Behind PowerShell 5 What About the Learning Curve? 6 What's New in PowerShell 2.0? 7 PowerShell in the Enterprise 8 PowerShell with a GUI 10 PowerShell Has Something for Everyone 13 What's in It for IT Professionals? 14 What's in It for Developers? 15 Chapter 2 Installing and Configuring PowerShell 2.0 19 Chapter Configure PowerShell 2.0 on Windows Server 2008 R2 20 Install the ISE on Windows Server 2008 R2 22 Install PowerShell 2.0 on Windows Server 2008 R2 Core 24 Install.NET Framework Support on Windows Server 2008 R2 Core 26 Install PowerShell 2.0 on Windows Server 2008 R2 Core 26 Install PowerShell 2.0 on Other Operating Systems 29 Set Up the Prerequisites 29 Obtain and Install PowerShell 2.0 31 3 PowerShell Grammar Lesson 37 Break Down PowerShell: A Lesson on Cmdlets.38 Learn the Verbs 39 Learn the Nouns 40 Put Verbs and Nouns Together: Cmdlets 41 Use Parameters 42 Use Wildcards 45 Understand Properties 46 Help Yourself with PowerShell 48 Learn How to Help Yourself 48 Use Tab Autocomplete 53 Leverage Online Resources 54 Use Even More Commands with Modules 55 Use and Understand Modules 55
Get to Know Your Modules 58 Create a Custom PowerShell Shell 60 Chapter 4 Aliases, Functions, and the Pipe, Oh My! 65 Use Aliases 66 Use Built-in Aliases 67 Create Your Own Aliases 69 Use Functions 77 Understand Functions 78 Use Existing Functions 79 Create Your Own Function 80 Work with the Pipe Operator 81 Use the Pipe Operator to Combine PowerShell Cmdlets 81 Control PowerShell Output 82 Chapter 5 Creating Your Own Scripts 97 Create Your Own Scripts 98 PowerShell Scripting Overview 99 Create a PowerShell Script 100 Run Your Scripts 106 Find Scripts 107 Understand Security and PowerShell Scripts 108 Work with Default Execution Policy of Scripts 109 Understand the RemoteSigned PowerShell Execution Policy 110 Set the PowerShell Execution Policy 111 Understand Digital Signing 116 Work with the GUI and the Shell 116 Understand What the ISE Is 116 Use the ISE with Scripts 119 Display the Call Stack with Scripts Requiring Parameters 123 Chapter 6 Remoting with PowerShell 2.0 125 Configure PowerShell Remoting 126 Learn the Requirements 126 Enable PowerShell Remoting 128 Disable PowerShell Remoting 133 Run Commands on Remote Systems 138 Use Invoke-Command 140 Use PowerShell Remote Sessions 140 Use Remoting in the ISE 143 Chapter 7 Server Essentials in PowerShell 147 Work with Your Server in PowerShell 148
Work with Server Manager Cmdlets 148 Analyze Your Server 150 Add Reliability to Your Server 156 Install the Backup Tools 156 Perform a Backup with PowerShell 156 Load-Balance Your Network 159 Use Other PowerShell Utilities for the Server 163 Use the Registry with PowerShell 163 Use PowerShell Troubleshooting Packs 166 Schedule PowerShell Scripts 169 Chapter 8 Managing Active Directory with PowerShell 173 Work with Active Directory 174 Load the AD PowerShell Module 174 Understand PowerShell Active Directory Basics 178 Work with Users, Groups, and OUs 182 Understand Managed Service Accounts 185 Understand Managed Service Accounts 185 Create Managed Service Accounts 186 Install and Use Managed Service Accounts 188 Work with the Active Directory Recycle Bin 189 Understand How the Recycle Bin Works 190 Enable the AD Recycle Bin 191 Use the AD Recycle Bin 193 Chapter 9 Managing Desktops with PowerShell 197 Access Group Policy 198 Understand Group Policy 199 Understand How Group Policy Works 200 Manage Group Policy 2 1 Create Group Policy Objects 204 Use Starter GPOs 206 Work with Settings 208 Understand the Difference Between Policies and Preferences 209 Work with Domain Password Policies 210 Understand Order of Precedence 212 Control Group Policy Order of Precedence 213 Work with RSOP 215 Back Up and Restore Group Policy Objects 216 Manage AppLocker 217 Understand AppLocker 217 Understand AppLocker Policy 218 Configure AppLocker 219
Chapter 10 Managing IIS Web Server with PowerShell 223 Use PowerShell and IIS 224 Work with Configuration Files 225 Back Up and Recover IIS Configuration 229 Deploy Websites, Application Pools, and Virtual Directories with PowerShell 231 Manage Sites with PowerShell 231 Work with Web Application Pools 236 Work with Virtual Directories 240 Chapter 11 PowerShell and Deployment Services 243 Work with Windows Deployment Services 244 Understand WDS 245 Install WDS 246 Work with WDS in PowerShell 247 UseWDSUTIL 248 Work with the Microsoft Deployment Toolkit 253 Understand the MDT 253 Deploy with Windows Imaging Format 254 Install MDT 256 Work with MDT in PowerShell 257 Put It All Together 265 Chapter 12 PowerShell and Virtualization 267 Install and Access Hyper-V 268 Install Hyper-V 269 Access Hyper-V in PowerShell 270 Work with Hyper-V 276 Work with Virtual Networks 276 Configure Virtual Machines 278 Connect to Virtual Machines 283 Work with Snapshots 285 Appendix A Solutions to Exercises 289 Solution V. Inventory Your Scripts 290 Solution 2: Install PowerShell 290 Solution 3: Create a PowerShell Profile 290 Solution 4: Create Your Own Alias 291 Solution 5: Create a Script to Find Startup Programs 292 Solution 6: Set Up a Remote PowerShell Session 292 Solution 7: Create a Scheduled Backup with PowerShell 293 Solution 8: Populate an Active Directory Test Environment 293 Solution 9: Turn Off the Display Control Panel in Group Policy with PowerShell 295 Solution 10: Create a Website with PowerShell 296
Solution 11: Create a Deployment Share 296 Solution 12: Create a Virtual Machine and Take a Snapshot 297 Appendix B Developing at a Command Prompt 299 Choose Between the ISE and the Command Prompt 300 Write Code at a Command Prompt 301 Write Scripts in the ISE 302 Work with Objects in PowerShell 304 Understand Properties 305 Create Your Own Custom Object 307 Appendix C Providing for PowerShell 311 Work with Built-in Providers 312 Understand Provider Basics 313 Use PowerShell-Specific Providers 314 Use Other Built-in Providers 317 Work with Additional Providers 322 Install and Remove Providers 322 Create Your Own Provider 323 Understand Basic Provider Concepts 323 Build a Custom Provider 327 Appendix D Custom Cmdlets and Advanced Functions 339 Choose Between an Advanced Function and a Cmdlet 340 Parameters and Attributes 340 Output 346 Runtime Life Cycle 347 Create an Advanced Function 348 Create a Custom Cmdlet 349 Appendix E Packaging PowerShell Extensions 357 Work with Existing Snap-ins 358 Create a Custom Module 361 Understand Module Concepts 361 Build Your Module 368 Appendix F Building Your Own GUI with PowerShell 379 Choose Between WinForms and WPF 380 Create a GUI in PowerShell 381 Create a WinForms Application 385 Create a WPF Application 391 Index 399