34,79 €
Enterprise PowerShell Scripting Bootcamp explains how to create your own repeatable PowerShell scripting framework. This framework contains script logging methodologies, answer file interactions, and string encryption and decryption strategies.
This book focuses on evaluating individual components to identify the system’s function, role, and unique characteristics. To do this, you will leverage built-in CMDlets and Windows Management Instrumentation (WMI) to explore Windows services, Windows processes, Windows features, scheduled tasks, and disk statistics. You will also create custom functions to perform a deep search for specific strings in files and evaluate installed software through executable properties.
We will then discuss different scripting techniques to improve the efficiency of scripts. By leveraging several small changes to your code, you can increase the execution performance by over 130%.
By the end of this book, you will be able to tie all of the concepts together in a PowerShell-based Windows server scanning script. This discovery script will be able to scan a Windows server to identify a multitude of components.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 291
Veröffentlichungsjahr: 2017
Copyright © 2017Packt 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: May2017
Production reference: 1160517
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B32PB, UK.
ISBN 978-1-78728-828-7
www.packtpub.com
Author
Brenton J.W. Blawat
Reviewer
Tim Amico
Commissioning Editor
Kartikey Pandey
Acquisition Editor
Namrata Patil
Content Development Editor
Amrita Noronha
Technical Editor
Jovita Alva
Copy Editors
Safis Editing
Laxmi Subramanian
Project Coordinator
Shweta H Birwatkar
Proofreader
Safis Editing
Indexer
Tejal Daruwale Soni
Graphics
Tania Dutta
Production Coordinator
Nilesh Mohite
Cover Work
Nilesh Mohite
Brenton J.W. Blawat is an entrepreneur, strategic technical advisor, multi-published author, and enterprise architect who has a passion for the procurement of technology in profit-based organizations. Brenton is business-centric, while technology minded, and has many years of experience bridging the gap between technical staff and decision makers in organizations. He takes pride in his ability to effectively communicate to a diverse audience and provide strategic direction for large and small organizations alike.
Since 2013, he has authored and published multiple books with Packt Publishing, including:
Brenton currently works at CDW as an enterprise architect in Strategic Solutions and Services. CDW is a leading multibrand technology solutions provider to business, government, education, and healthcare organizations in the United States, Canada, and the United Kingdom. A Fortune 500 company with multinational capabilities, CDW was founded in 1984 and employs approximately 8,500 workers. For the year ending December 31, 2016, the company generated net sales of nearly $14 billion. For more information about CDW, visit www.CDW.com.
As the author of this book, Brenton always extends himself to the PowerShell community. You can follow Brenton on Twitter at @brentblawat or his blog at http://www.masteringposh.com. The author is always open for discussions on the book and will provide feedback to readers, as time allows. If you are an academic institution, Brenton engages with multiple colleges for guest speaking. You can contact Brenton on Twitter for more information.
Happy coding!
Tim Amico is a passionate technology leader with years of consulting experience around designing, implementing, and managing Endpoint and Mobility management infrastructures for small organizations to large global enterprise environments. He has continued to define technology solutions that help solve complex business problems and deliver highly customized solutions to clients.
He currently works at LMI as a systems engineer, managing their System Center Configuration Manager, Active Directory, and Azure IaaS environments. LMI is a not-for-profit government consulting firm that designs and implements solutions to some of the toughest problems facing government managers in logistics, information technology, and resource allocation. For more information about LMI, visit www.lmi.org.
You can follow Tim on twitter at @tim_amico or his blog at http://www.deploymentlife.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.
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/1787288285.
If you'd like to join our team of regular reviewers, you can e-mail 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!
I would like to dedicate this book to my wife, Rachel, whose love and support drives me to be a better person every day.
PowerShell is quickly becoming the language of choice to support Microsoft systems in organizations. With Microsoft's deep integration of PowerShell in all of their software and cloud services, scripting knowledge will be a required skill set in the years to come. Even third-party vendors are creating modules or plugins to extend the PowerShell manageability of their systems. As a result, organizations are relying on their cross-platform engineers to automate systems through the use of PowerShell.
When you are implementing PowerShell scripts in enterprise environments, there are many common challenges that need to be solved. Robust logging functions, for example, are typically required to ensure that the script ran successfully and as intended. You may also need to implement a string encryption strategy to encrypt credentials, server host names, IP addresses, or other sensitive data. Further more, script execution speed may also take some consideration when processing large sets of data. You will want to ensure you are leveraging the fastest coding methods possible to optimize your scripting performance.
All of these challenges, and many more, can be solved by leveraging PowerShell. Through both native cmdlets and the .NET Framework, you can create sophisticated scripts to solve almost any problem. The PowerShell community of developers is very passionate about sharing their knowledge and providing information on how to solve problems. Through code sharing websites, social media, and Microsoft's TechNet, there is always a constant stream of examples that you can implement in your code. Also, now that PowerShell is open source and available on Linux, it deepens the relationships with the open source community. It provides a much wider set of open source developers to solve enterprise problems.
This book has been designed to teach you the ins and outs of enterprise PowerShell scripting. It is geared towards real-world scenarios of how Fortune 500 companies leverage PowerShell to solve problems. It provides a scripting framework that you can easily follow to start integrating the examples into your own scripts. At the end of this book, you will have learned many techniques to secure, optimize, and deliver PowerShell scripts that are ready to be deployed in the enterprise.
Chapter 1, Getting Started with Enterprise PowerShell Scripting, introduces the Windows server scanning script and performance considerations, and helps you create your own PowerShell scripting templates.
Chapter 2, Script Structure, Comment Blocks, and Script Logging, explores how to structure your PowerShell scripts, how to properly comment your script, and how to create a robust logging function.
Chapter 3, Working with Answer Files, explains the purpose of XML-based answer files and displays how to leverage them in your scripts.
Chapter 4, String Encryption and Decryption, dives into advanced string encryption and encoding methodologies. It shows you how to securely store encrypted data in scripts, and a how to decrypt and decode values during runtime.
Chapter 5, Interacting with Services, Processes, Profiles, and Logged-on Users, evaluates different Windows server components for nonstandard configurations. It also evaluates logged-on users and user profiles to identify what users have been interacting with a particular system.
Chapter 6, Evaluating Scheduled Tasks, displays how to interact with scheduled tasks on a system. It helps identify the different processes that may be invoked on a scheduled basis on a system.
Chapter 7, Determining Disk Statistics, explores how to interact with the disks on a system to retrieve and record disk information.
Chapter 8, Windows Features and Installed Software Detection, explains how to review the installed Windows features and installed software on a system.
Chapter 9, File Scanning, dives into scanning for file types in directories on a server. It also displays the process to scan for strings in files.
Chapter 10, Optimizing Script Execution Speed, evaluates how to increase server performance through the use of different scripting techniques. It also evaluates how to measure command execution time for benchmarking script performance.
Chapter 11, Improving Performance Using Regular Expressions, displays how to leverage regular expressions to significantly increase the performance of your PowerShell scripts.
Chapter 12, Overall Script Workflow, Termination Files, and Merging Data Results, evaluates the overall Windows server scanning script workflow and the full answer file creation. It also explores when and why you would use termination files and how to merge the results from the Windows server scanning script.
Chapter 13, Creating the Windows Server Scanning Script and Post Execution Cleanup, provides an in-depth explanation of all of the components in the Windows server scanning script. It also provides cleanup considerations for postscript execution on systems.
To work through the examples provided in Enterprise PowerShell Scripting Bootcamp, you will need access to two Server 2012 R2, or greater, Windows Server operating systems. Preferably, both systems will be joined to a domain. The chapters in this book highly rely on Windows Management Framework and it is recommended to leverage version 5.0 for PowerShell 5.0. You will need to download and install Windows Management Framework on the systems you are running these examples on.
Enterprise PowerShell Scripting Bootcamp has been designed for PowerShell scripters that are both beginner- and advanced-level coders. After reading this book, you will have in-depth knowledge of PowerShell scripting in enterprises and will create a working script to execute in your environment. Previous scripting and basic coding experience is required to understand the concepts presented in this book.
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to <[email protected]>, and mention the book title via 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 on www.packtpub.com/authors.
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.
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
You can download the code files by following these steps:
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Enterprise-PowerShell-Scripting-Bootcamp. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
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/EnterprisePowerShellScriptingBootcamp_ColorImages.pdf.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would 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 erratasubmissionform link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy of copyright 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.
You can contact us at <[email protected]> if you are having a problem with any aspect of the book, and we will do our best to address it.
The PowerShell language was developed to provide engineers with a method to quickly automate the provisioning and management of environments. In the last decade, PowerShell's use has greatly expanded, to encompass the setup and configuration of a myriad of hardware and software systems. Since third-party manufacturers are creating PowerShell modules to control their systems, PowerShell is becoming the automation language of choice. It is being used to configure multiple systems from multiple manufacturers, with a single set of PowerShell code.
Creating scripts for enterprise environments requires a robust scripting framework. A scripting framework consists of reusable code that you can leverage for a large majority of your scripts. This framework needs to be efficient, reliable, and secure. It must also provide logging capabilities, leverage answer files, and be flexible for execution on a wide variety of systems.
As you are exploring this book, you will learn a large number of tested and industry-proven scripting techniques. The examples you create will be applied at the end of the book in a Windows server scanning script. This will enable you to quickly integrate new sections of code into your own scripts, and will provide a trusted platform you can confidently deploy in your environment.
In this chapter, you will:
There can be times in large enterprises where you need to determine the functionality of systems. If you don't have large asset management software, you will need the ability to perform a discovery. This book was designed to provide a significant jumpstart for the creation of a Windows server scanning script. The script takes into consideration the limitations of Windows components and provides extremely detailed information about systems.
Some of the items that you will be scanning for include:
This script is designed to provide a robust platform to scan your enterprise. Variations of this script have been used in multiple Fortune 500 companies and executed on well over 10,000 systems. The chapters in this book have been designed to enable you to quickly learn the core components to integrate this PowerShell tool into your personal repository.
This book dives into important performance considerations for your scripts. Since PowerShell is an extremely flexible language, you can complete the same activity in multiple ways. While both sets of code get the script to create the same results, there are ways to optimize your code to make it much more efficient. This becomes increasingly important as you evaluate large amounts of data.
Execution time for file scanning, for example, exponentially grows with the quantity of files and the file sizes. 100 files with 100 lines of code can quickly create 10,000 evaluations. Now, if you scale that to 6 million files in a storage volume times 100 lines of code, you are performing 600 million evaluations. Small code performance improvements can significantly optimize your scanning of scripts at scale.
Some of the performance considerations include:
As you develop different scripts for your enterprise environment, you will have a core set of functions that you tend to use in all of your scripts. Some enterprise scripters create their own functions, while others leverage scripts from the vast community of PowerShell developers. With either method, you will need a solid foundation to build your scripts on.
Some of the most common items this book provides for scripting templates include:
Whether you are new to PowerShell scripting or you have been scripting for a long time, this book provides thought-inspiring methods to improve your scripting abilities. As you work through this book, it's strongly encouraged to incorporate these examples in your own scripts. This applied learning methodology will significantly help in your retention of the content, and provide a very robust enterprise scripting platform.
In this chapter, you explored the different components that will be implemented in the Windows server scanning script. You continued to learn about the different performance optimizations that you will be implementing in the book. Finally, you reviewed the individual components that should be included in most of your PowerShell Scripts.
In the next chapter, you will begin your journey by learning script structure, comment blocks, and script logging techniques.
The Windows server scanning script requires three core components to make the script suitable for enterprise environments. These components include developing a strict scripting structure, creating the comment block, and developing a flexible logging solution. Strict scripting structures ensure that all your scripts are created in the same order. This means that each section of the code is defined in the same place in all your scripts. A uniform structure helps others understand your code quickly and enables them to follow a standardized format for changes.
You will also learn how to create a comment block at the beginning of the script. The comment block ensures that you fully document the purpose, the requirements, the bug fixes, and change history for the script. It also provides details on how to execute the script leveraging command line parameters.
The final core component discussed in this chapter is logging mechanisms. Logging mechanisms provide the ability to record information about what is being performed during the execution of the script. This information is typically written to log files, event logs, and data collection files.
In this chapter, you will:
The examples in this chapter build upon each other. You will need to execute the script sequentially to have the final script in this chapter work properly.
When you are creating scripts, you should adhere to a strict script structure. The structure of the script dictates the order of execution and how things are processed. The following diagram displays the proper method for organizing your script:
It is recommended that you structure your scripts as follows:
The first recommendation is to create comment blocks for detailed tracking information about the PowerShell script itself. Comment blocks can track information about the script's creation, authors, changes, and other useful information that will enable you to quickly determine what the script is doing. PowerShell has built-in comment block support which integrates with the get-help cmdlet.
The required components include the following:
The following screenshot displays a proper comment block that will integrate with the help system:
This properly shows the syntax of a comment block that is usable via the PowerShell help system. You start by first defining the commend block with <#. You then create a .SYNOPSIS section with a synopsis of the script. Then you create a .DESCRIPTION section and provide a detailed description of the script. You also provide details regarding the author, revision, and editing revisions to the script. Following that section, you define a .PARAMETER PATH section, which provides detailed information about the .PARAMETERPATH in the script. Next, you create an .EXAMPLE section, to provide an example for usage, and you create a .NOTES section to provide information about the required execution environment. You then close the comment block by issuing #>.