Windows Server 2019 Automation with PowerShell Cookbook
上QQ阅读APP看书,第一时间看更新

Getting the most from this book

This book was written based on some assumptions and with some constraints. You should 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 basics of PowerShell. This is not a PowerShell tutorial. The recipes do 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 uses PowerShell language, syntax, and cmdlets that come with Windows Server 2019 and Windows 10.

The second, related, assumption is that you have a reasonable background in Windows infrastructure including AD, networking and storage. The recipes provide an overview of the various technologies and links for more information. And I provide links to more information on the topics in this book.

The recipes provide the basics—you adopt, adapt, and extend. The recipes are designed to show you the basics of how to manage aspects of Windows Server. In many cases, a recipe suggests method for improving it for your environment. A recipe is meant to show you how features work, enabling you to leverage and extend it for your environment.

Start by running the recipes step-by-step using either the PowerShell ISE or VS Code. An exception is the recipes in Chapter 8 (Introducing Containers). The main tool you use in that chapter, docker.exe, runs differently in a GUI (ISE or VS Code), so run these recipes from the PowerShell console.

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 in to 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 and property spatting and other devices to ensure that every line of every recipe is both 65 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 which 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 have used a large VM farm consisting of over 20 Windows 2019 servers 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 which 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, is at: https://github.com/doctordns/ReskitBuildScripts. The full set of VMs, at the end of this writing, took up around 600 GB of storage. Fortunately, storage is cheap!

PowerShell provides great feature coverage—you can manage most of the functions and features of Windows Server 2019 using PowerShell, but by no means all. In some cases, you can dip down into WMI using the CIM cmdlets to get to object properties and methods not exposed by any cmdlet. The advent of CDXML-based cmdlets has increased the number of networking and other cmdlets that are WMI-based. But even then, there are still a number of places where you need to use a Windows console application or invoke an unmanaged DLL. The bottom line is that to manage some aspects of Windows, such as event forwarding or performance logging, you need to use older tools. We try to avoid these, but in many cases the recipe demonstrates how to use the console applications within PowerShell.

In many cases, there are no official Microsoft authored cmdlets included in Windows Server 2019 that might help you manage aspects of your infrastructure. The advent of the PS Gallery, and great third-party add-ins, has meant that you can find a wealth of functionality, download it and install it.

All the code provided in this book has been tested; it worked and did what it says (at least during the writing stage). The production process is complex and it's possible that errors in code creep in during the production stages. Some of the more complex steps may have errors introduced during production. If any step fails for you, please contact Packt and we can help. Feel free to post issues to the Spiceworks PowerShell forum for quick resolution.

In writing the recipes, I use full cmdlet names with no aliases and with all parameter names spelled out in full. Thus, no abbreviated parameter names or positional parameters). 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 2019. As the book progressed, we quickly hit (and broke) several content limits. In order to publish the book, it was necessary to remove some content, which we did most reluctantly. Just about 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 other recipes being completed. These related recipes worked well when we wrote them and hopefully work for you as well.

There is a fine line between PowerShell and a Windows feature. To use PowerShell to manage a Windows feature, you need to understand the feature itself. The chapters describe each feature although in the space limited, thus I can't provide complete details of every feature. I have provided links to help you get more information. And as ever, Bing and Google are your best friends.

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. Here is an example: "Create a PSSession with both HV servers."

A block of code is set as follows:

$Sb = {
  Install-WindowsFeature -Name Hyper-V -IncludeManagementTools
}
Invoke-Command -ComputerName HV1, HV2 -ScriptBlock $Sb

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. Here is an example: "Select System info from the Administration panel."

Note

Warnings or important notes appear like this.

Tip

Tips and tricks appear like this.