Book Image

Windows Server Automation with PowerShell Cookbook - Fifth Edition

By : Thomas Lee
Book Image

Windows Server Automation with PowerShell Cookbook - Fifth Edition

By: Thomas Lee

Overview of this book

The Windows Server Automation with PowerShell Cookbook is back with a new edition, featuring over 100 PowerShell recipes that will make your day-to-day work easier. This book is designed to help you learn how to install, configure and use PowerShell 7.2 effectively. To start with, we’ll look at how to install and configure PowerShell 7.2, along with useful new features and optimizations, and show you how the PowerShell compatibility solution bridges the gap to older versions of PowerShell. We’ll also be covering a wide range of fundamental and more advanced use cases, including how to create a VM and set up an Azure VPN, as well as looking at how to back up to Azure. As you progress, you’ll explore topics such as using PowerShell to manage networking and DHCP in Windows Server, objects in Active Directory, Hyper-V, and Azure. We’ll also take a closer look at WSUS, containers and see how to handle modules that are not directly compatible with PowerShell 7. Finally, you’ll also learn how to use some powerful tools to diagnose and resolve issues with Windows Server. By the end of this PowerShell book, you’ll know how to use PowerShell 7.2 to automate tasks on Windows Server 2022 with ease, helping your Windows environment to run faster and smoother.
Table of Contents (17 chapters)
15
Other Books You May Enjoy
16
Index

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 out of this book.

The first assumption I made in writing this book is that you know the very basics of Windows 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/11 and 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. 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 book’s chapters without wrapping. Many recipes use hash tables, property splatting, 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 the output that 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 created a VM farm consisting of 14 Windows Server 2022 hosts. My main development host was a well-configured Windows 11 system (with 128 GB RAM, 2 x 16 core Xeon processors, and several fast SSDs). My host runs all the VMs in this book simultaneously. If your computing power is more modest, you can spin up just the VMs you need. I suggest you have a minimum of 16GB of RAM.

To assist in writing this book, I have also created a set of scripts that build the Hyper-V VMs, which I then used to develop and test the recipes in this book. I have published these scripts at: https://github.com/doctordns/ReskitBuildScripts. I have also published some details of the network of VMs created by using these scripts, complete with hostnames and IP addresses, at: https://github.com/doctordns/ReskitBuildScripts/blob/master/ReskitNetwork.md. The full set of VMs, at the end of writing this, took up around 500 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.

The build scripts are pretty easy to use, and I have added details on how to approach these scripts in the Readme.md file.

PowerShell 7 provides great feature coverage for managing Windows Server 2022 using PowerShell. PowerShell offers considerable flexibility in 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 PowerShell cmdlets do not provide.

An important aspect of the recipes in this book is the use of third-party modules obtained from the PowerShell Gallery. A rich and vibrant PowerShell community has created a substantial amount of functionality for you to use. The PowerShell Gallery is a repository provided by Microsoft. With PowerShell, you can, download and use the resources available in the Gallery. The NTFSSecurity module, for example, makes it easier to manage the Access Control List (ACL) on NTFS files and folders.

I have tested all the code provided in this book. It worked when I tested it and did what it says (at least during the writing stage). I have taken some liberties regarding the layout and formatting of screenshots to cater to the book’s production and printing process, but you should get the same results when you run these scripts.

The book production process is very complex, and errors can creep in during production. So if you find a step in any recipe that fails, file an issue in my GitHub repository for this book, and for generic issues, please post issues to the Spiceworks PowerShell forum at .

When writing the recipes, I use full cmdlet names with all parameter names spelled out in full. This approach makes the text a bit longer, but hopefully easier to read and understand. I have also used a variety of different ways you can achieve results.

In writing this book, I set out to create content around many features of Windows Server 2022. To publish the book, I have to avoid going too deep into every Windows Feature. I have had to decide which Windows Server features (and commands) to show and which not to cover since every chapter could easily have become its own 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 previous 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 recipes, raise issues on my GitHub repository, and I can take a look and help.

Finally, there is a fine line between PowerShell and Windows Server features. To use PowerShell to manage any Windows feature, you need to understand the Windows feature as well as understand PowerShell. 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

The code bundle for the book is hosted on GitHub at https://github.com/PacktPublishing/Windows-Server-Automation-with-PowerShell-Cookbook-5th-edition. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

The full and up-to-date repository for this book’s scripts is at: https://github.com/doctordns/PacktPS72/.

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://packt.link/SlBcp.

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: “ The System.IO.FileInfo class has a static method new() that enables you to create a new file.”

Any command-line input or output is written as follows:

$Manifest = Get-Content -Path $Mod.Path
$Manifest | Select-Object -First 20

Bold: Indicates a new term, an important word, or words that you see on the screen. For instance, words in menus or dialog boxes appear in the text like this. For example: “ Click on Open File to run the .NET SDK installer.”

Warnings or important notes appear like this.

Tips and tricks appear like this.