Windows PowerShell for Developers

Book description

Want to perform programming tasks better, faster, simpler, and make them repeatable? Take a deep dive into Windows PowerShell and discover what this distributed automation platform can do. Whether you’re a .NET developer or IT pro, this concise guide will show you how PowerShell’s scripting language can help you be more productive on everyday tasks.

Quickly learn how to create PowerShell scripts and embed them into your existing applications, write "little languages" to solve specific problems, and take charge of your code. This book includes example scripts that you can easily pull apart, tweak, and then use in your own PowerShell and .NET solutions.

  • Slice and dice text, XML, CSV, and JSON with ease
  • Embed PowerShell to provide scripting capabilities for your C# apps
  • Create GUI applications five to ten times faster with less code
  • Leverage PowerShell’s capabilities to work with the Internet
  • Interact with DLLs and create objects, automatically display properties, and call methods in live interactive sessions
  • Build domain-specific languages (DSLs) and vocabularies to express solutions more clearly
  • Work with Microsoft Office via the Component Object Model (COM)
  • Discover PowerShell v3 features included with Windows 8 and Windows Server 2012

Publisher resources

View/Submit Errata

Table of contents

  1. Windows Powershell for Developers
  2. Dedication
  3. Preface
    1. Audience
    2. Assumptions This Book Makes
    3. Contents of This Book
    4. Conventions Used in This Book
    5. Using Code Examples
      1. Available for Download
    6. We’d Like to Hear from You
    7. Safari® Books Online
    8. Acknowledgments
      1. And Now, the Small Village of Folks Who Helped, Inspired, and Supported Me
  4. 1. Introduction
    1. This Is Just the Beginning
    2. Why Use PowerShell
    3. There’s a New Game in Town
    4. An Underutilized Development Tool
  5. 2. Getting Started
    1. Installing PowerShell
      1. Checking the PowerShell Version
    2. Interactivity, the Key to PowerShell
    3. Running a PowerShell Script
      1. Changing the Execution Policy from the Command Line
        1. RemoteSigned is good for you
        2. Running scripts with the execution policy set to Restricted
        3. Now we’re set to run a script
    4. PowerShell ISE
    5. Other PowerShell Editors
    6. PowerShell and Visual Studio
    7. The PowerShell Community
    8. The Future of PowerShell on Windows 8
    9. Summary
  6. 3. The Dime Tour
    1. The Object Pipeline: The Game Changer
    2. Automation References
    3. Semicolons
    4. Return Statements
    5. Datatypes
    6. Exception Handling
      1. Break
      2. Continue
      3. Try/Catch/Finally
    7. Quoting Rules
    8. PowerShell Subexpressions in Strings
    9. Here-Strings
      1. Great Code Generation Techniques
      2. C# Code
    10. Closures, Functions, and Lambdas
      1. Scriptblocks, Dynamic Languages, and Design Patterns
    11. Arrays
      1. Creating an Empty Array
      2. Adding an Array Item
      3. Retrieving an Element from an Array
      4. Array Slicing
      5. Finding Array Elements
      6. Reversing an Array
      7. Assigning Values to Multiple Variables in an Array
    12. Parentheses and Commas
    13. Hash Tables
      1. Creating an Empty Hash Table
      2. Adding a Hash Table Item
      3. Initializing a Hash Table with Items
      4. Concatenating Hash Tables
    14. Get-Member
      1. Filtering with Get-Member
      2. Using Get-Member with Collections
    15. Inject a GUI into the PowerShell Command Line
    16. New-Object
      1. Launching Internet Explorer
      2. Creating a New PowerShell Object
        1. PowerShell v3 is more pithy
      3. Using the .NET Framework
    17. Add-Member
    18. Add-Type
      1. Compiling C# on the Fly
      2. Newing Up the Class
      3. Calling the Add Method on MyMathClass
      4. Wait, I Don’t Have the Source
    19. “What Does % Do?” and Other Aliases
    20. Modules
    21. Summary
  7. 4. Accelerating Delivery
    1. Scanning for const Definitions
      1. Reading a Single C# File
        1. Using Select-String
      2. Reading C# Files in a Directory
    2. Working with Template Engines
      1. The Engine
      2. A Single Variable
      3. Multiple Variables
      4. Multiple Templates
      5. Complex Logic
      6. UML Style Syntax
      7. Reading XML
      8. Bonus Round
    3. Generating PowerShell Functions from C# Methods
      1. Get Parameters
      2. Pulling It All Together
    4. Calling PowerShell Functions from C#
    5. Overriding C# Methods with PowerShell Functions
      1. The Breakdown
      2. Looking for PowerShell Functions
      3. Extracting Metadata and Generating C#
      4. The PowerShell Module
      5. Testing It All
    6. Summary
  8. 5. Add PowerShell to Your GUI
    1. Embedding PowerShell in your C# Application
    2. Beaver Music Application
      1. PowerShell Console
      2. Foundational Functions
        1. New-Album
        2. Add-Album
        3. Import-Csv
        4. Get-Album and Clear-Album
      3. Managing Applications Better with PowerShell
      4. Importing Albums from the Web
        1. Function Get-AlbumFromWeb
        2. PowerShell v3
        3. Out-GridView
        4. Export-ToExcel
      5. Interacting with MEF
        1. Discovering the executable commands
        2. Show-NewAlbumDialog
      6. Implementing Performance Counters
        1. Get-PrivateBytes
        2. Get-YahooMusic
      7. Wiring a Textbox to Execute PowerShell Code
      8. Working in the PreviewKeyDown
      9. Running Script and Debugging the C#
    3. Getting the PowerShell Console in Your App
      1. PSConfig.Profile
      2. PSConfig.AddVariable
      3. The PowerShell Console Code
        1. PS.cs
        2. PSConfig.cs
    4. Summary
  9. 6. PowerShell and the Internet
    1. Net.WebClient
    2. Wrapping Code in a PowerShell Function
      1. Reading CSV-Formatted Data from the Web
    3. Reading XML-Formatted Data from the Web
      1. The Structure of XML Data
      2. US Government Data Sources
    4. Invoke-RestMethod
      1. Detecting XML
      2. Detecting JSON
    5. PowerShell and The New York Times Semantic API
      1. Reading The New York Times, part 1
      2. Reading The New York Times, part 2
    6. New-WebServiceProxy
      1. Stock WebService
      2. Dig a Little Deeper
    7. Invoke-WebRequest
      1. PowerShell and Google
        1. The target HTML
      2. PowerShell and Bing
      3. PowerShell and the Twitter API
    8. Summary
  10. 7. Building GUI Applications in PowerShell
    1. Why a Chapter About GUIs?
      1. Answer: Two Lines of Code
    2. PowerShell and WinForms
    3. PowerShell, ShowUI, and the Twitter API
    4. A Twitter GUI Application
      1. The Code
    5. ShowUI Video Player
    6. Summary
  11. 8. DLLs, Types, Properties, Methods, and Microsoft Roslyn
    1. Sending Text to the Clipboard
    2. Transcoding C# to PowerShell
      1. First, the C#
      2. Intermediate PowerShell
      3. Results
      4. Converting JSON to PowerShell
    3. Microsoft’s Roslyn
      1. Microsoft Roslyn and PowerShell
    4. Using PowerShell to Display Visual Studio Detail
    5. Roslyn’s Document Methods
    6. PowerShell Roslyn Class Viewer
      1. How It Works at a High Level
    7. Summary
  12. 9. Writing Little Languages in PowerShell
    1. Adding a New Construct to PowerShell
    2. PowerShell: A Better XML
      1. But Wait—There’s More
        1. Building the New-ToDoList function
        2. Building the New-ToDoItem function
        3. Where to put this function?
        4. Invoking the script block
        5. The New-ToDoItem Body
      2. Putting It All Together
    3. The Little Language in Action
      1. Is It Worth Creating Your Own Little Language?
    4. Graphviz
      1. Graphviz “Hello World”
      2. Hello World Visual
      3. A PowerShell DSL as a façade to GraphViz
        1. Building Add-Edge
        2. Building New-Graph
        3. Emitting output from New-Graph
    5. Mix and Match PowerShell and GraphViz
      1. Kick It Up a Notch: New-Graph Is an Internal DSL
      2. Graphing the Companies from Get-Process
    6. Summary
  13. 10. PowerShell, COM, and More
    1. Opening a File in Excel Using Invoke-Item
      1. Working Invoke-Item into a PowerShell Script
    2. Calling an Excel Function
      1. Creating an Excel COM Instance
    3. Discovering Available Excel Functions
      1. Calling More Excel Functions
    4. Automating Excel from PowerShell
      1. Making Excel Visible
      2. Creating a Workbook and Worksheets
      3. Putting the Date in a Cell in an Excel Worksheet from PowerShell
      4. Setting Up Pivot Tables in Excel
      5. Building an Excel Pivot Table in PowerShell
    5. Discovering Other COM Applications to Automate
      1. Automating Internet Explorer as a COM Application
    6. Summary
  14. 11. PowerShell Version 3
    1. PowerShell Workflows
      1. PowerShell Script-Based Workflow
      2. Running the Workflow
      3. Running the Workflow on Other Boxes
      4. Discovering More About Your Workflow
      5. Visual Studio Workflow
        1. Import-Module on a XAML workflow
        2. Getting performance counters in parallel
        3. Why workflows rock
    2. Using PowerShell with Web Data: Converting to and from JSON
      1. Converting JSON to PowerShell Objects and Back Again
      2. What If a Web/REST Service Returns JSON?
    3. Creating an Instance of a Microsoft .NET Framework Object
    4. Get-Content –Tail
    5. ISE v3
    6. Out-GridView and the -PassThru Parameter
    7. Scheduling Jobs
    8. Invoke-WebRequest and Invoke-RestMethod
    9. PowerShell v3 Items That Are a Must-See
      1. Show-Command
      2. Less Typing for ForEach and Where
      3. Execute PowerShell Commands from the Web
      4. Windows PowerShell Management ODATA IIS Extensions
    10. Summary
  15. A. Productive PowerShell
    1. Getting Automation Approved
    2. Saving Time with Automation
    3. Adding Aliases to Your PowerShell Profile
    4. Adding Variables and Functions to Your PowerShell Profile
      1. The ql Function
      2. Adding Variables to $Profile
      3. Adding Custom PowerShell Functions
      4. Quick Access to Launching Visual Studio Solutions
    5. Remote Desktop Connection
    6. Starting Another PowerShell Session
      1. Start-Process Can Do More
    7. Using PowerShell’s Tokenizer
    8. PowerShell and Older Tools
      1. Subversion
        1. Displaying SVN info on your PowerShell prompt
          1. The script
    9. Faster, Faster—The Light Is Turning Red
      1. SlowTrain
      2. FastTrain
      3. ForEach-Object Versus ForEach Statement
    10. Summary
  16. B. Running PowerShell with the .NET 4.0 Runtime
  17. About the Author
  18. Copyright

Product information

  • Title: Windows PowerShell for Developers
  • Author(s): Douglas Finke
  • Release date: July 2012
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781449322663