39,59 €
If you are a system administrator who wants to become an expert in controlling and automating your Windows environment, then this book is for you. Prior knowledge of PowerShell's core elements and applications is required for this book.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 370
Veröffentlichungsjahr: 2015
Copyright © 2015 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: April 2015
Production reference: 1210415
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78217-355-7
www.packtpub.com
Author
Brenton J.W. Blawat
Reviewers
Tim Amico
Christophe CRÉMON
Tomas Restrepo
Acquisition Editor
Meeta Rajani
Content Development Editor
Rohit Singh
Technical Editor
Tanvi Bhatt
Copy Editors
Hiral Bhat
Sonia Mathur
Vikrant Phadke
Alpha Singh
Project Coordinator
Mary Alex
Proofreaders
Simran Bhogal
Safis Editing
Maria Gould
Paul Hindle
Indexer
Rekha Nair
Production Coordinator
Alwin Roy
Cover Work
Alwin Roy
We all appreciate a little simplicity when it enters our busy, complicated lives. Technology is no different. In fact, that is what we expect from technology—it should simplify our lives. But it doesn't always work that way.
Sometimes technology can introduce vexing problems we don't anticipate.
With every version of Microsoft DOS and then Windows, Microsoft included separate command-line interface shells. The shell could automate some tasks, but not all of them. Some tasks had to be accomplished manually, which is antithetical—nearly heretical—to our understanding of technology's role.
What should have been simple, automated tasks became slow, frustrating, and manual chores. That conundrum didn't go unnoticed.
Microsoft provided much-needed simplicity when it introduced PowerShell in 2006. Task automation and configuration management eliminated a great deal of time-consuming manual work. In short, PowerShell was a game changer.
PowerShell has solved many of the command-line and scripting issues that complicated our work. It introduced simplicity. It helped organizations become more agile, more productive, and save money. PowerShell is a powerful tool, and it has demonstrated its practical value many times.
Despite its utility, though, PowerShell remains a confusing tool to many IT administrators, and the need for greater clarity remains.
It is through this lens that Mr. Brenton Blawat provides an insightful new analysis of PowerShell—a valuable guidebook for those who struggle to understand PowerShell. Others have offered narratives that attempt to explain PowerShell, but they rarely provide the roadmap, background, or context that administrators need to get from Point A to Point B.
So, this is the right time for a comprehensive new analysis.
In general terms, Mr. Blawat's book will help those who struggle to manage their compute environments. It includes important guidance on programming in PowerShell, starting with basic concepts and then introducing advanced configurations.
Mr. Blawat deconstructs and demystifies this programming language, sharing his intimate knowledge in a format that demonstrates the clarity of thought and prose that a difficult subject requires. Many authors have attempted this climb, but only Mr. Blawat has reached the summit. His examination provides relevant information for administrators who work with—and sometimes struggle with—PowerShell on a daily basis.
As Mr. Blawat explains in the following pages, the rapid adoption of cloud-based technologies paralleled the never-ending need for additional computing power in data centers. This created the need to efficiently build and expand systems with extreme precision. In addition to provisioning the base systems, there was a need to dynamically customize these new systems to work in unison with current running environments.
This drove the need for the next generation system automation languages that would provide full configuration for systems on the fly. Not only do these automation languages greatly reduce time to use, but they also ensure that no mistakes are made during the configuration process. All systems are created equally.
PowerShell is a .NET-based next generation automation language that provides both systems provisioning and management functionality for Windows-based systems. Leveraging command-line like interactions, PowerShell can be compiled into scripts that can systematically execute tasks on a system. Not limited to creating new systems, engineers are leveraging PowerShell to automate mundane tasks so that they can focus on other pressing activities in their environments.
Microsoft has fully embraced PowerShell in its full software portfolio to offer full integration with its products. Not only can you dynamically install the Microsoft software, but you can also fully manage the entire Microsoft software environment using PowerShell. PowerShell has also been embraced by third-party manufacturers through the integration of PowerShell modules. These modules provide full management capabilities for products such as network devices, storage subsystems, virtualization guests and hosts, security appliances, and other third-party applications.
Like many organizations, CDW has benefitted greatly from PowerShell, so this book hits very close to home for me. We use PowerShell scripts to manage customer environments in managed services and the installation of management tools.
We use Microsoft Orchestrator to provide back-end logic for simple user interfaces for help desk activities, like user-driven password resets and user-driven software installations. We also rely on Microsoft Orchestrator to automatically troubleshoot and remediate systems.
CDW's customers also benefit from PowerShell in myriad ways. We assist a wide variety of Fortune 500 clients to develop PowerShell automation scripts to build new systems and manage their environments, including health check scripts, systems discovery, and advanced regulatory security analysis.
Clearly, PowerShell's incredible utility has made it invaluable to CDW, our customers, and countless organizations across the globe. But understanding all of its many facets represents a daunting task. Organizations are unable to leverage PowerShell if they don't fully understand its potential.
That's where Mr. Blawat excels.
Mr. Blawat's comprehensive new work will serve as a reference tool for engineers who work with Windows by taking the mystery out of common tasks that aren't easily understood and aren't always intuitive. Diligent readers will no doubt find many more reasons to give Mr. Blawat's thorough narrative a prominent place on their bookshelves.
As Mr. Blawat's coworker, I also feel proud that he is sharing his knowledge with the world—not only so others can benefit from his experience, but so they can see what those of us who work with him at CDW witness on a daily basis.
PowerShell has proven to be an incredibly useful tool for IT administrators since it burst on to the scene. Now there is a book about PowerShell that will prove equally useful.
Jon Stevens
Chief Information Officer
CDW
Brenton J.W. Blawat is an entrepreneur, strategic technical advisor, author, and senior consultant, who has a passion for the procurement of technology in profit-based organizations. He is business-centric and technology-minded. Brenton has many years of experience in bridging the gap between technical staff and decision-makers in several organizations. He takes pride in his ability to effectively communicate with a diverse audience and provide strategic direction for large and small organizations alike.
In 2013, Brenton authored his first book, PowerShell 3.0 WMI Starter, Packt Publishing. This book was designed to be a starter for those interested in manipulating Windows Management Instrumentation through the use of PowerShell 3.0. This book is available in all popular book stores, including Packt Publishing's website, http://bit.ly/18pcpGK.
Brenton currently works at CDW as a senior consulting engineer in strategic solutions and services. CDW is a leading multibrand technology solutions provider in the fields of business, government, education, and healthcare. A Fortune 500 company, it was founded in 1984 and employs approximately 7,200 coworkers. In 2014, the company generated net sales of more than $12.0 billion. For more information about CDW, you can visit www.CDW.com.
I would like to thank the foreword coordination team, Mary Viola, Scott Thomas, Jimmy Thomson, Brandon King, Sondra Ragusin, Meredith Braselman, and Bill Glanz. I would like to dedicate this book to my beautiful nieces, Caliett and Evie.
Tim Amico is a consulting engineer at CDW, who specializes in Configuration Manager and Operating System Deployment. He started his career in IT back in 1998, when he worked as a configuration tech for CompuCom, learning about what is required to become an IT professional. Since then, he has climbed the ladder from hardware field support and software deployment support to his current role (for the last 10 years) as an IT consultant, designing and implementing endpoint and mobility management solutions. Apart from Configuration Manager and OSD, Tim also has experience in PowerShell, BitLocker drive encryption deployments, Intel vPro provisioning and use case design, Active Directory (both in Azure and Windows Server), SQL design and reporting, PKI design, and Microsoft Intune.
Christophe CRÉMON is a SharePoint infrastructure architect, with 10 years of experience in information technology, especially Microsoft. He has been using PowerShell since 2008, and has published useful scripts and modules for IT Administrators at powershell.codeplex.com. He has a personal website at www.christophecremon.com.
Tomas Restrepo has been writing software for over 10 years, starting with C/C++ and eventually moving to the .NET platform. He currently spends most of his time helping other developers solve complex problems and troubleshooting application performance and scalability issues.
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://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.
Get notified! Find out when new books are published by following @PacktEnterprise on Twitter or the Packt Enterprise Facebook page.
PowerShell is a network scripting language that provides a set of tools to administer Microsoft products. While PowerShell is based on command-line interactions, it is much more powerful than what the standard command line offers. It has built-in sections of code called cmdlets. They simplify functions that you may need to perform on a system. Using cmdlets greatly reduces the number of lines of code that are required to perform actions, compared to other scripting languages, such as VBScript.
PowerShell is based on the verb-noun naming convention, which allows scripters to declare an action followed by an object to configure. For example, the get-service cmdlet easily designates that you are getting a Windows service. This literal naming convention helps readers quickly learn how to program in PowerShell, as the actions are easily remembered.
Community support for PowerShell has grown astronomically. Not only have large companies adopted PowerShell in their environments, but universities are also regularly teaching PowerShell courses to their students. PowerShell's feature set keeps growing with every release of the product. It is conceivable in the near future that you will be able to fully automate the configuration of every component in a data center. This will remove the needs of multiple engineering specialists to provision networking, storage, firewalls, operating systems builds, and high-availability configurations. It will all be done via PowerShell scripting and the systems will be able to be configured using a singular network language.
This book provides a strong foundation for learning PowerShell using real-world scenarios. You will not only be able to quickly learn how to program in this language, but also be able to produce scripts that you can use in your existing environments. This book will also be a great reference book for you to look back on and revisit as you are coding. It will provide the proper syntax and show you successful ways to implement your code. When you are done with reading this book, you will be well on your way to "mastering PowerShell"!
Chapter 1, Variables, Arrays, and Hashes, explores the different data and object containers that you can use in PowerShell. These containers include variables, arrays, and hashes. This chapter provides examples on how to use these containers to store objects.
Chapter 2, Data Parsing and Manipulation, dives into the different data types and how to manipulate them in your scripts. These data type examples include strings, integers, dates, XML, and many more.
Chapter 3, Comparison Operators, evaluates multiple comparison operators and displays how to use each of these comparison operators. This chapter also displays how to leverage implied true and false comparison operators.
Chapter 4, Functions, Switches, and Loops Structures, displays the use of different data structures to perform repeatable actions. It provides examples on how to parse large arrays of data through looping structures and how to include overload parameters in these structures.
Chapter 5, Regular Expressions, explores PowerShell's implementation of regular expressions. It evaluates the built-in comparison operators that provide expression validation and how to create complex expressions.
Chapter 6, Error and Exception Handling and Testing Code, shows you how to create code in a robust manner to avoid exceptions during execution. This chapter explains various built-in error and exception handling techniques, as well as support for legacy systems that don't support PowerShell cmdlet triggers. It also explains the different items to be aware of during the testing cycle of your code.
Chapter 7, Session-based Remote Management, provides an insight into session-based management through PowerShell. It displays how to leverage the built-in WinRM to execute items on remote systems.
Chapter 8, Managing Files, Folders, and Registry Items, displays how to query, create, modify, and delete items in the filesystem and registry. This includes files, folders, registry keys, registry-named values, and properties.
Chapter 9, File, Folder, and Registry Attributes, ACLs, and Properties, dives deep into the interworking of files, folders, and registries. This chapter explains how to set file and folder standards and advanced attributes. It also displays how to manipulate ACLs to set permissions on files, folders, and registry items.
Chapter 10, Windows Management Instrumentation, explains how to use Windows Management Instrumentation (WMI) to query local and remote systems for advanced system information and the different cmdlets that provide access to a system's WMI.
Chapter 11, XML Manipulation, explores eXtensible Markup Language (XML) and shows you how to interact with it using PowerShell. This chapter explains the different components that make up a proper XML document and how to interact with these individual components.
Chapter 12, Managing Microsoft Systems with Powershell, provides information on how to work with Windows users and groups, Windows services, Windows processes, and the manipulation of Windows features and roles.
Chapter 13, Automation of the Environment, explains how to invoke items for use with automation scripts. This chapter explains parent and child relationships because they pertain to linking scripts together. It also explores Desired Configuration Management (DCM) and configuration baselines.
Chapter 14, Script Creation Best Practices and Conclusion, provides best practice recommendations for utilizing PowerShell in your environment. This chapter concludes with some final thoughts from the author.
To work through the examples provided in Mastering Windows PowerShell Scripting, you will need access to Windows 7 or a higher Windows operating system. You will also need Server 2008 R2 or a higher Windows Server operating system. The chapters in this book rely highly on Windows Management Framework 4.0 (PowerShell 4.0) and Remote Server Administration Tools. You will need to download and install both of these software packages on the systems you are running these examples on.
Mastering Windows PowerShell Scripting has been designed for PowerShell scripters who can be both beginners and advanced-level coders. By reading this book, you will be able to gain in-depth knowledge of PowerShell and the best practices to develop scripts using this automation language. Previous scripting and coding experience will be helpful, though it is not required.
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.
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.
When you are scripting, it is important to put consideration into what kind of container you will be using. Sometimes the simplicity of creating a singular variable and updating that variable is less complex than creating an array or hash table to search through. At other times, it may be more efficient to pull the whole dataset and use individual pieces of that data within your script.
Single-line variables can be used for:
Arrays are best used for:
Hashes are best used for:
This chapter explores the use of a variety of containers. You learned that variables, arrays, and hashes have the commonality of being able to store data, but they do it in different ways. You learned that different types of data can be stored in these containers. These types of data include numbers, strings, and objects.
This chapter explored that variables are best used for the storage of single-dimensional datasets. These datasets can contain strings but also include mathematical equations that PowerShell has the ability to inherently calculate. You also now know that arrays are primarily used in situations where you want to store more than one set of data. You are able to navigate, add, and remove values in the array based off of a starting value of 0. Last, you learned that hashes are best used while correlating data from one value to another. You are able to add, remove, and search data contained in the hash tables with the use of simple commands. In the next chapter, you will learn techniques to perform data parsing and manipulation by leveraging variables and arrays.