Windows PowerShell in Action
eBook - ePub

Windows PowerShell in Action

Bruce Payette, Richard Siddaway

Buch teilen
  1. 904 Seiten
  2. English
  3. ePUB (handyfreundlich)
  4. Über iOS und Android verfĂŒgbar
eBook - ePub

Windows PowerShell in Action

Bruce Payette, Richard Siddaway

Angaben zum Buch
Buchvorschau
Inhaltsverzeichnis
Quellenangaben

Über dieses Buch

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

HĂ€ufig gestellte Fragen

Wie kann ich mein Abo kĂŒndigen?
Gehe einfach zum Kontobereich in den Einstellungen und klicke auf „Abo kĂŒndigen“ – ganz einfach. Nachdem du gekĂŒndigt hast, bleibt deine Mitgliedschaft fĂŒr den verbleibenden Abozeitraum, den du bereits bezahlt hast, aktiv. Mehr Informationen hier.
(Wie) Kann ich BĂŒcher herunterladen?
Derzeit stehen all unsere auf MobilgerĂ€te reagierenden ePub-BĂŒcher zum Download ĂŒber die App zur VerfĂŒgung. Die meisten unserer PDFs stehen ebenfalls zum Download bereit; wir arbeiten daran, auch die ĂŒbrigen PDFs zum Download anzubieten, bei denen dies aktuell noch nicht möglich ist. Weitere Informationen hier.
Welcher Unterschied besteht bei den Preisen zwischen den AboplÀnen?
Mit beiden AboplÀnen erhÀltst du vollen Zugang zur Bibliothek und allen Funktionen von Perlego. Die einzigen Unterschiede bestehen im Preis und dem Abozeitraum: Mit dem Jahresabo sparst du auf 12 Monate gerechnet im Vergleich zum Monatsabo rund 30 %.
Was ist Perlego?
Wir sind ein Online-Abodienst fĂŒr LehrbĂŒcher, bei dem du fĂŒr weniger als den Preis eines einzelnen Buches pro Monat Zugang zu einer ganzen Online-Bibliothek erhĂ€ltst. Mit ĂŒber 1 Million BĂŒchern zu ĂŒber 1.000 verschiedenen Themen haben wir bestimmt alles, was du brauchst! Weitere Informationen hier.
UnterstĂŒtzt Perlego Text-zu-Sprache?
Achte auf das Symbol zum Vorlesen in deinem nÀchsten Buch, um zu sehen, ob du es dir auch anhören kannst. Bei diesem Tool wird dir Text laut vorgelesen, wobei der Text beim Vorlesen auch grafisch hervorgehoben wird. Du kannst das Vorlesen jederzeit anhalten, beschleunigen und verlangsamen. Weitere Informationen hier.
Ist Windows PowerShell in Action als Online-PDF/ePub verfĂŒgbar?
Ja, du hast Zugang zu Windows PowerShell in Action von Bruce Payette, Richard Siddaway im PDF- und/oder ePub-Format sowie zu anderen beliebten BĂŒchern aus Informatica & Linguaggi di programmazione. Aus unserem Katalog stehen dir ĂŒber 1 Million BĂŒcher zur VerfĂŒgung.

Information

Verlag
Manning
Jahr
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...

Inhaltsverzeichnis