Book Image

Mastering Windows PowerShell Scripting (Second Edition) - Second Edition

By : Brenton J.W. Blawat
Book Image

Mastering Windows PowerShell Scripting (Second Edition) - Second Edition

By: Brenton J.W. Blawat

Overview of this book

PowerShell scripts offer a handy way to automate various chores. Working with these scripts effectively can be a difficult task. This comprehensive guide starts from scratch and covers advanced-level topics to make you a PowerShell expert. The first module, PowerShell Fundamentals, begins with new features, installing PowerShell on Linux, working with parameters and objects, and also how you can work with .NET classes from within PowerShell. In the next module, you’ll see how to efficiently manage large amounts of data and interact with other services using PowerShell. You’ll be able to make the most of PowerShell’s powerful automation feature, where you will have different methods to parse and manipulate data, regular expressions, and WMI. After automation, you will enter the Extending PowerShell module, which covers topics such as asynchronous processing and, creating modules. The final step is to secure your PowerShell, so you will land in the last module, Securing and Debugging PowerShell, which covers PowerShell execution policies, error handling techniques, and testing. By the end of the book, you will be an expert in using the PowerShell language.
Table of Contents (24 chapters)
Title Page
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Preface

PowerShell scripts offer a handy way to automate various chores. Working with these scripts effectively can be a difficult task. This comprehensive guide starts from scratch and covers advanced-level topics, along with tips to make you a PowerShell scripting expert.

What this book covers

Chapter 1, Introduction to PowerShell, introduces PowerShell and why it is applicable to the IT community. This chapter discusses the types of Microsoft systems that leverage PowerShell and why it's important to learn this scripting language.

Chapter 2, Working with PowerShell, talks about finding and using commands and parameters.

Chapter 3, Modules and Snap-Ins, shows how to use PowerShell modules and snap-ins.

Chapter 4, Working with Objects in PowerShell, shows how to work with objects in PowerShell.

Chapter 5, Operators, shows how to use operators to test and manipulate data.

Chapter 6, Variables, Arrays, and Hashtables, explains the different mechanisms within PowerShell that are able to store data. These include variables, hashes, and arrays.

Chapter 7, Branching and Looping, shows how to explore conditional statements and how to implement loops in PowerShell.

Chapter 8, Working with .NET, shows how to work with existing classes from the .NET framework in PowerShell.

Chapter 9, Data Parsing and Manipulation, explains different methods to manipulate simple types in PowerShell.

Chapter 10, Regular Expressions, shows the usage of regular expressions in PowerShell scripts. This will provide the user with the ability to provide quick data comparisons.

Chapter 11, Files, Folders and the Registry, explores different methods to interact with common items on the file system and registry.

Chapter 12, Windows Management Instrumentation, explores PowerShell’s ability to interact with WMI. This includes methods by which users can manipulate the WMI to obtain information pertaining to an operating system.

Chapter 13, HTML, XML, and JSON, explores the XML structure and how PowerShell can interact with XML files. This chapter will also explore how to use XML files as answer files for PowerShell scripts.

Chapter 14, Working with REST and SOAP, shows how to use REST or SOAP interfaces.

Chapter 15, Remoting and Remote Management, explores the use of Windows remoting and remote sessions to execute against remote systems.

Chapter 16, Testing, shows how to use PSScriptAnalyzer and Pester to improve the quality of your code.

Chapter 17, Error Handling, shows how to leverage PowerShell’s error handling to work with errors.

What you need for this book

For this book, you will need Windows 7 or 10 and PowerShell 5.0 or 5.1. Nothing else is mandatory or used.

Who this book is for

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. It is also for those new to the PowerShell language.

Conventions

In this book, you will 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, dummy URLs, user input, and Twitter handles are shown as follows: "Use Get-ChildItem to list all of the environment variables:"

A block of code is set as follows:

function Get-IPConfig { 
    [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() | ForEach-Object { 
        $ipProperties = $_.GetIPProperties() 
             
        $addresses = $ipProperties.UnicastAddresses | 
            Where-Object { 
                $_.Address.AddressFamily -eq 'InterNetwork' 
            } | ForEach-Object { 
                "$($_.Address) $($_.IPv4Mask)" 
            } 
 
        $gateway = $ipProperties.GatewayAddresses.Address | 
            Where-Object { 
                $_.AddressFamily -eq 'InterNetwork' -and  
                $_ -ne '0.0.0.0' 
            } 
 
        [PSCustomObject]@{ 
            Name      = $_.Name 
            Id        = $_.Id 
            Addresses = $addresses 
            Gateway   = $gateway 
        } 
    } | Where-Object { $_.Addresses } 
} 
Get-IPConfig 

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

PS> Update-Help -Module DnsClient -Verbose
VERBOSE: Help was not updated for the module DnsClient, because the Update-Help command was run on this computer within the last 24 hours.

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: "Press the Initialize git repository button, as shown in the following screenshot:"

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 will really get the most out of. To send us general feedback, simply email [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/MasteringWindowsPowerShellScriptingSecondEdition_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 one of our books-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 will be accepted and the errata will be 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 will appear under the Errata section.

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 will do our best to address the problem.