Windows PowerShell for developers : [enhance your ...

Windows Powershell for

Developers

Douglas Finke

O'REILLY

Beijing ? Cambridge ? Farnham ? Kbln ? Sebastopol ? Tokyo

Table of Contents

Preface

1. Introduction This Is Just the Beginning Why Use PowerShell There's a New Game in Town An Underutilized Development Tool

2. Getting Started Installing PowerShell Checking the PowerShell Version Interactivity, the Key to PowerShell

Running a PowerShell Script

Changing the Execution Policy from the Command Line

PowerShell ISE Other PowerShell Editors PowerShell and Visual Studio The PowerShell Community The Future of PowerShell on Windows 8

Summary

3. The Dime Tour

The Object Pipeline: The Game Changer

Automation References Semicolons Return Statements Datatypes

Exception Handling

Break Continue Try/Catch/Finally

xi

1 2 3 3 4

5

5 5 6 7 7 9 10 11 12 12 13

15 15 16

.

17 17 18 18 18 19 19

Quoting Rules

19

PowerShell Subexpressions in Strings

20

Here-Strings

20

Great Code Generation Techniques

20

C# Code

21

Closures, Functions, and Lambdas

21

Scriptblocks, Dynamic Languages, and Design Patterns

22

Arrays

23

Creating an Empty Array

23

Adding an Array Item

23

Retrieving an Element from an Array

23

Array Slicing

24

Finding Array Elements

24

Reversing an Array

24

Assigning Values to Multiple Variables in an Array

24

Parentheses and Commas

25

Hash Tables

25

Creating an Empty Hash Table

25

Adding a Hash Table Item

26

Initializing a Hash Table with Items

26

Concatenating Hash Tables

26

Get-Member

26

Filtering with Get-Member

27

Using Get-Member with Collections

28

Inject a GUI into the PowerShell Command Line

29

New-Object

29

Launching Internet Explorer

30

Creating a New PowerShell Object

30

Using the .NET Framework

31

Add-Member

31

Add-Type

31

Compiling C# on the Fly

31

Newing Up the Class

32

Calling the Add Method on MyMathClass

32

Wait, I Don't Have the Source

32

"What Does % Do?" and Other Aliases

33

Modules

33

Summary

34

4. Accelerating Delivery

37

Scanning for const Definitions

37

Reading a Single C# File

38

Reading C# Files in a Directory

39

vi | Table of Contents

Working with Template Engines The Engine A Single Variable Multiple Variables Multiple Templates Complex Logic UML Style Syntax Reading XML Bonus Round

Generating PowerShell Functions from C# Methods

Get Parameters

Pulling It All Together Calling PowerShell Functions from C# Overriding C# Methods with PowerShell Functions

The Breakdown

Looking for PowerShell Functions Extracting Metadata and Generating C#

The PowerShell Module

Testing It All Summary

5. Add PowerShell to Your GUI Embedding PowerShell in your C# Application Beaver Music Application PowerShell Console

Foundational Functions

Managing Applications Better with PowerShell Importing Albums from the Web Interacting with MEF Implementing Performance Counters Wiring a Textbox to Execute PowerShell Code Working in the PreviewKeyDown Running Script and Debugging the C# Getting the PowerShell Console in Your App PSConfig.Profile PSConfig.AddVariable

The PowerShell Console Code

Summary

6. PowerShell and the Internet

Net.WebClient

Wrapping Code in a PowerShell Function Reading CSV-Formatted Data from the Web

40 40 41 42 42 43 44 45 45 46 47 48 49 50 51 51 52 53 54 55

57 57 60 60 62 65 66 72 73 75 77 79 79 80 - 80 80 83

85 85

86 86

Table of Contents | vii

Reading XML-Formatted Data from the Web

The Structure of XML Data US Government Data Sources Invoke-RestMethod

Detecting XML Detecting JSON

PowerShell and The New York Times Semantic API

Reading The New York Times, part 1 Reading The New York Times, part 2 New-WebServiceProxy

Stock WebService

Dig a Little Deeper Invoke-WebRequest

PowerShell and Google PowerShell and Bing

PowerShell and the Twitter API

Summary

7. Building GUI Applications in PowerShell Why a Chapter About GUIs?

Answer: Two Lines of Code PowerShell and WinForms PowerShell, ShowUI, and the Twitter API A Twitter GUI Application

The Code

ShowUI Video Player Summary

8. DLLs, Types, Properties, Methods, and Microsoft Roslyn Sending Text to the Clipboard Transcoding C# to PowerShell

First, the C# Intermediate PowerShell Results Converting JSON to PowerShell Microsoft's Roslyn

Microsoft Roslyn and PowerShell Using PowerShell to Display Visual Studio Detail Roslyn's Document Methods PowerShell Roslyn Class Viewer

How It Works at a High Level Summary

viii | Table of Contents

87 87 88 88 88 89 89 90 90 91 91 92 93 94 95 96 97

99 99 100 100

101 103 103 105 106

109 109 111 111 113 113 114 114 115 116 118 121 122 123

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download