Data Loading...

9781787287242 Flipbook PDF


138 Views
65 Downloads
FLIP PDF 1.8MB

DOWNLOAD FLIP

REPORT DMCA

James Pogran

Learning PowerShell DSC Second Edition

Automate deployment and configuration of your servers

FOR SALE IN INDIA ONLY

Learning PowerShell DSC Second Edition

Automate deployment and configuration of your servers

James Pogran

BIRMINGHAM - MUMBAI

Learning PowerShell DSC Second Edition Copyright © 2017 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: October 2015 Second edition: September 2017 Production reference: 1060917 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.

ISBN 978-1-78728-724-2 www.packtpub.com

Credits Author James Pogran

Copy Editor Stuti Shrivastava

Reviewers Mark Andrews Steve Parankewich

Project Coordinator Virginia Dias

Commissioning Editor Vijin Boricha

Proofreader Safis Editing

Acquisition Editor Prateek Bharadwaj

Indexer Aishwarya Gangawane

Content Development Editor Sharon Raj

Graphics Kirk D'Penha

Technical Editor Vishal Kamal Mewada

Production Coordinator Aparna Bhagat

About the Author James Pogran has been working with computers in one way or another for over 15 years. His first job involved systems administration for a large military installation. He then moved on to develop monitoring software and automating large-scale Windows environments for a major managed services provider. He is currently a senior software engineer at Puppet, Inc, where he helps make Windows automation even better with Puppet. I would like to express my gratitude to the many people, more than I can name here, who saw this book through with me. To my wife, Jessica, without whose support and confidence in me, I would not have attempted so many things in my life. For now, for always, and forever. To my children: Ethan, Charlotte, and Amelia, who are my constant inspiration and my constant joy. To my dad, who has seen me through many things in life and will see me through much more. In memory of my mother, who taught me so many things in life; may I continue to learn from her example.

About the Reviewers Mark Andrews's career in technology has been a varied one; over the last 20 years, he has held several different positions, ranging from customer service to quality assurance. Throughout all of these positions, the responsibility of configuration management and build management has always fallen either on Mark personally or on one of the groups that he managed; because of his “keeping a hand in” management style, he has been closely involved with the scripting and automation framework for this area. Creating scripted frameworks that intercommunicate across machine/operating system/domain boundaries is a passion for him. PowerShell 3.0 Advanced Administration Handbook Windows PowerShell 4.0 for .NET Developers PowerShell for SQL Server Essentials Microsoft Exchange Server PowerShell Essentials Microsoft Exchange Server PowerShell Cookbook (Third Edition) AWS Tools for PowerShell 6

Steve Parankewich is a professional systems analyst, architect, and engineer. With over 20 years of experience, Steve has always had a passion for automation. He is currently a PowerShell evangelist and leads the Boston PowerShell user group, organizing monthly meetups with fellow IT professionals. Steve currently focuses on implementation and migration to both Office 365 and Azure. You can reach him or read additional PowerShell based articles that he has written at powershellblogger.com.

www.PacktPub.com For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com, and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

https://www.packtpub.com/mapt

Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.

Why subscribe? Fully searchable across every book published by Packt Copy and paste, print, and bookmark content On demand and accessible via a web browser

Customer Feedback Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://www.amazon.com/dp/1787287246. If you'd like to join our team of regular reviewers, you can email us at [email protected]. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!

Table of Contents Preface Chapter 1: Introducing PowerShell DSC What is PowerShell DSC? What is PowerShell? On to PowerShell DSC Why do we need configuration management? What is DevOps? DSC quick wins DSC high-level overview The authoring phase The staging phase The execution phase Why all the abstraction? How does DSC help? The who The what The how Idempotence Isn't this Group Policy or SCCM? DSC features DSC requirements DSC versions PowerShell V4 DSC V4 DSC language extensions V4 DSC base resources V4 DSC cmdlets V4 DSC pull server PowerShell V5 DSC V5 DSC language extensions V5 DSC base resources V5 DSC cmdlets V5 improvements The PowerShell ISE Partial configurations and dependencies Class-based DSC resources

1 7 8 8 10 12 13 14 15 15 15 16 16 17 18 18 19 19 20 21 21 23 23 24 24 26 27 28 28 28 28 29 29 29 30

DSC built-in support for help DSC run as credential support DSC resource side-by-side installation DSC resource script debugging Separation of node and configuration IDs DSC LCM MetaConfig updates DSC LCM rich state information DSC LCM RefreshMode values DSC status from a central location

PowerShell V6 DSC Summary

Chapter 2: DSC Architecture

30 31 31 31 31 32 32 32 32 33 33 35

Overview Push and pull modes The push management model The pull management model The general workflow Authoring Syntax Compilation

Staging Execution Push executions Pull executions

The example workflow Configuration data Hardcoded data Parameter-based data Hashtable data Local Configuration Manager LCM settings Configuration modes ApplyOnly ApplyAndMonitor ApplyAndAutoCorrect Refresh modes

The LCM configuration An example LCM configuration The DSC pull server What is a DSC pull server? What does the pull server do for us? Automatic and continuous configuration

[]

36 37 38 40 41 42 42 43 44 45 46 46 46 50 51 51 52 53 54 55 56 56 56 56 57 57 59 60 60 60

Repository Reporting Security

Deployment considerations General observations LCM gotchas Deployment mode differences Summary

61 61 61 62 62 62 64 65 68 68 69 70 70

Chapter 3: DSC Configuration Files

71

Setting up a DSC pull server Pull server settings Installing the DSC server Adding MOF files to a pull server Adding DSC resources to a pull server

Defining a DSC configuration script file Authoring DSC configuration files DSC automatic variables AllNodes Node ConfigurationData

DSC resource import declarations DSC configuration syntax The configuration keyword The Node keyword DSC resource statements DSC configuration script file example

Defining a DSC configuration data file Authoring DSC configuration data files Configuration data syntax The variable syntax The data file syntax

Allowable DSC configuration content Creating reusable DSC configurations Nested DSC configurations Nested DSC configuration syntax Nested DSC configuration limitations

DSC composite resources Why use DSC composite resources? The DSC composite resource syntax The DSC composite resource folder structure DSC composite resource drawbacks

Partial DSC configurations Debugging and troubleshooting configuration script files

[]

71 72 74 74 75 76 78 79 82 83 85 86 88 89 89 91 92 94 95 96 96 98 99 99 103 104 106 107 108

Status at a glance Using DSC event logs Enabling verbose logging What do DSC event logs contain? Gathering events from a single DSC operation Event Viewer PowerShell

Using the xDscDiagnostics module to analyze DSC logs Get-xDSCOperation Trace-xDSCOperation

Resetting the DSC engine cache Enabling the debug mode Fixing a stuck DSC run Summary

Chapter 4: DSC Resources

109 110 110 110 111 111 111 112 113 113 113 114 114 115 117

What are DSC resources? What makes a good DSC resource Idempotent Do one thing well Reuse code, but don't go overboard Contribute back

Creating PowerShell MOF-based custom DSC resources MOF-based DSC resource folder structure MOF-based DSC resource syntax The DSC resource definition file Naming Versioning Parameters Qualifiers The DSC PowerShell module file Get-TargetResource Test-TargetResource Set-TargetResource Authoring custom DSC resources Creating DSC resources manually Creating DSC resources automatically

Creating PowerShell class-based custom DSC resources PowerShell classes Class-based DSC resources The folder structure of class-based DSC resource Class-based DSC resource syntax Advantages of a class-based DSC resource Disadvantages of a class-based DSC resource

Creating single-instance custom DSC resources

[]

117 119 119 119 120 120 121 121 123 124 124 125 125 126 126 127 127 128 128 128 129 133 134 135 135 136 139 140 140

Creating PowerShell custom DSC resources in C# Create the MOF schema Create the Visual Studio project Create the C# cmdlet code Packaging the C# custom DSC resource

Testing custom DSC resources Using xDscResourceDesigner Pester Microsoft recommendations

How do you find DSC resources? Microsoft DSC resource Kits Installing DSC resource Kits Microsoft PowerShell DSC GitHub repository Installing DSC resources from GitHub Microsoft PowerShell Gallery PackageManagement Discover DSC resources in the PowerShell Gallery Installing DSC resources in the PowerShell Gallery Custom hosting options Local SMB/network share NuGet IIS Third-party NuGet servers

Deploying DSC resources Deploying DSC resources in a push DSC configuration deployment Deploying DSC resources in a pull DSC configuration deployment

Summary

Chapter 5: Pushing DSC Configurations Tooling Setting things up Test environment Locally pushing DSC configurations Setting up the test environment locally Compiling configurations for local target nodes Executing configurations for local target nodes Remotely pushing DSC configurations Setting up the test environment remotely Compiling configurations for remote target nodes Executing configurations for remote target nodes Things you must consider when pushing DSC configurations Summary

Chapter 6: Pulling DSC Configurations Creating DSC pull servers

142 142 143 143 145 146 146 147 148 148 149 150 150 152 152 153 154 155 157 157 159 159 159 160 160 160 161 162 162 163 164 164 165 167 168 169 171 172 174 176 177 178

[]

DSC pull server setup considerations DSC pull server type The Windows management framework version

Initial setup tasks Installing required DSC resources SSL certificates SMB share creation Preparing DSC resources for pull server distribution Miscellaneous tasks

Creating an SMB DSC pull server Creating an HTTPS DSC pull server DSC pull server and registration keys Validating a DSC pull server install Testing using a web browser Testing using the command line Registering target nodes with a DSC pull server Registering a target node using a configuration ID Registering a target node using RegistrationKey Pulling DSC configurations with a DSC pull server Compiling DSC configurations using ConfigurationIDs Compiling DSC configurations using RegistrationKeys Deploying DSC Configurations to the DSC pull server DSC pull server and target node status Checking the current LCM configuration status Triggering a DSC configuration to run on a target node Triggering a WMF 4 target node Triggering a WMF 5 target node

Using DSC logging to check on the status Reporting on the target node status Reporting on the target node status with the DSC pull server WMF 5 pull server reporting WMF 4 pull server reporting

Summary

Chapter 7: DSC Cross Platform Support DSC cross-platform support Using DSC on Linux Installing DSC on Linux Pushing a DSC configuration to a Linux node

PowerShell Core Installing PowerShell Core on Windows Installing PowerShell Core on macOS Homebrew

[]

179 179 180 180 180 183 185 185 186 187 189 193 195 195 196 197 198 199 202 203 203 204 207 207 209 209 209 210 215 216 216 218 220 221 221 222 222 223 226 227 227 227

Manual Installing PowerShell Core on Linux Ubuntu

Summary

Chapter 8: Example Scenarios

228 228 228 229 231

Real-life DSC usage Setting up a common installation base Installing software Configuration management refresher Complicated deployments Handling change Summary

Index

232 233 235 238 241 245 248 249

[]

Learning PowerShell DSC - Second Edition The main goal of this book is to teach you how to configure, deploy, and manage your system using the new features of PowerShell v5.1 Desired State Configuration (DSC). This book begins with the basics of PowerShell DSC, covering its architecture and components. It familiarizes you with the set of Windows PowerShell language extensions and new Windows PowerShell commands that make up DSC. Then it helps you create DSC custom resources and work with DSC configurations with the help of practical examples. Finally, it describes how to deploy configuration data using PowerShell DSC. Throughout this book, we will be focusing on concepts such as building configurations with parameters, the local configuration manager, and testing and restoring configurations using PowerShell DSC. By the end of the book, you will be able to deploy a real-world application end-to-end and will be familiar enough with the powerful DSC platform to achieve continuous delivery and efficiently and easily manage and deploy data for systems.

www.packtpub.com

Things you will learn:

• Explore PowerShell DSC and activities around it, including the need for configuration management and abstraction

• Create reusable DSC configurations and debug/ troubleshoot configuration files

• Learn about the PowerShell DSC architecture with the help of push-and-pull management and workflows

• Define DSC configuration scripts and data files and push DSC configuration files remotely and locally

• Validate DSC pull server installation and register target nodes with a DSC pull server

• Learn about DSC cross platform and install PowerShell on Linux and macOS, along with real-life DSC uses and different types of deployment

FOR SALE IN INDIA ONLY