Book Image

Mastering Windows PowerShell Scripting

By : Brenton J.W. Blawat
Book Image

Mastering Windows PowerShell Scripting

By: Brenton J.W. Blawat

Overview of this book

Table of Contents (22 chapters)
Mastering Windows PowerShell Scripting
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

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"!

What this book covers

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.

What you need for this book

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.

Who this book is for

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.

Conventions

In this book, you will find a number of styles of text 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, dummy URLs, user input, and Twitter handles are shown as follows: "The get-service cmdlet is used to retrieve detailed information about Windows services."

Any command-line input or output is written as follows:

# Retrieve the service sstatus and start the service if it is stopped.
$status = (Get-service –DisplayName "Windows Audio").Status
If ($status –like "Stopped") { 
start-service –DisplayName "Windows Audio"
}

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "To add a firewall rule on a system using the netsh command, you need to open PowerShell with the Run as Administrator option"

Note

Warnings or important notes appear in a box like this.

Tip

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 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 , 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.

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 example code

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.

Errata

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 errata submission form 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

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 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

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.