|Learn PowerShell Scripting in a Month of Lunches (Manning)|
Authors: Don Jones & Jeffrey Hicks
This book aims to teach you PowerShell scripting in around 30 hours, how does it fare?
PowerShell is a tool that allows you to automate and explore many administration tasks on various systems (e.g. Windows Server, SQL Server, Network server etc). Automation provides consistency, reduces errors, and saves time – all good reasons for investigating PowerShell. Additionally, many jobs are asking for PowerShell, typically as an adjunct to other skills.
Single line PowerShell commands are great for getting interactive answers to your queries, but often you will want to create a script that integrates related single line commands to answer bigger questions (e.g. find all my databases and report which ones don’t have the latest software updates). While the book covers single line commands, it is really about how to combine them to create the best scripts you can – in a PowerShell-like manner.
The publisher Manning Press has a series of “… in a Month of Lunches” books, with the aim of introducing you to a topic using your lunch hour, by completing all the chapters within a month. This book picks up where the authors’ related book “Learn Windows PowerShell in a Month of Lunches” finishes, see my review of the previous title.
Below is an overview of the book’s content:
Introduction to Scripting
The book opens with a look at the process of creating a script that combines single line PowerShell commands. Details are provided on how to set up your scripting environment, discussing the operating system (PowerShell works on Windows, Linux, and Mac), script editors (Integrated Shell Environment [ISE] is common but Visual Studio Code is preferred – since it provides great debugging facilities), and the advantages of using a virtual environment.
Having run many classes, the authors are aware of the most common pitfalls when starting out with scripting – the number one problem is people create tools (commands) that do more than one thing. Ideally if your tool does one thing only, it is easier to test, debug, and incorporate into other scripts. Another common problem is beginners try to reinvent how things work, whereas they should look at how PowerShell currently works and copy its patterns (e.g. naming convention).
The book continues with a look at the pipeline, this takes output from one command and uses it as input for another command. If you follow the authors suggestions regarding simple single functionality, and generic output, using the pipeline will become much easier.
So far, the book has largely been about preparation, now we look at the syntax of the scripting language, namely: If, ForEach, Switch, Do/While, For, and Break. If you’re familiar with programming, you will quickly get to grips with this.
There is a useful interlude, where the authors ask you to step back and think about creating commands that do only one thing, and how they can be incorporated into controller scripts.
There is a brief discussion about privacy and security. In essence, PowerShell provides very little in addition, if you have permission to do things, then PowerShell will not prevent you.
Building a PowerShell Script
Having looked at the scripting background, this section deals with the practicalities of writing code. It opens with another interlude, where the authors discuss the advantages of taking the time to design your code first. Often developers want to rush into coding, but that often leads to misunderstandings and mistakes, and takes longer than when the task is designed and then coded.
It is suggested you run your single line commands in an editor first, and test them thoroughly, before incorporating them into your scripts, since commands often may not behave in the way you assume.
In many ways, what follows is the heart of the book. The authors provide a base example (i.e. get details of Windows machines), in which new commands are created and tested, then incorporated into a script. Over the next few chapters, this script is extended to include: parameters (mandatory, default values), verbose messaging, comments, help, error handling (try/catch/finally), and a manifest (meta file for faster loading etc).
Having looked at writing PowerShell scripts, this section concerns itself with features to move your coding experience into the realms of the professional developer. The initial chapter looks at naming conventions, code formatting, commenting your code, and thinking about flexibility and future usage.
Source control provides a backup of your code, it also gives the ability for several people to work on a project. Git is a commonly used system, and its general usage is discussed from a practical viewpoint. Testing is also very important, and the Pester tool is discussed (it facilitates unit testing). Other aspects of professionalism discussed include signing you code (for security/authentication), and publishing (for others to reuse).
This section contains an assortment of techniques to improve your PowerShell scripting experience. It opens with a discussion concerning the various types of bugs in code. It then moves on to the associated topic of using Visual Studio Code as an editor since it contains many features that aid the debugging process (e.g. watch and change variables, inspect the call stack, set breakpoints, and step-by step execution of code). All of this is much easier if you are already familiar with the Visual Studio environment.
Another technique includes creating prettier output, which often involves the script writing out a generic object, and then piping it as input into various tools that create formatted output (e.g. CSV, HTML, Excel etc). A related technique discusses how we should be storing data in a database, making querying much easier, an example is provided of storing data into SQL Server.
Before you think that PowerShell has limited functionality, the authors show how it can be extended to use any .NET class, and provides an interesting example concerning the speech class.
The book ends with a look at your next steps, suggesting where to go for help, other books, forums, and user groups.
This is a very hands-on book, providing a gentle and gradated introduction to creating PowerShell scripts to explore and automate various administration tasks. All the topics are explained very clearly, with practical examples, and with common misunderstandings explained. The book is based on the authors’ courses, which might explain its insightful manner.
To get the most out of this book, you should already have some basic knowledge of PowerShell, and the authors' previous book is the ideal starting point.
This book is a great starting point for learning how to script PowerShell to provide automated solutions to your administration tasks. It provides useful practical content, and helpful pointers for further information. Highly recommended.
|Last Updated ( Tuesday, 02 June 2020 )|