Book Image

Windows Server 2019 Automation with PowerShell Cookbook - Third Edition

By : Thomas Lee
Book Image

Windows Server 2019 Automation with PowerShell Cookbook - Third Edition

By: Thomas Lee

Overview of this book

Windows Server 2019 is the latest version of Microsoft’s flagship server operating system. It also comes with PowerShell Version 5.1 and offers a number of additional features that IT professionals will find useful. This book is designed to help you learn how to use PowerShell and manage the core roles, features, and services of Windows Server 2019. You will begin by creating a PowerShell Administrative Environment that features updated versions of PowerShell, the Windows Management Framework, .NET Framework, and third-party modules. Next, you will learn to use PowerShell to set up and configure Windows Server 2019 networking and understand how to manage objects in the Active Directory (AD) environment. The book will also guide you in setting up a host to utilize containers and deploying containers. Further along, you will be able to implement different mechanisms to achieve Desired State Configuration. The book will then get you up to speed with Azure infrastructure, in addition to helping you get to grips with setting up virtual machines (VMs), websites, and file share on Azure. In the concluding chapters, you will be able to deploy some powerful tools to diagnose and resolve issues with Windows Server 2019. By the end of this book, you will be equipped with a number of useful tips and tricks to automate your Windows environment with PowerShell.
Table of Contents (19 chapters)
Windows Server 2019 Automation with PowerShell Cookbook Third Edition
Foreword
Contributors
Preface
Index

Preface

PowerShell was first introduced to the world at the Professional Developer's conference in Los Angeles in 2003 by Jeffrey Snover. Code-named Monad, it represented a complete revolution in management. A white paper written around that time, The Monad Manifesto (refer to http://www.jsnover.com/blog/2011/10/01/monad-manifesto/) remains an amazing analysis of the problem at the time – that of managing large numbers of Windows systems. A key takeaway is that the GUI does not scale, whereas PowerShell does.

PowerShell has transformed the management of complex, network-based Windows infrastructure, and, increasingly, non-Windows infrastructure. Knowledge of PowerShell and how to get the most from PowerShell is now obligatory for any professional IT Pro. The popular adage continues to be true: Learn PowerShell or learn golf.

This book takes you through the use of PowerShell in a variety of scenarios using many of the rich set of features included in Windows Server 2019. This preface provides you with an introduction to what is in the book, along with some tips on how to get the most out of it.

What this book covers

Chapter 1, Establishing a PowerShell Administrative Environment, looks at setting up your work station and your environment to make use of PowerShell.

Chapter 2, Managing Windows Networking, shows you how to manage Windows networking with PowerShell. Networks are today central to almost every organization and this chapter looks at a variety of network-related tasks, including looking at new ways (with PowerShell) to do old things, setting up DNS, DHCP, and Active directory.

Chapter 3, Managing Windows Active Directory, examines how to install, manage and leverage Active Directory, including managing AD objects and Group Policy.

Chapter 4, Managing Windows Storage, looks at managing storage in Windows Server 2019.

Chapter 5, Managing Shared Data, examines different ways to share data with Windows Server and PowerShell including SMB shares, iSCSI and Distributed File System.

Chapter 6, Managing Windows Update, helps you get to grips with managing updates via Windows Update. With the importance of keeping all your Windows clients and servers fully patched.

Chapter 7, Managing Printers, shows you how to manage printers, printer queues, and printer drivers, including deploying printers via Group Policy.

Chapter 8, Leveraging Containers, introduces Docker Containers in Windows Server 2019. You download and use container images as well as build your own.

Chapter 9, Managing Windows Internet Information Server, shows you how to conduct a variety of IIS-related tasks, including IIS installation and configuration, setting up SSL and managing cipher suites, as well as configuring Network Load Balancing.

Chapter 10, Managing Desired State Configuration, shows how to use this important feature to ensure a server is setup correctly and continues to remain so. This covers setting up a pull server and configuring partial configurations.

Chapter 11, Managing Hyper-V, demonstrates the use of Hyper-V. This chapter shows you how to build and deploy VMs with Hyper-V. This includes nested Hyper-V running a Hyper-V VM inside another Hyper-V VM which is useful for a number of scenarios.

Chapter 12, Managing Azure, looks at managing IAAS and PAAS resources in Azure using PowerShell. To test the recipes in this chapter, you need access to Azure. This chapter describes how to setup a Virtual Machine, an Azure website and an SMB3 file share.

Chapter 13, Managing Performance and Usage, how to measure, monitor, and report on the performance of your Windows 2019 servers. There are several recipes that demonstrate how to get specific performance measurements and how to create graphs of performance for further analysis.

Chapter 14, Troubleshooting Windows Server, looks at a number of aspects of both reactive and proactive troubleshooting. This includes getting events from the event log and using the Best Practice Analyzer contained in Windows Server 2019.

What you need for this book

To get the most out of this book, you need to experiment with the code contained in the recipes. To avoid errors impacting live production servers, you should instead use either test hardware, or adopt virtualization to create a test lab, where mistakes do not cause any serious damage.

This book uses a variety of servers within a single Reskit.Org domain containing multiple servers, and using an IP address block of 10.10.10/24 described in the Getting the most from this book section.

You should have a Windows 10 or Windows Server 2019 host with hardware virtualization capabilities enabled and use a virtualization solution. If you have access to a cloud computing platform, then you could perform most of the recipes on cloud-hosted virtual machines, although that has not been tested by us. You can use any virtualization.

The book was developed using Hyper-V and nested Hyper-V on Windows 10 and Windows Server 2019.

Who this book is for

This book is aimed at IT professionals, including system administrators, system engineers, architects, and consultants who need to leverage Windows PowerShell to simplify and automate their daily tasks.

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.

Note

Tips and tricks appear like this.

Sections

In this book, you find several headings that appear frequently (Getting ready, How to do it, How it works, There's more, and See also).

To give clear instructions on how to complete a recipe, we use these sections as follows.

Chapter and recipe headings

Every chapter and every recipe introduces some part of Windows which the recipes help you to manage. I've attempted to summarize the key points about each feature - but as ever there is more detail you can discover using your favorite search engine.

Getting ready

This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe. It also indicates the hosts (VMs) you need for the recipe and any files, folders, or other resources you need to complete the recipe successfully.

How to do it…

This section contains the steps required to follow the recipe. We show the PowerShell code you use to perform each step. With the exception of the Containers' chapter, all the recipes can be used and run in the PowerShell ISE. In the container's chapter, you should use the PowerShell console to run the recipe steps.

How it works…

This section contains a detailed explanation of what happened in the previous section along with screen shots to show you the results of the recipe.

There's more…

This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.

See also

In some recipes, this section provides some pointers to further information you may find useful.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at .

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book we would be grateful if you would report this to us. Please visit, http://www.packt.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit http://authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packt.com.