1

Interacting with TechEd NA 2012 Schedule using PowerShell v3

The techniques used in this article are covered in deeper detail in Doug Finke’s book “Windows PowerShell for Developers” . Microsoft TechEd NA 2012 is just around corner. One way to get at content catalog is through the web page at http://goo.gl/EMCYo. Another way is to use PowerShell v3 and ShowUI.

Here is the PowerShell WPF GUI that lets you search the TechEd content catalog;  simple and easy, read on to see what else you can do with this PowerShell script, download it and check out how it was built.

Play Along

If you want to try this out you need a few things:

The only requirements are PowerShell v3 and the TechEd PowerShell scripts.

TechEd Content at the Command Line

Here are some alternate ways to interact with the TechEd content catalog. You can run this script at the command line and search for any session with the string 2012 in it. Then pipe it to Out-GridView cmdlet.

PS> .\Get-TechEdTitle.ps1 2012 | Out-GridView

Here is the result. You can further search/subset the list by typing in the text box at the top.

TechEd Content and Excel

Using a couple more built-in PowerShell cmdlets, I can find all the session titles that contain the word storage and export the results to a comma-separated value file (CSV) and then open it in Excel, using Invoke-Item.

PS> .\Get-TechEdTitle.ps1 storage | Export-Csv .\storage.csv –NoTypeInformation
PS> Invoke-Item .\storage.csv

How It’s Done

The interesting parts about this script are two-fold. First, the new PowerShell v3 cmdlet Invoke-RestMethod is being used to retrieve the TechEd content catalog via the Internet. Second, Microsoft makes the catalog accessible using OData (Open Data Protocol) http://www.odata.org/. OData is an open protocol for sharing data.

The protocol allows for a consumer to query a datasource over the HTTP protocol and get the result back in formats like Atom, JSON or plain XML, including pagination, ordering or filtering of the data.

PowerShell v3 makes it super easy to consume OData feeds whether they served either up as XML or JSON format. Invoke-RestMethod auto-detects the format, consumes it and returns PowerShell objects ready for use.

One of the nice things that OData supports is the $filter system query option, allowing clients to filter the data from the URL. $filter specifies conditions that MUST be met by the target service for it to be returned.

param ([string]$Title)

$Global:baseUrl = 'http://odata.msteched.com/tena2012/sessions.svc/'
$Global:url = $baseUrl + 'Sessions'
$search = $url + "?`$filter=substringof('$($Title)', Title)"

Invoke-ODataTransform (Invoke-RestMethod $search)

Here is the Invoke-ODataTransform function. In a nutshell here is what it does. The XML data returned from the OData query contains data types of string and System.Xml.XmlElement. Strings can be used as is, when you encounter a System.Xml.XmlElement, you need to get the #text property.

Here is one way to get this done.

function Global:Invoke-ODataTransform ($records) {

    $propertyNames = ($records | Select -First 1).content.properties |
        Get-Member -MemberType Properties |
        Select -ExpandProperty name

    foreach($record in $records) {

        $h = @{}
        $h.ID = $record.ID
        $properties = $record.content.properties

        foreach($propertyName in $propertyNames) {
            $targetProperty = $properties.$propertyName
            if($targetProperty -is [System.Xml.XmlElement]) {
                $h.$propertyName = $targetProperty.'#text'
            } else {
                $h.$propertyName = $targetProperty
            }
        }

        [PSCustomObject]$h
    }
}

I encourage you to download and run the PowerShell script that uses ShowUI and displays the WPF GUI. ShowUI is a PowerShell module to help build WPF user interfaces in script. ShowUI makes the complicated world of WPF easy to use in PowerShell. You can use ShowUI to write simple WPF gadgets, quick front ends for your scripts, components, and full applications.

Filed in: Articles Tags: , , ,

One Response to "Interacting with TechEd NA 2012 Schedule using PowerShell v3"

Leave a Reply

Submit Comment

© 2016 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: