Zum Hauptinhalt springen Zur Suche springen Zur Hauptnavigation springen

Building Better PowerShell Code

56,99 €

Sofort verfügbar, Lieferzeit: Sofort lieferbar

Format auswählen

Building Better PowerShell Code, Apress
Applying Proven Practices One Tip at a Time
Von Adam Bertram, im heise Shop in digitaler Fassung erhältlich

Produktinformationen "Building Better PowerShell Code"

Learn to write better PowerShell code via short, example-driven tips. This book covers tips to make your PowerShell scripts faster and easier to read all while following proven best practices. Written by a six-time Microsoft MVP and one of the first Microsoft PowerShell MVPs with over a decade of PowerShell experience, Building Better PowerShell Code gives you easily digestible tips you can begin using immediately.

The book starts with an overview of some of the most important tips the author can muster which segues into a deeper dive with dozens of examples throughout the book. It takes you through tips such as using community modules, writing better comments, thinking of PowerShell functions as building blocks, and more. You will also see how to use parameters the right way and how to create simple logging code to easily record script activity.

You will learn not only how to write better code, but also how to implement some mindset tricks, such as being explicit and specific with code and how to write code that reads well. You’ll get into error handling and also how to make your scripts more secure. Finally, you’ll examine the concept of building PowerShell tools and how to build scripts for speed.

Other tips and best practices include:

* Building Pester tests
* Improving performance through parallel processing
* Writing cross-platform scripts
* Using filtering properly

After reading this book and applying these tips, you will have an expert coding mindset and be able to build PowerShell code that’s efficient, readable, and compliant with many best practices.

WHAT YOU WILL LEARN

* Implement error handling
* Create a logging function
* Use regular expressions to search strings
Implement parallel processing


WHO THIS BOOK IS FOR

PowerShell script developers.

Adam Bertram is a 20+ year veteran of IT and an experienced online business professional. He’s a consultant, Microsoft MVP, blogger, trainer, published author and content marketer for multiple technology companies.

INTRODUCTION

CHAPTER 1. DO THE BASICS

Plan Before you Code

Don’t Reinvent the Wheel

Build Functions as Building Blocks

Build Re-usable Tools

Don’t Focus Purely on Performance

Build Pester tests

Implement Error handling

Build Manageable Code

Don’t Skimp on Security

Log Script Activity

Parameterize Everything

Limit Script and Function Input

Maintain Coding Standards

Code in Context

Return Informative Output

Understand Your Code

Use Version Control

Write for Cross Platform

Write for the Next Person

Use a Code Editor

CHAPTER 2. DON’T REINVENT THE WHEEL

Use Community Modules

Leverage Others’ work

CHAPTER 3: USE VISUAL STUDIO CODE

PowerShell Integrated Scripting Environment

Using Visual studio over PowerShell ISE

CHAPTER 4. PLAN BEFORE YOU CODE

Write Comments Before Coding

Use your Code as a Todo List

CHAPTER 5. CREATE BUILDING BLOCKS WITH FUNCTIONS

Write Functions with One, Single Goal

Build Functions with Pipeline Support

Save Commonly-Used, Interactive Functions to Your Profile

CHAPTER 6. PARAMETERIZE EVERYTHING

Don’t Hardcode. Always Use Parameters

Use Parameter Sets When All Parameters Should Not be Used at Once

Use a PSCredential Object Rather than a Separate Username and Password

CHAPTER 7. LOG SCRIPT ACTIVITY

Use a Logging Function

Clean up Verbose Messages

CHAPTER 8. BUILD WITH MANAGEABILITY IN MIND

DRY: Don’t Repeat Yourself

Don’t Store Configuration Items in Code

Always Remove Dead Code

CHAPTER 9. BE SPECIFIC

Use Strict Mode

Don’t Ignore Errors

Validate Input Parameters

Explicitly Define Parameter Types

Enforce Mandatory Parameters

Use the #requires Statement .

CHAPTER 10. WRITE FOR THE NEXT PERSON

Give your Variables Meaningful Names

String Substitution

Don’t use Aliases in a Script

Put functions in Alphabetical Order in a Module

Explain Regular Expressions with Comments

Write Comment-Based Help

Weigh the Difference Between Performance and Readability

CHAPTER 11. HANDLE ERRORS GRACEFULLY

Force Hard-Terminating Errors

Avoid Using $?

Copy $Error[0] to your Own Variable

CHAPTER 12. DON’T SKIMP ON SECURITY

Sign Scripts

Use Scriptblock Logging

Never Store Sensitive Information in Clear Text in Code

Don’t use Invoke-Expression

Use PowerShell Constrained Language Mode

CHAPTER 13. STICK TO POWERSHELL

Use Native PowerShell Where Possible

Use PowerShell standard cmdlet naming

CHAPTER 14. BUILD TOOLS

Code for Portablity

Wrap Command-Line Utilities in Functions .

Force Functions to Return Common Object Types

Ensure Module Functions Cover all the Verbs

CHAPTER 15. RETURN STANDARDIZED, INFORMATIVE OUTPUT

Use Progress Bars Wisely

Leave the Format Cmdlets to the Console

Use Write-Verbose

Use Write-Information

Ensure a Command Returns One Type of Object

Only Return Necessary Information to the Pipeline

CHAPTER 16. BUILD SCRIPTS FOR SPEED

Use an ArrayList or GenericList .NET Class when Elements Need to be Added to an Array

Use a Regular Expression to Search Multiple String Values

Don’t use Write-Host in Bulk

Don’t use the Pipeline

Use the .foreach() and .where() Methods .

Use Parallel Processing .

Use the .NET StreamReader Class When Reading Large Text Files

CHAPTER 17: USE VERSION CONTROL

Create repositories based on a purpose

Commit Code changes based on small goals

Create a branch based on the future

CHAPTER 18. BUILD TESTS

Learn the Pester Basics

Leverage Infrastructure Tests

Automate Pester Tests

Use PSScriptAnalyzer

CHAPTER 19. MISCELLANEOUS TIPS

Write for Cross Platform .

Don’t Query the Win32_Product CIM Class

Create a Shortcut to run PowerShell as Administrator . .

Store ‘Formatable’ Strings for Use Later

Use Out-GridView for GUI-based Sorting and Filtering

Don’t Make Automation Scripts Interactive

CHAPTER 20. SUMMARY

Artikel-Details

Anbieter:
Apress
Autor:
Adam Bertram
Artikelnummer:
9781484263884
Veröffentlicht:
19.10.20