Log In
Or create an account ->
Imperial Library
Home
About
News
Upload
Forum
Help
Login/SignUp
Index
Windows Powershell for Developers
Dedication
Preface
Audience
Assumptions This Book Makes
Contents of This Book
Conventions Used in This Book
Using Code Examples
Available for Download
We’d Like to Hear from You
Safari® Books Online
Acknowledgments
And Now, the Small Village of Folks Who Helped, Inspired, and Supported Me
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
RemoteSigned is good for you
Running scripts with the execution policy set to Restricted
Now we’re set to run a script
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
Quoting Rules
PowerShell Subexpressions in Strings
Here-Strings
Great Code Generation Techniques
C# Code
Closures, Functions, and Lambdas
Scriptblocks, Dynamic Languages, and Design Patterns
Arrays
Creating an Empty Array
Adding an Array Item
Retrieving an Element from an Array
Array Slicing
Finding Array Elements
Reversing an Array
Assigning Values to Multiple Variables in an Array
Parentheses and Commas
Hash Tables
Creating an Empty Hash Table
Adding a Hash Table Item
Initializing a Hash Table with Items
Concatenating Hash Tables
Get-Member
Filtering with Get-Member
Using Get-Member with Collections
Inject a GUI into the PowerShell Command Line
New-Object
Launching Internet Explorer
Creating a New PowerShell Object
PowerShell v3 is more pithy
Using the .NET Framework
Add-Member
Add-Type
Compiling C# on the Fly
Newing Up the Class
Calling the Add Method on MyMathClass
Wait, I Don’t Have the Source
“What Does % Do?” and Other Aliases
Modules
Summary
4. Accelerating Delivery
Scanning for const Definitions
Reading a Single C# File
Using Select-String
Reading C# Files in a Directory
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
New-Album
Add-Album
Import-Csv
Get-Album and Clear-Album
Managing Applications Better with PowerShell
Importing Albums from the Web
Function Get-AlbumFromWeb
PowerShell v3
Out-GridView
Export-ToExcel
Interacting with MEF
Discovering the executable commands
Show-NewAlbumDialog
Implementing Performance Counters
Get-PrivateBytes
Get-YahooMusic
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
PS.cs
PSConfig.cs
Summary
6. PowerShell and the Internet
Net.WebClient
Wrapping Code in a PowerShell Function
Reading CSV-Formatted Data from the Web
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
The target HTML
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
9. Writing Little Languages in PowerShell
Adding a New Construct to PowerShell
PowerShell: A Better XML
But Wait—There’s More
Building the New-ToDoList function
Building the New-ToDoItem function
Where to put this function?
Invoking the script block
The New-ToDoItem Body
Putting It All Together
The Little Language in Action
Is It Worth Creating Your Own Little Language?
Graphviz
Graphviz “Hello World”
Hello World Visual
A PowerShell DSL as a façade to GraphViz
Building Add-Edge
Building New-Graph
Emitting output from New-Graph
Mix and Match PowerShell and GraphViz
Kick It Up a Notch: New-Graph Is an Internal DSL
Graphing the Companies from Get-Process
Summary
10. PowerShell, COM, and More
Opening a File in Excel Using Invoke-Item
Working Invoke-Item into a PowerShell Script
Calling an Excel Function
Creating an Excel COM Instance
Discovering Available Excel Functions
Calling More Excel Functions
Automating Excel from PowerShell
Making Excel Visible
Creating a Workbook and Worksheets
Putting the Date in a Cell in an Excel Worksheet from PowerShell
Setting Up Pivot Tables in Excel
Building an Excel Pivot Table in PowerShell
Discovering Other COM Applications to Automate
Automating Internet Explorer as a COM Application
Summary
11. PowerShell Version 3
PowerShell Workflows
PowerShell Script-Based Workflow
Running the Workflow
Running the Workflow on Other Boxes
Discovering More About Your Workflow
Visual Studio Workflow
Import-Module on a XAML workflow
Getting performance counters in parallel
Why workflows rock
Using PowerShell with Web Data: Converting to and from JSON
Converting JSON to PowerShell Objects and Back Again
What If a Web/REST Service Returns JSON?
Creating an Instance of a Microsoft .NET Framework Object
Get-Content –Tail
ISE v3
Out-GridView and the -PassThru Parameter
Scheduling Jobs
Invoke-WebRequest and Invoke-RestMethod
PowerShell v3 Items That Are a Must-See
Show-Command
Less Typing for ForEach and Where
Execute PowerShell Commands from the Web
Windows PowerShell Management ODATA IIS Extensions
Summary
A. Productive PowerShell
Getting Automation Approved
Saving Time with Automation
Adding Aliases to Your PowerShell Profile
Adding Variables and Functions to Your PowerShell Profile
The ql Function
Adding Variables to $Profile
Adding Custom PowerShell Functions
Quick Access to Launching Visual Studio Solutions
Remote Desktop Connection
Starting Another PowerShell Session
Start-Process Can Do More
Using PowerShell’s Tokenizer
PowerShell and Older Tools
Subversion
Displaying SVN info on your PowerShell prompt
The script
Faster, Faster—The Light Is Turning Red
SlowTrain
FastTrain
ForEach-Object Versus ForEach Statement
Summary
B. Running PowerShell with the .NET 4.0 Runtime
About the Author
Copyright
← Prev
Back
Next →
← Prev
Back
Next →