contents

  

Front matter

foreword

preface

acknowledgments

about this book

about the authors

  

  1 Before you begin

  1.1   Why you can no longer afford to ignore PowerShell

Life without PowerShell

Life with PowerShell

  1.2   Windows, Linux, and macOS, oh my

  1.3   Is this book for you?

  1.4   How to use this book

The chapters

Hands-on labs

Supplementary materials

Further exploration

Above and beyond

  1.5   Setting up your lab environment

  1.6   Installing PowerShell

  1.7   Contacting us

  1.8   Being immediately effective with PowerShell

  2 Meet PowerShell

  2.1   PowerShell on Windows

  2.2   PowerShell on macOS

Installation on macOS

  2.3   PowerShell on Linux (Ubuntu 18.04)

Installation on Ubuntu 18.04

  2.4   Visual Studio Code and the PowerShell extension

Installing Visual Studio Code and the PowerShell extension

Getting familiar with Visual Studio Code

Customizing Visual Studio Code and the PowerShell extension

  2.5   It’s typing class all over again

  2.6   What version is this?

  2.7   Lab

  3 Using the help system

  3.1   The help system: How you discover commands

  3.2   Updatable help

  3.3   Asking for help

  3.4   Using help to find commands

  3.5   Interpreting the help

Parameter sets and common parameters

Optional and mandatory parameters

Positional parameters

Parameter values

Finding command examples

  3.6   Accessing “about” topics

  3.7   Accessing online help

  3.8   Lab

  3.9   Lab answers

  4 Running commands

  4.1   Let’s talk security

Execution policy

  4.2   Not scripting, but running commands

  4.3   The anatomy of a command

  4.4   The cmdlet naming convention

  4.5   Aliases: Nicknames for commands

  4.6   Taking shortcuts

Truncating parameter names

Using parameter name aliases

Using positional parameters

  4.7   Support for external commands

  4.8   Dealing with errors

  4.9   Common points of confusion

Typing cmdlet names

Typing parameters

  4.10 Lab

  4.11 Lab answers

  5 Working with providers

  5.1   What are providers?

  5.2   Understanding how the filesystem is organized

  5.3   Navigating the filesystem

  5.4   Using wildcards and literal paths

  5.5   Working with other providers

Windows Registry

  5.6   Lab

  5.7   Lab answers

  6 The pipeline: Connecting commands

  6.1   Connecting one command to another: Less work for you

  6.2   Exporting to a file

Exporting to CSV

Exporting to JSON

Exporting to XML

Out-File

Comparing files

  6.3   Piping to a file

  6.4   Converting to HTML

  6.5   Using cmdlets that modify the system: Killing processes

  6.6   Common points of confusion

  6.7   Lab

  6.8   Lab answers

  7 Adding commands

  7.1   How one shell can do everything

  7.2   Extensions: Finding and installing modules

  7.3   Extensions: Finding and adding modules

  7.4   Command conflicts and removing extensions

  7.5   Playing with a new module

  7.6   Common points of confusion

  7.7   Lab

  7.8   Lab answers

  8 Objects: Data by another name

  8.1   What are objects?

  8.2   Understanding why PowerShell uses objects

  8.3   Discovering objects: Get-Member

  8.4   Using object attributes, or properties

  8.5   Using object actions, or methods

  8.6   Sorting objects

  8.7   Selecting the properties you want

  8.8   Objects until the end

  8.9   Common points of confusion

  8.10 Lab

  8.11 Lab answers

  9 A practical interlude

  9.1   Defining the task

  9.2   Finding the commands

  9.3   Learning to use the commands

  9.4   Tips for teaching yourself

  9.5   Lab

  9.6   Lab answer

10 The pipeline, deeper

10.1   The pipeline: Enabling power with less typing

10.2   How PowerShell passes data down the pipeline

10.3   Plan A: Pipeline input ByValue

10.4   Plan B: Pipeline input ByPropertyName

10.5   When things don’t line up: Custom properties

10.6   Working with Azure PowerShell

10.7   Parenthetical commands

10.8   Extracting the value from a single property

10.9   Lab

10.10 Lab answers

10.11 Further exploration

11 Formatting: And why it’s done on the right

11.1   Formatting: Making what you see prettier

11.2   Working with the default formatting

11.2   Formatting tables

11.3   Formatting lists

11.4   Formatting wide lists

11.5   Creating custom columns and list entries

11.6   Going out: To a file or to the host

11.7   Another out: GridViews

11.8   Common points of confusion

Always format right

One type of object at a time, please

11.9   Lab

11.10 Lab answers

11.11 Further exploration

12 Filtering and comparisons

12.1   Making the shell give you just what you need

12.2   Filtering left

12.3   Using comparison operators

12.4   Filtering objects out of the pipeline

12.5   Using the iterative command-line model

12.6   Common points of confusion

Filter left, please

When $_ is allowed

12.7   Lab

12.8   Lab answers

12.9   Further exploration

13 Remote control: One-to-one and one-to-many

13.1   The idea behind remote PowerShell

Remoting on Windows devices

Remoting on macOS and Linux devices

Cross-platform remoting

13.2   Setting up PSRP over SSH

macOS and Linux

Setting up SSH on Windows

13.3   PSRP over SSH overview

13.4   WinRM overview

13.5   Using Enter-PSSession and Exit-PSSession for one-to-one remoting

13.6   Using Invoke-ScriptBlock for one-to-many remoting

13.7   Differences between remote and local commands

Deserialized objects

Local vs. remote processing

13.8   But wait, there’s more

13.9   Common points of confusion

13.10 Lab

13.11 Lab answers

13.12 Further exploration

14 Multitasking with background jobs

14.1   Making PowerShell do multiple things at the same time

14.2   Synchronous vs. asynchronous

14.3   Creating a process job

14.4   Creating a thread job

14.5   Remoting, as a job

14.6   Jobs in the wild

14.7   Getting job results

14.8   Working with child jobs

14.9   Commands for managing jobs

14.10 Common points of confusion

14.11 Lab

14.12 Lab answers

15 Working with many objects, one at a time

15.1   The preferred way: “Batch” cmdlets

15.2   The CIM way: Invoking methods

15.3   The backup plan: Enumerating objects

Making the cmdlets work for you

15.4   Let’s speed things up

15.5   Common points of confusion

Which way is the right way?

Diminishing returns of Parallel ForEach

Method documentation

ForEach-Object confusion

15.6   Lab

15.7   Lab answers

16 Variables: A place to store your stuff

16.1   Introduction to variables

16.2   Storing values in variables

16.3   Using variables: Fun tricks with quotes

16.4   Storing many objects in a variable

Working with single objects in a variable

Working with multiple objects in a variable

Other ways to work with multiple objects

Unrolling properties and methods in PowerShell

16.5   More tricks with double quotes

16.6   Declaring a variable’s type

16.7   Commands for working with variables

16.8   Variable best practices

16.9   Common points of confusion

16.10 Lab

16.11 Lab answers

16.12 Further exploration

17 Input and output

17.1   Prompting for, and displaying, information

17.2   Read-Host

17.3   Write-Host

17.4   Write-Output

17.5   Other ways to write

17.6   Lab

17.7   Lab answers

17.8   Further exploration

18 Sessions: Remote control with less work

18.1   Creating and using reusable sessions

18.2   Enter-PSSession with session objects

18.3   Invoke-Command with session objects

18.4   Implicit remoting: Importing a session

18.5   Using disconnected sessions

18.6   Lab

18.7   Lab answers

18.8   Further exploration

19 You call this scripting?

19.1   Not programming, more like batch files

19.2   Making commands repeatable

19.3   Parameterizing commands

19.4   Creating a parameterized script

19.5   Documenting your script

19.6   One script, one pipeline

19.7   A quick look at scope

19.8   Lab

19.9   Lab answer

20 Improving your parameterized script

20.1   Starting point

20.2   Getting PowerShell to do the hard work

20.3   Making parameters mandatory

20.4   Adding parameter aliases

20.5   Validating parameter input

20.6   Adding the warm and fuzzies with verbose output

20.7   Lab

20.8   Lab answer

21 Using regular expressions to parse text files

21.1   The purpose of regular expressions

21.2   A regex syntax primer

21.3   Using regex with -Match

21.4   Using regex with Select-String

21.5   Lab

21.6   Lab answers

21.7   Further exploration

22 Using someone else’s script

22.1   The script

Parameter block

Process block

22.2   It’s a line-by-line examination

22.3   Lab

22.4   Lab answer

23 Adding logic and loops

23.1   Foreach and Foreach-Object

Foreach

Foreach-Object

Foreach-Object -Parallel

23.2   While

23.3   Do While

23.4   Lab

23.5   Lab answers

24 Handling errors

24.1   Understanding errors and exceptions

24.2   Bad handling

24.3   Two reasons for exception handling

24.4   Handling exceptions

24.5   Handling exceptions for noncommands

24.6   Going further with exception handling

24.7   Lab

24.8   Lab answer

25 Debugging techniques

25.1   Output everything

25.2   One line at a time

25.3   Hey, script, stop right there with breakpoints

25.4   Lab

26 Tips, tricks, and techniques

26.1   Profiles, prompts, and colors: Customizing the shell

PowerShell profiles

Customizing the prompt

Tweaking colors

26.2   Operators: -as, -is, -replace, -join, -split, -contains, -in

-as and -is

-replace

-join and -split

-contains and -in

26.3   String manipulation

26.4   Date manipulation

26.5   Dealing with WMI dates

26.6   Setting default parameter values

26.7   Playing with script blocks

26.8   More tips, tricks, and techniques

27 Never the end

27.1   Ideas for further exploration

27.2   “Now that I’ve read the book, where do I start?”

27.3   Other resources you’ll grow to love

  

Appendix. PowerShell cheat sheet

  

index

  

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.190.170