This is a PowerShell book for administrators. It’ll show you how to use PowerShell v1, PowerShell v2, and the PowerShell functionality introduced with products such as SQL Server, Exchange, and Windows Server 2008 R2. Third-party additions will also be used where appropriate. We’ll see problems solved using scripts in version 1 with reference to cmdlets that were introduced in version 2. This is a deliberate decision to ensure that the book has the widest possible scope. PowerShell v1 isn’t going to disappear overnight and we need to be able to work across both versions in the near future.
I’ve aimed at covering the breadth of PowerShell, in terms of showing the number of different aspects of the environment we can control, and the depth in terms of showing the detailed and practical techniques for performing administrative tasks.
When you read the book, it’ll seem to be a hybrid. It lies somewhere between a cookbook of PowerShell recipes and an explanation of how to administer Windows-based systems. That’s deliberate in that I believe you can’t properly apply the automation techniques you’ll gain from the book unless the underlying technologies are understood. The book is a PowerShell book, so the explanations aren’t complete—just enough to explain why we’re performing a specific task.
Most of all, it’s a book to be used. Keep it on your desk and refer to it often. There are 205 techniques in the book, numbered consecutively and divided into sections called Problem, Solution, and Discussion. Techniques first appear in chapter 5. They should enable you to solve your particular problem. If not, a message on the Author Online forum will reach me and I may be able to supply some pointers. No promises, because I have a day job as well.
PowerShell in Practice is written for anyone interested in using PowerShell to automate the administration of her Windows environment. The obvious audience is administrators in a large enterprise environment, but the lone administrator in a smaller organization will gain as much if not more from the techniques described here.
The IT manager and IT architect audience will also benefit from viewing what it’s possible to achieve using PowerShell. Microsoft is releasing a number of workbooks that cover the actions that need to be taken to ensure the reliability of various components of the infrastructure such as DNS, Active Directory, or IIS. Many of the actions can be performed by PowerShell scripts taken directly, or adapted, from the techniques shown in the book.
Above all, this book is written for people interested in PowerShell and what can be accomplished with it. It’s not the last word on the subject—we’d need a book 5 or 10 times the size for that—but it does take you a long way on the journey to automation.
PowerShell in Practice is organized into three parts. The book opens with part 1, “Getting Started with PowerShell.” This introductory section covers the installation and configuration of PowerShell, together with the background knowledge we’ll need to work with other technologies such as WMI and Active Directory.
Chapter 1, “PowerShell fundamentals,” covers some of the background to PowerShell, including the major features of PowerShell such as cmdlets and providers, the PowerShell pipeline and the utility cmdlets we use for operations such as sorting and grouping. The chapter closes with an overview of the new features introduced in PowerShell v2.
Chapter 2, “Learning PowerShell,” discusses the installation and configuration of PowerShell and how we can use PowerShell to discover information about PowerShell, including the help system. We’ll also look at the language features we need to know, such as loops and branching. The use of scripts will be highlighted together with information on converting from other VBScript to PowerShell.
Chapter 3, “PowerShell toolkit,” covers the other technologies we need to know. PowerShell on its own can’t solve all of our administration problems. We need to use other technologies such as WMI, ADSI (for Active Directory), .NET to access functionality not built into PowerShell, and COM to work with applications such as Microsoft Office and Internet Explorer. How to use these technologies is covered in depth, with examples that are immediately usable.
Chapter 4, “Automating Administration,” concludes part 1. After a look at the way our administration scripts can evolve through an ad hoc development process, we examine some PowerShell best practices. These aren’t meant to dictate the way we work with PowerShell, but are more of a set of guidelines to help avoid major pitfalls. This chapter closes with an examination of how we can make our scripts secure, including how to use a code-signing certificate.
The three chapters of part 2, “Working with people,” describe how we administer those aspects of our environment that directly impact the user population. The 205 techniques covered in this book can be found, numbered chronologically, in parts 2 and 3. The final technique is in appendix D
In chapter 5, we look at the management of user accounts and groups. This covers local accounts and Active Directory accounts. In the enterprise environment, we’ll be mainly working with Active Directory, but there are a number of areas such as the DMZ where we still need local accounts.
In chapter 6, we turn our attention to Exchange mailboxes. The usual management functions for mailboxes and other mail-enabled objects are discussed together with mail protocols and quotas. We also discover how to report on mailbox statistics such as size and number of items.
Part 2 concludes with chapter 7, which discusses the administration of the user’s desktop. This includes system configuration, printers, special folders, and Microsoft Office applications such as Word and Excel.
The third and final part of the book, “Working with servers,” opens with chapter 8, “Windows servers,” in which we find techniques for working with services, processes, the filesystem, registry, and event logs. This is a linking chapter between parts 2 and 3, as many of these techniques can be applied to the desktop environment.
DNS is the subject of chapter 9. It’s a supporting technology for all modern Windows environments, and as such we need to be automate where appropriate. We can’t work directly with DNS, but we can use WMI and the techniques we learned in chapter 3.
Active Directory is revisited in the next two chapters. In chapter 10, we concentrate on Active Directory structure and work with organizational units. This leads us to administering GPOs through PowerShell and protecting objects from accidental deletion. Chapter 10 concludes with a look at recovering objects that have been deleted from Active Directory.
The physical topology is visited in chapter 11, with an examination of domain controllers, global catalogs, and Active Directory sites and subnets. We close out Active Directory by examining how we can administer site links and replication.The next three chapters demonstrate how we can use PowerShell to administer applications we’ll commonly find in a Windows environment. Chapter 12 deals with Exchange 2007/2010, where we learn how to work with data stores, mail servers, and the mail organization as a whole. The creation and management of Exchange policies is also covered.
IIS 7 is the topic of chapter 13. We learn how websites and applications can be managed by PowerShell cmdlets, a PowerShell provider, and WMI or .NET classes. Working with XML files completes the chapter. PowerShell remoting is heavily featured in this chapter.
In chapter 14, our attention turns to SQL Server. PowerShell functionality is directly available in SQL Server 2008, but we can use .NET based techniques to work with earlier versions. A framework configuration database is presented that can be created and administered by PowerShell.
The final chapter looks at PowerShell innovations, including new features introduced with PowerShell v2 and Windows Server 2008 R2. Topics include PowerShell background jobs, Server Manager cmdlets, Hyper-V PowerShell library, and new Active Directory functionality. We close the book with a brief glance at the administration of cloud based applications.
Five appendices are supplied. They cover PowerShell reference material including format files, PowerShell modules and advanced functions, PowerShell events, reference data, and useful links to downloads and further information.
This is a book about using PowerShell and there are a lot of examples provided throughout the book. A fixed-width font like this is used for all source code, and major blocks of code are formatted as a specific listing as, for example, this listing from chapter 5:
Listing 5.12 Searching for a user account
$struser = "BOSCH Herbert"$dom = System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()$root = $dom.GetDirectoryEntry()$search = [System.DirectoryServices.DirectorySearcher]$root$search.Filter = "(cn=$struser)"$result = $search.FindOne()if ($result -ne $null){$result.properties.distinguishedname}else {Write-Host $struser " Does not exist"}
These listings are annotated with full explanations provided in the text. In many cases, the code statements have been split across multiple lines to fit the page correctly. These lines terminate with a back tick (`), which is the PowerShell line continuation character.
Code examples are also be embedded in the text where they aren’t long enough to warrant an explicit listing. They are presented as follows:
Search-ADAccount -AccountDisabled -UsersOnly | select Name, distinguishedName
If the code has been typed directly at a PowerShell prompt, it’ll be displayed like this:
PS> 1kb1024
PowerShell has the ability to span multiple lines at the prompt, in which case the continuation lines will be prefixed by >>.
When discussing code examples, attribute names, cmdlet names, and all other PowerShell related items are displayed like this: - Get-Help about_Arrays.
Source code for the examples can be downloaded from the publisher’s website at http://www.manning.com/PowerShellinPractice.
Warning In my experience, any script obtained from the internet or any other source should be treated as suspect until proven otherwise. This includes the scripts in this book! I’ve tested them in my environment but I don’t know and can’t guarantee that they’re 100% safe for your environment. It’s your responsibility to test them in your environment.
In addition to the presentation conventions, I’ve also applied my own style to the code examples. I’ve used the following “rules”:
My intention is to provide a balance between readability, conciseness, and completeness. Only you can tell if I’ve succeeded.
Purchase of PowerShell in Practice includes free access to a private web forum run by Manning Publications where you can make comments about the book, ask technical questions, and receive help from the author and from other users. To access the forum and subscribe to it, point your web browser to http://www.manning.com/PowerShellinPractice. This page provides information on how to get on the forum once you are registered, what kind of help is available, and the rules of conduct on the forum.
Manning’s commitment to our readers is to provide a venue where a meaningful dialog between individual readers and between readers and the author can take place. It is not a commitment to any specific amount of participation on the part of the author, whose contribution to the AO remains voluntary (and unpaid). We suggest you try asking the author some challenging questions, lest his interest stray!
The Author Online forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.