Automating
Microsoft®
Windows
Server®
2008 R2
with
Windows
PowerShell®
2.0
Matthew Hester
Sarah Dutkiewicz
WILEYTABLE OF
CONTENTS
Introduction xvii
Chapter
1 What IsPowerShell,
andWhy
Do You Need It? 1Why
PowerShell? 2Overview of PowerShell 3 The Power Behind PowerShell 5 What About the
Learning
Curve? 6What's New in PowerShell2.0? 7 PowerShellinthe
Enterprise
8PowerShell withaGUI 10
PowerShell Has
Something
forEveryone 13What'sinIt for IT Professionals? 14
What's in It forDevelopers? 15
Chapter
2Installing
andConfiguring
PowerShell 2.0 19Configure
PowerShell2.0onWindows Server 2008 R2 20Install the ISEonWindows Server 2008 R2 22
Install PowerShell 2.0onWindows Server 2008 R2 Core 24
Install .NET Framework
Support
onWindows Server 2008 R2 Core 26Install PowerShell 2.0onWindows Server 2008 R2 Core 26
Install PowerShell 2.0onOther
Operating
Systems 29SetUpthe
Prerequisites
29 Obtain and Install PowerShell 2.0 31Chapter
3PowerShell
GrammarLesson 37Break Down PowerShell: A LessononCmdlets .38 Learn the Verbs 39 Learn the Nouns 40 Put Verbs and NounsTogether: Cmdlets 41 Use Parameters 42
Use Wildcards 45
UnderstandProperties 46
HelpYourself withPowerShell 48
Learn HowtoHelpYourself 48
UseTabAutocomplete 53
LeverageOnline Resources 54 UseEven More Commands with Modules 55
Get to Know YourModules 58
CreateaCustomPowerShell Shell 60
Chapter
4Aliases, Functions,
andthe
Pipe,
Oh
My!
65Use Aliases 66 Use Built-in Aliases 67 Create Your Own Aliases 69 Use Functions 77
Understand Functions 78
Use
Existing
Functions 79CreateYourOwn Function 80
Work with the
Pipe
Operator 81 UsethePipe OperatortoCombine PowerShell Cmdlets 81ControlPowerShell Output 82
Chapter
5Creating Your Own
Scripts
97Create Your OwnScripts 98
PowerShellScriptingOverview 99
CreateaPowerShell Script 100
Run YourScripts 106
Find Scripts 107
Understand
Security
and PowerShellScripts
108Work with Default Execution
Policy
ofScripts
109Understand the
RemoteSigned
PowerShellExecutionPolicy
110 Setthe PowerShellExecutionPolicy
111UnderstandDigital Signing 116 Workwith theGUI and theShell 116 Understand Whatthe ISE Is 116
Use the ISE with
Scripts
119DisplaytheCall StackwithScripts RequiringParameters 123
Chapter
6Remoting
withPowerShell
2.0 125Configure PowerShell
Remoting
126Learn theRequirements 126
Enable PowerShell
Remoting
128Disable PowerShell
Remoting
133Run Commandson Remote
Systems
138Use Invoke-Command 140 Use PowerShellRemoteSessions 140 UseRemotingintheISE 143
Chapter
7 Server EssentialsinPowerShell
147Work with ServerManagerCmdlets 148
Analyze
Your Server 150 AddReliability
toYour Server 156 Install theBackup
Tools 156 PerformaBackup
with PowerShell 156 Load-BalanceYourNetwork 159UseOther PowerShell Utilities fortheServer 163 UsetheRegistry with PowerShell 163
UsePowerShell
Troubleshooting
Packs 166Schedule PowerShell Scripts 169
Chapter
8Managing
ActiveDirectory
with PowerShell 173Work with ActiveDirectory 174
Load the AD PowerShell Module 174
Understand PowerShell ActiveDirectoryBasics 178
Work withUsers,
Groups,
and OUs 182Understand
Managed
Service Accounts 185Understand
Managed
ServiceAccounts 185Create
Managed
Service Accounts 186Install andUse
Managed
ServiceAccounts 188Work with the ActiveDirectory RecycleBin 189
Understand Howthe RecycleBin Works 190
Enable the ADRecycleBin 191 Use the AD RecycleBin 193
Chapter
9Managing Desktops
with PowerShell 197Access
Group Policy
198Understand
Group Policy
199Understand How
Group Policy
Works 200Manage Group
Policy
2°1CreateGroup
Policy Objects
204Use StarterGPOs 206
Work withSettings 208
Understand the Difference BetweenPoliciesand Preferences 209 Work with Domain PasswordPolicies 210 Understand Order of Precedence 212 Control
Group Policy
Order of Precedence 213 Work with RSOP 215Back
Up
andRestoreGroupPolicy Objects
216Manage
AppLocker
217 UnderstandAppLocker
217UnderstandAppLocker
Policy
218Chapter
10Managing
IIS Web Server with PowerShell 223Use PowerShell andIIS 224 Workwith
Configuration
Files 225BackUpand Recover IISConfiguration 229
DeployWebsites,ApplicationPools,andVirtualDirectories withPowerShell 231
ManageSites with PowerShell 231 WorkwithWebApplication Pools 236 Work with Virtual Directories 240
Chapter
11PowerShell and
Deployment
Services 243Work with Windows
Deployment
Services 244Understand WDS 245
Install WDS 246
Work withWDS inPowerShell 247
UseWDSUTIL 248
Workwiththe Microsoft
Deployment
Toolkit 253Understand the MDT 253
Deploy
withWindowsImagingFormat 254InstallMDT 256
Workwith MDT in PowerShell 257 PutItAllTogether 265
Chapter
12 PowerShelland
Virtualization 267Install and Access
Hyper-V
268Install
Hyper-V
269Access
Hyper-V
inPowerShell 270Work with
Hyper-V
276Work with Virtual Networks 276
Configure
Virtual Machines 278ConnecttoVirtual Machines 283
Work with
Snapshots
285Appendix
A Solutionsto Exercises 289SolutionV.
Inventory
YourScripts 290 Solution2:InstallPowerShell 290Solution3: CreateaPowerShell Profile 290
Solution4: Create Your Own Alias 291
Solution5: CreateaScripttoFind
Startup Programs
292Solution6: SetUpaRemote PowerShell Session 292
Solution7:CreateaScheduledBackupwith PowerShell 293
Solution 8:PopulateanActive
Directory
TestEnvironment 293Solution 9: Turn Off the
Display
Control PanelinGroup Policy
withPowerShell 295 Solution 10: CreateaWebsite with PowerShell 296Solution11:Createa
Deployment
Share 296 Solution12:CreateaVirtual Machine andTakeaSnapshot 297Appendix
BDeveloping
ata CommandPrompt
299ChooseBetweentheISEand the CommandPrompt 300
Write Codeat aCommand Prompt 301
WriteScriptsin theISE 302
Work with Objectsin PowerShell 304
UnderstandProperties 305
CreateYourOwn CustomObject 307
Appendix
CProviding
for
PowerShell 311Work with Built-in Providers 312 Understand Provider Basics 313
UsePowerShell-SpecificProviders 314
Use Other Built-in Providers 317 Work with Additional Providers —322
Install and Remove Providers 322 CreateYourOwn Provider 323 UnderstandBasicProviderConcepts 323 BuildaCustom Provider 327
Appendix
D CustomCmdlets
andAdvanced Functions
339Choose BetweenanAdvanced Function andaCmdlet 340
ParametersandAttributes 340
Output 346 Runtime LifeCycle 347 CreateanAdvanced Function 348
CreateaCustom Cmdlet 349
Appendix
EPackaging
PowerShell Extensions 357Work withExisting
Snap-ins
358 CreateaCustomModule 361 Understand ModuleConcepts 361 BuildYourModule 368Appendix
FBuilding
YourOwn GUI
with PowerShell 379Choose Between WinForms andWPF 380 CreateaGUI in PowerShell 381
CreateaWinForms
Application
385CreateaWPF