Windows PowerShell in Action
eBook - ePub

Windows PowerShell in Action

Bruce Payette, Richard Siddaway

Partager le livre
  1. 904 pages
  2. English
  3. ePUB (adapté aux mobiles)
  4. Disponible sur iOS et Android
eBook - ePub

Windows PowerShell in Action

Bruce Payette, Richard Siddaway

DĂ©tails du livre
Aperçu du livre
Table des matiĂšres
Citations

À propos de ce livre

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

Foire aux questions

Comment puis-je résilier mon abonnement ?
Il vous suffit de vous rendre dans la section compte dans paramĂštres et de cliquer sur « RĂ©silier l’abonnement ». C’est aussi simple que cela ! Une fois que vous aurez rĂ©siliĂ© votre abonnement, il restera actif pour le reste de la pĂ©riode pour laquelle vous avez payĂ©. DĂ©couvrez-en plus ici.
Puis-je / comment puis-je télécharger des livres ?
Pour le moment, tous nos livres en format ePub adaptĂ©s aux mobiles peuvent ĂȘtre tĂ©lĂ©chargĂ©s via l’application. La plupart de nos PDF sont Ă©galement disponibles en tĂ©lĂ©chargement et les autres seront tĂ©lĂ©chargeables trĂšs prochainement. DĂ©couvrez-en plus ici.
Quelle est la différence entre les formules tarifaires ?
Les deux abonnements vous donnent un accĂšs complet Ă  la bibliothĂšque et Ă  toutes les fonctionnalitĂ©s de Perlego. Les seules diffĂ©rences sont les tarifs ainsi que la pĂ©riode d’abonnement : avec l’abonnement annuel, vous Ă©conomiserez environ 30 % par rapport Ă  12 mois d’abonnement mensuel.
Qu’est-ce que Perlego ?
Nous sommes un service d’abonnement Ă  des ouvrages universitaires en ligne, oĂč vous pouvez accĂ©der Ă  toute une bibliothĂšque pour un prix infĂ©rieur Ă  celui d’un seul livre par mois. Avec plus d’un million de livres sur plus de 1 000 sujets, nous avons ce qu’il vous faut ! DĂ©couvrez-en plus ici.
Prenez-vous en charge la synthÚse vocale ?
Recherchez le symbole Écouter sur votre prochain livre pour voir si vous pouvez l’écouter. L’outil Écouter lit le texte Ă  haute voix pour vous, en surlignant le passage qui est en cours de lecture. Vous pouvez le mettre sur pause, l’accĂ©lĂ©rer ou le ralentir. DĂ©couvrez-en plus ici.
Est-ce que Windows PowerShell in Action est un PDF/ePUB en ligne ?
Oui, vous pouvez accĂ©der Ă  Windows PowerShell in Action par Bruce Payette, Richard Siddaway en format PDF et/ou ePUB ainsi qu’à d’autres livres populaires dans Informatica et Linguaggi di programmazione. Nous disposons de plus d’un million d’ouvrages Ă  dĂ©couvrir dans notre catalogue.

Informations

Éditeur
Manning
Année
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...

Table des matiĂšres