To get the most out of this book
I designed and wrote this book based on some assumptions and with some constraints. Please read this section to understand how I intended the book to be used and what I have assumed about you. This should help you to get the most from this book.
The first assumption I made in writing this book is that you know the very basics of PowerShell. For that reason, this book is not a PowerShell tutorial. The recipes in this book make use of a wide range of PowerShell features, including WMI, Remoting, AD, and so on, but you need to know the basics of PowerShell. The book was developed using Windows 10 and both Windows Server 2019 and the emerging Windows Server 2022.
The second, related, assumption is that you have a reasonable background in Windows infrastructure, including AD, networking, and storage. The recipes in each chapter provide an overview of the various technologies, and I've tried to provide good links for more information on the topics in this book. The recipes are designed to show you the basics of how to manage aspects of Windows Server and how you might adapt them for your environment.
You start your exploration by installing and configuring PowerShell 7 and VS Code and creating Hyper-V VMs to test out each chapter's recipes. I built and tested the recipes in this book step by step (i.e. not running the entire recipe as a single script file). If you run a recipe as a single step, some of the output may not be what you see here, due to how PowerShell formats objects.
Once you have any recipe working, try to re-factor the recipe's code into your own reusable functions. In some cases, we build simple functions as a guide to richer scripts you could build. Once you have working and useful functions, incorporate them into organizational or personal modules and reuse the code.
As any author knows, writing PowerShell scripts for publication in a book is a layout and production nightmare. To reduce the issues specifically with line width and line wrapping, I have made extensive use of methods that ensure the command line width fits in the chapters in this book without wrapping. Many recipes use hash tables, property spatting, and other devices to ensure that every line of every recipe is 73 characters or less and that there are no unintended line breaks. I hope there are not too many issues with layout!
Many of the cmdlets, commands, and object methods used in this book produce output that may not be all that helpful or useful, particularly in production. Some cmdlets generate output that would fill many pages of this book but with little added value. For this reason, many recipes pipe cmdlet output to Out-Null
. Feel free to remove this where you want to see more details. I have also adjusted the output in many cases to avoid wasted white space.
Thus, if you test a recipe, you may see that the output is laid out a bit differently, but it should contain the same information. Finally, remember that the specific output you see may be different based on your environment and the specific values you use in each step.
To write this book, I have used a large VM farm consisting of over 12 Windows Server 2022 hosts and Windows 10 clients. My main development host was a well-configured Windows 10 system (96 GB RAM, 2 x 6 core Xeon processors, and fast SSDs). All the hosts used in this book are a combination of some physical hardware (running almost entirely on Windows 10 and a large set of VMs) as described in the recipe.
To assist in writing this book, I created a set of scripts that built the Hyper-V VMs that I used to develop this book. These scripts are published at https://github.com/doctordns/ReskitBuildScripts. I have also published some details of the network of VMs created by using these scripts, complete with host names and IP addresses, at the same URL. The full set of VMs, at the end of this writing, took up around 600 GB of storage. Fortunately, storage is cheap! The GitHub repository has more details on the scripts and how to run them. If you have any issues with the scripts, please file an issue on GitHub and I can assist.
PowerShell 7 provides great feature coverage with respect to being able to manage the functions and features of Windows Server 2022 using PowerShell. As with Windows PowerShell, you have considerable flexibility as to what commands you use in your scripts. While PowerShell cmdlets are generally your first choice, in some cases, you need to dip down into .NET, or into WMI, to get to objects, properties, and methods that no existing PowerShell command provides. And if that is not enough, you can develop your own .NET classes and full PowerShell 7 cmdlets.
An important aspect of the recipes in this book is the use of third-party modules obtained from the PowerShell Gallery. There is a rich and vibrant PowerShell community that has created a substantial amount of functionality for you to use. The PowerShell Gallery, a repository provided by Microsoft, enables you to download and use these modules. The NTFSSecurity
module, for example, makes it simple to manage the Access Control List (ACL) on NTFS files and folders.
All the code provided in this book has been tested. It worked when I tested it, and it did what it says (at least during the writing stage). I have taken some liberties with respect to the layout and formatting to cater for the book's production and printing process, but you should get the same results. That said, the book production process is very complex and it is possible that errors can creep in during the production stages. So if you find a step in any recipe that fails for you, file an issue on my GitHub repository for this book (see below). For generic issues, please post issues to the Spiceworks PowerShell forum.
In writing the recipes, I have used full cmdlet names with all parameter names spelled out in full. This makes the text a bit longer, but hopefully easier to read and understand.
In writing this book, I set out to create content around a number of features of Windows Server 2022. In order to publish the book, it was necessary to avoid going too deep into every Windows feature. I have had to decide which features (and commands) to show and which to not cover, since every chapter could easily have become a small book. To paraphrase Jeffrey Snover, to ship is to choose. I hope I chose well.
Some recipes in this book rely on you having run other recipes in prior chapters. These related recipes worked well when we wrote and tested them and hopefully work for you as well. If you have problems with any of the recipes, then raise issues on my GitHub repository.
Finally, there is a fine line between PowerShell and a Windows feature. To use PowerShell to manage a Windows feature, you need to understand the Windows feature itself. The chapters provide short overviews of the Windows Server features and I have provided links to help you get more information. And as ever, Bing and Google are your best friends.
Download the example code files and VM build scripts
I have published every recipe (and a bit more) to a public GitHub repository: https://github.com/PacktPublishing/Windows-Server-Automation-with-PowerShell-7.1-Cookbook-Fourth-Edition. There is a README.md
file at the top of the repository introducing what is in the repo. Within the scripts
folder, you can find all the recipes within this book.
Should you find any issues with the recipes in this repository, please file an issue at https://github.com/PacktPublishing/Windows-Server-Automation-with-PowerShell-7.1-Cookbook-Fourth-Edition/issues and I can assist.
This book makes use of a farm of Hyper-V VMs that you can use to replicate the recipes. I have created a set of VM build scripts you can download from my GitHub repository at https://github.com/doctordns/ReskitBuildScripts. To use these scripts, you need to obtain an ISO image of Windows Server 2022 to serve as a base image. You can get that from https://www.microsoft.com/evalcenter/evaluate-windows-server-2022-preview/ or via your Visual Studio or other subscription. You use the ISO image to create a "reference disk" – then for each VM, the scripts create a unique VM based on the reference disk. This VM is a brand-new install based on unattended XML used to pre-configure each VM. In theory, you could create these VMs in Azure, but I have not tested that. You can read more about how to use the build scripts from the README.md
document in the repository.
Download the color images
We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://static.packt-cdn.com/downloads/9781800568457_ColorImages.pdf.
Conventions used
There are a number of text conventions used throughout this book.
CodeInText
: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. For example; "To establish a link shortcut, you can use the Wscript.Shell
COM object."
A block of code is set as follows:
"On Host [$(hostname)]"
"Total features available [{0}]" -f $Features.count
"Total features installed [{0}]" -f $FeaturesI.count
"Total RSAT features available [{0}]" -f $RSATF.count
"Total RSAT features installed [{0}]" -f $RSATFI.count
Bold: Indicates a new term, an important word, or words that you see on the screen, for example, in menus or dialog boxes, also appear in the text like this. For example: "Signing a script is simple once you have a digital certificate issued by a Certificate Authority (CA)."
Warnings or important notes appear like this.
Tips and tricks appear like this.