Book Image

Windows Server 2016 Automation with PowerShell Cookbook - Second Edition

By : Thomas Lee, Ed Goad
Book Image

Windows Server 2016 Automation with PowerShell Cookbook - Second Edition

By: Thomas Lee, Ed Goad

Overview of this book

This book showcases several ways that Windows administrators can use to automate and streamline their job. You'll start with the PowerShell and Windows Server fundamentals, where you'll become well versed with PowerShell and Windows Server features. In the next module, Core Windows Server 2016, you'll implement Nano Server, manage Windows updates, and implement troubleshooting and server inventories. You'll then move on to the Networking module, where you'll manage Windows network services and network shares. The last module covers Azure and DSC, where you will use Azure on PowerShell and DSC to easily maintain Windows servers.
Table of Contents (21 chapters)
Title Page
Credits
About the Author
Acknowledgment
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Preface

PowerShell was first introduced to the world at the Professional Developer's conference in Los Angles 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 of managing large number of Windows systems. A key takeaway—the GUI does not scale, whereas PowerShell does.

PowerShell has transformed managing 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 IT Pro job—the adage being 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 2016. This preface provides you with an introduction to what is in the book and some tips on how to get the most out of the content.

What this book covers

Chapter 1, What's New in PowerShell and Windows Server, looks at some of the key new features in Windows Server 2016 and in the latest version of PowerShell.

Chapter 2, Implementing Nano Server, shows you how to set up and use Nano Server—a new server installation option for Windows Server 2016. Nano Server provides a great platform for running roles with a vastly reduced attack and patch surface.

Chapter 3, Managing Windows Updates, helps you get to grips with managing updates via Windows Update. With the importance of keeping all your Windows servers fully patched, managing WSUS is a key take in almost any size organization.

Chapter 4, Managing Printers, shows you how to manage printers, printer queues, and printer drivers, including deploying printers via Group Policy. This chapter also looks at branch office printing.

Chapter 5, Managing Server Backup, examines the use of Windows Server Backup and covers both backup (and restore) via cmdlet and via the console application. Windows Server Backup is a nice feature you can use either on its own in a small organization or to augment an enterprise wide third-party service.

Chapter 6, Managing Performance, shows you how to measure and monitor the performance of a server. There are several recipes that demonstrate how to get specific performance measurements and how to create graphs of performance for further analysis.

Chapter 7Troubleshooting Windows Server 2016, looks at a number of aspects of both reactive and proactive troubleshooting. This includes getting events from the event log and forwarding event logs to a central server. This chapter also looks at the Best Practice Analyzer features baked into Windows Server.

Chapter 8, Managing Windows networking services, looks at various aspects of networking. 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, as well as building a multi-tier certificate authority infrastructure.

Chapter 9Managing Network Shares, looks at sharing data between systems, including building a scaled out clustered file server based on iSCSI and using the DFS Namespace and DFS Replication features of Windows Server.

Chapter 10Managing 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 11Managing 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 12Managing Azure, looks at managing IaaS resources in Azure using PowerShell. To test the recipes in this chapter, you need access to Azure. This chapter describes how to get a trial subscription.

Chapter 13Using 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.

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 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 Getting the most from this book.

Ideally, you should have a Windows 10 or Windows Server 2016 host with virtualization capabilities and use a virtualization solution. If you have access to a cloud computing platform, then you could perform most of the recipies in cloud-hosted virtual machines although that has not been tested. You can use any virtualization.

The book was developed using Hyper-V and nested Hyper-V on Windows 10 Creator's Update and Windows Server 2016. More details of the servers are contained in the preface and each recipe.

Who this book is for

This book is aimed at IT Pros, including system administrators, system engineers, as well as architects and consultants who need to leverage 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 will need to 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.

  1. 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 will need to know the basics of PowerShell. The book uses PowerShell language, syntax, and cmdlets that come with Windows Server 2016 and Windows 10 (CU).
  1. The recipes provide the basics—you adopt and adapt. The recipes are designed to show you the basics of how to manage certain aspects of Windows Server 2016 using PowerShell (and in some cases Windows Console Applications). In many cases, a recipe stresses that you can improve it for your environment. The recipe is meant to show you how some features work, so you can leverage and extend it for your environment.
  2. Start by running the recipes step by step. The recipes were built and tested step by step. Once you have it working, re-factor them into your own reusable functions. In some cases, we build simple functions as a guide to richer scripts you could build.
  3. Writing PowerShell scripts for publication in a book is a layout nightmare. To get around this, I have made extensive use of the various ways in which you can create multiline commands within PowerShell. This involves using the back tick (`) line continuation as well as using the Pipe character at the end of the line. I also sometimes specify an array of values across multiple lines with a comma at the end of the continuing line. Hopefully, the screenshots more or less match up. So, read the text carefully and pay attention particularly to the back tick. In all too many places and to save lots of extra blank space, code spills over a page break, or where a figure and related text are split across a page boundary. I hope there are not too many issues with layout!
  1. Many of the cmdlet or object methods used in this book produce output that may not be all that helpful or useful. In some cases, the output generates a lot of pages of little value. For this reason, many recipes pipe to Out-Null. Feel free to remove this where you want to see more details. In some cases, I have adjusted the output 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. Of course, the specific output you see may be different based on your environment and the specific values you use in each step.
  2. To write this book, I have used a large VM farm consisting of over 20 Windows 2016 servers and Windows 10 clients. All the hosts used in this book are a combination of some physical hardware (running almost entirely on Windows 10 Creators Update and a large set of VMs, including the following:
    • Domain Controllers (DC1DC2)—also hosts DHCP Server, IIS, and other roles).
    • File Servers (FS1FS1)
    • Network Load Balanced IIS servers (NLB1NLB2)
    • Print Server (PSrv)
    • General purpose servers (SRV1SRV2)
    • Client computers (CL1SG-BR-CL1)
    • Certificate servers (rootCA)
    • Hyper-V Servers (HV1HV1), including an embedded VM, VM1.

Each recipe notes the servers in use. Feel free to change things to suit your needs and based on your own naming conventions.

  1. In building the VM farm, I have used an IP address block of 10.10.10.0/24. The recipes show specific addresses in use, but you can adapt these to fit your environment. The IP addresses used are assigned as follows:

IP address

Server name

10.10.10.10

DC1 (DC, DHCP, DNS, IIS, and so on)

10.10.10.11

DC2 (DC, DHCP, and DNS)

10.10.10.20

Root (CA offline root)

10.10.10.21

CA.Reskit.Org—issuing CA

10.10.10.50

SRV1 (server with numerous roles)

10.10.10.51

10.10.10.55

SRV2 (server with numerous roles)

ReskitNLB (NLB Virtual Server)

10.10.10.60

PSRV (print server)

10.10.10.61

Sales.Reskit.Org—a network printer

10.10.10.62

Sales2.reskit.org—a printer at as remote office

10.10.10.100

FS.Reskit.Org (Cluster address)

10.10.10.101/102

FS1 (file server cluster node—with 2 nics)

10.10.10.105/106

FS2 (file server cluster node—with w nics)

10.10.10.131

Nano1

10.10.10.132

Nano2

10.10.10.141

SG-CL1 (client computer in the Sales Group)

10.10.10.146

SG-BR-CL1 (sales group branch office client)

10.10.10.201

HV1 (Hyper-V server)

10.10.10.202

HV2 (Hyper-V server)

10.10.10.251

WSUS1 (WSUS Server)

10.10.10.254

Default gateway

 

The full set of VMs, at the end of this writing, took up around 725 GB of storage. Fortunately, storage is cheap!

  1. PowerShell provides great feature coverage—you can manage most of the functions and features of Windows Server 2016 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 will 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.
  2. I have avoided where possible using external, third-party modules and have focused on what comes in the box. But, in some cases, such as Azure, you have to add code and in other cases such as DSC you benefit greatly from third-party code. The book shows that there is a wealth of tools, add-ins, and tips/tricks that you can leverage (even if we do not use all that much of it). One thing to keep in mind, integrating various add-ons (and keeping them up to date and working well) can be a challenge.
  3. 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'll help. Feel free to post issues to the Spiceworks PowerShell forum for quick resolution.
  4. In writing this book, we set out to create content around a number of features of Windows Server 2016. 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. Coverage of Storage and Containers had to be dropped. To paraphrase Jeffrey Snover, 

    To ship is to choose. I hope I chose well

    .
  1. In writing the recipes, we use full cmdlet names with no aliases and with all parameter names spelled out in full (so, this means no abbreviated parameter names or positional parameters). This makes the text a bit longer, but hopefully easier to read and understand.
  2. Some recipes in this book rely on other recipes being completed. These related recipes worked well when we wrote them and hopefully will work for you as well. Each depending recipe is noted.
  3. 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 friends.

Sections

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

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

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.

Conventions

In this book, you find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, URLs, and so on are shown as follows:

"A great PowerShell cmdlet is Get-Help"

A block of code is set as follows:

If (-Not (Get-WindowsFeature -Name PowerShell)) 
 {
   'PowerShell Not installed'
 }

Any command-line input or output is written as follows (Note the back tick at the end of the second line):

#  Copy ISO image
 Copy-Item -Path c:\Image\Server2016.iSO `
           -TargetPath c:\VM\ISO\Server2016.ISO

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Clicking the Next button moves you to the next screen."

Note

Warnings or important notes appear in a box like this.

Note

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you can really get the most out of.

To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/WindowsServer2016AutomationwithPowerShellCookbookSecondEdition_ColorImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in this book-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission is accepted and the errata uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information appears under the Errata section.

If you find issues, feel free to reach out to the author via the Spiceworks forum.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we do our best to address the problem.

Help and assistance

If you want help on any of the recipes, or want to discover more information about any of the steps, come over to the PowerShell forum at Spiceworks. Navigate to: https://community.spiceworks.com/programming/powershell and ask away. Note you do need to register to be able to ask questions and participate.