Windows PowerShell in Action
eBook - ePub

Windows PowerShell in Action

Bruce Payette, Richard Siddaway

Condividi libro
  1. 904 pagine
  2. English
  3. ePUB (disponibile sull'app)
  4. Disponibile su iOS e Android
eBook - ePub

Windows PowerShell in Action

Bruce Payette, Richard Siddaway

Dettagli del libro
Anteprima del libro
Indice dei contenuti
Citazioni

Informazioni sul libro

Summary Windows PowerShell in Action, Third Edition is the definitive guide to PowerShell, now revised to cover PowerShell 6.Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications. About the Technology In 2006, Windows PowerShell reinvented the way administrators and developers interact with Windows. Today, PowerShell is required knowledge for Windows admins and devs. This powerful, dynamic language provides command-line control of the Windows OS and most Windows servers, such as Exchange and SCCM. And because it's a first-class.NET language, you can build amazing shell scripts and tools without reaching for VB or C#. About the Book Windows PowerShell in Action, Third Edition is the definitive guide to PowerShell, now revised to cover PowerShell 6. Written by language designer Bruce Payette and MVP Richard Siddaway, this rich book offers a crystal-clear introduction to the language along with its essential everyday use cases. Beyond the basics, you'll find detailed examples on deep topics like performance, module architecture, and parallel execution. What's Inside

  • The best end-to-end coverage of PowerShell available
  • Updated with coverage of PowerShell v6
  • PowerShell workflows
  • PowerShell classes
  • Writing modules and scripts
  • Desired State Configuration
  • Programming APIs and pipelines


About the Reader Written for intermediate-level developers and administrators. About the Authors Bruce Payette is codesigner and principal author of the Power-Shell language. Richard Siddaway is a longtime PowerShell MVP, author, speaker, and blogger. Table of Contents

  • Welcome to PowerShell
  • Working with types
  • Operators and expressions
  • Advanced operators and variables
  • Flow control in scripts
  • PowerShell functions
  • Advanced functions and scripts
  • Using and authoring modules
  • Module manifests and metadata
  • Metaprogramming with scriptblocks and dynamic code
  • PowerShell remoting
  • PowerShell workflows
  • PowerShell Jobs
  • Errors and exceptions
  • Debugging
  • Working with providers, files, and CIM
  • Working with.NET and events
  • Desired State Configuration
  • Classes in PowerShell
  • The PowerShell and runspace APIs
  • Appendix - PowerShell 6.0 for Windows, Linux, and MacOS

Domande frequenti

Come faccio ad annullare l'abbonamento?
È semplicissimo: basta accedere alla sezione Account nelle Impostazioni e cliccare su "Annulla abbonamento". Dopo la cancellazione, l'abbonamento rimarrà attivo per il periodo rimanente già pagato. Per maggiori informazioni, clicca qui
È possibile scaricare libri? Se sì, come?
Al momento è possibile scaricare tramite l'app tutti i nostri libri ePub mobile-friendly. Anche la maggior parte dei nostri PDF è scaricabile e stiamo lavorando per rendere disponibile quanto prima il download di tutti gli altri file. Per maggiori informazioni, clicca qui
Che differenza c'è tra i piani?
Entrambi i piani ti danno accesso illimitato alla libreria e a tutte le funzionalità di Perlego. Le uniche differenze sono il prezzo e il periodo di abbonamento: con il piano annuale risparmierai circa il 30% rispetto a 12 rate con quello mensile.
Cos'è Perlego?
Perlego è un servizio di abbonamento a testi accademici, che ti permette di accedere a un'intera libreria online a un prezzo inferiore rispetto a quello che pagheresti per acquistare un singolo libro al mese. Con oltre 1 milione di testi suddivisi in più di 1.000 categorie, troverai sicuramente ciò che fa per te! Per maggiori informazioni, clicca qui.
Perlego supporta la sintesi vocale?
Cerca l'icona Sintesi vocale nel prossimo libro che leggerai per verificare se è possibile riprodurre l'audio. Questo strumento permette di leggere il testo a voce alta, evidenziandolo man mano che la lettura procede. Puoi aumentare o diminuire la velocità della sintesi vocale, oppure sospendere la riproduzione. Per maggiori informazioni, clicca qui.
Windows PowerShell in Action è disponibile online in formato PDF/ePub?
Sì, puoi accedere a Windows PowerShell in Action di Bruce Payette, Richard Siddaway in formato PDF e/o ePub, così come ad altri libri molto apprezzati nelle sezioni relative a Informatica e Linguaggi di programmazione. Scopri oltre 1 milione di libri disponibili nel nostro catalogo.

Informazioni

Editore
Manning
Anno
2017
ISBN
9781638351566

Chapter 1. Welcome to PowerShell

This chapter covers
  • Core concepts
  • Aliases and elastic systems
  • Parsing and PowerShell
  • Pipelines
  • Formatting and output
Vizzini: Inconceivable!
Inigo: You keep on using that word. I do not think it means what you think it means.
William Goldman, The Princess Bride
It may seem strange to start by welcoming you to PowerShell when PowerShell is ten years old (at the time of writing), is on its fifth version, and you’re reading the third edition of this book.
Note
PowerShell v6 is under development as we write this. The appendix covers the changes that this new version will introduce.
In reality the adoption of PowerShell is only now achieving significant momentum, meaning that to many users PowerShell is a new technology and the three versions of PowerShell subsequent to this book’s second edition contain many new features. Welcome to PowerShell.
Note
This book is written using PowerShell v5. It’ll be noted in the text where earlier versions are different, or work in a different manner. We’ll also document when various features were introduced to PowerShell or significantly modified between versions. We treat v5 and v5.1 together as v5 as the differences are relatively minor.
Windows PowerShell is the command and scripting language from Microsoft built into all versions of Windows since Windows Server 2008. Although PowerShell is new and different (or has new features you haven’t yet explored), it’s been designed to make use of what you already know, making it easy to learn. It’s also designed to allow you to learn a bit at a time.
Running PowerShell commands
You have two choices for running the examples provided in this book. First is to use the PowerShell console. This provides a command-line interface. It’s the tool of choice for interactive work.
The second choice is the PowerShell Integrated Scripting Environment (ISE). The ISE supplies an editing pane plus a combined output and interactive pane. The ISE is the tool of choice when developing scripts, functions, and other advanced functionality.
The examples in the book will be written in a way that allows pasting directly into either tool.
Third-party tools exist, such as those supplied by Sapien, but we’ll only consider the native tools in this book.
Starting at the beginning, here’s the traditional “Hello world” program in PowerShell:
'Hello world.'
But “Hello world” itself isn’t interesting. Here’s something a bit more complicated:
Get-ChildItem -Path $env:windir\*.log | Select-String -List error | Format-Table Path,LineNumber –AutoSize
Although this is more complex, you can probably still figure out what it does. It searches all the log files in the Windows directory, looking for the string “error”, and then prints the full name of the matching file and the matching line number. “Useful, but not special,” you might think, because you can easily do this using cmd.exe on Windows or bash on UNIX. What about the “big, really big” thing? Well, how about this example:
([xml] [System.Net.WebClient]::new(). DownloadString('http://blogs.msdn.com/powershell/rss.aspx')). RSS.Channel.Item | Format-Table title,link
Now we’re getting somewhere. This script downloads the RSS feed from the PowerShell team blog and then displays the title and a link for each blog entry. By the way, you weren’t expected to figure out this example yet. If you did, you can move to the head of the class!
One last example:
using assembly System.Windows.Forms using namespace System.Windows.Forms $form = [Form] @{ Text = 'My First Form' } $button = [Button] @{ Text = 'Push Me!' Dock = 'Fill' } $button.add_Click{ $form.Close() } $form.Controls.Add($button) $form.ShowDialog()
This script uses the Windows Forms library (WinForms) to build a GUI that has a single button displaying the text “Push Me!” Figure 1.1 shows the window this script creates.
Figure 1.1. When you run the code from the example, this window will be displayed.
When you click the button, it closes the form and exits the script. With this you go from "Hello world" to a GUI application in less than two pages.
Let’s come back down to Earth for a minute. The intent of chapter 1 is to set the stage for understanding PowerShell—what it is, what it isn’t, and, almost as important, why the PowerShell team made the decisions they made in designing the PowerShell language. Chapter 1 covers the goals of the project, along with some of the major issues the team faced in trying to achieve those goals. First, a philosophical digression: while under development, from 2002 until the first public release in 2006, the codename for this project was Monad. The name Monad comes from The Monadology by Gottfried Wilhelm Leibniz, one of the inventors of calculus. Here’s how Leibniz defined the Monad:
The Monad, of which we shall here speak, is nothing but a simple substance, which enters into compounds. By “simple” is meant “without parts.”
Gottfried Wilhelm Leibniz, The Monadology (translated by Robert Latta)
In The Monadology, Leibniz describes a world of irreducible components from which all things could be composed. This captures the spirit of the project: to create a toolkit of simple pieces you compose to create complex solutions.

1.1. What is PowerShell?

What is PowerShell, and what can you do with it? Ask a group of PowerShell users and you’ll get different answers:
  • PowerShell is a command-line shell.
  • PowerShell is a scripting environment.
  • PowerShell is an automation engine.
These are all part of the answer. We prefer to say PowerShell is a tool you can use to manage your Microsoft-based machines and applications that programs consistency into your management process. The tool is attractive to administrators and developers in that it can span the range of command line, simple and advanced scripts, to real programs.
Note
If you take this to mean PowerShell is the ideal DevOps tool for the Microsoft platform, then congratulations—you’ve got it in one.
PowerShell draws heavily from existing command-line shell and scripting languages, but the language, runtime, and subsequent additions, such as PowerShell Workflows and Desired State Configuration, were designed from scratch to be an optimal environment for the modern Windows operating system.
Most people are introduced to PowerShell through its interactive aspects. Let’s refine our definitions of shell and scripting.

1.1.1. Shells, command lines, and scripting languages

In the previous section we called PowerShell a command-line shell. You may be asking, what’s a shell? And how’s it different from a command interpreter? What about scripting languages? If you can script in a shell language, doesn’t that make it a scripting language? In answering these questions, let’s start with shells.
Defining a shell can be tricky because pretty much everything at Microsoft has something called a shell. Windows Explorer is a shell. Visual Studio has a component called a shell. Heck, even the Xbox has somet...

Indice dei contenuti