Windows PowerShell 4.0 Examples – How to fetch data exposed by OData services

Last Updated on

The Invoke-RestMethod cmdlet sends an HTTP(S) request to a REST-compliant web service. You can use it to consume data exposed by, for example, the OData service for TechEd North America 2014 content. Put information about TechEd NA 2014 sessions into a $sessions variable. Let’s iterate through that collection of XML elements and create custom PowerShell objects using [PSCustomObject]. Pipe the output to an Out- GridView command specifying the PassThru parameter to send selected items from the interactive window down the pipeline, as input to an Export-Csv command. (While you are in a grid view window, try to filter only PowerShell Hands-on Labs, for example). If you have Excel installed the last command opens a CSV file in Excel.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$sessions = Invoke-RestMethod https://odata.eventpointdata.com/tena2014/
Sessions.svc/Sessions
$sessions | ForEach-Object {
$properties = $_.content.properties
[PSCustomObject]@{
Code = $properties.Code
Day = $properties.Day_US
StartTime = $properties.Start_US
EndTime = $properties.Finish_US
Speaker = $properties.PrimarySpeaker
Title = $properties.Title
Abstract = $properties.Abstract
}
} | Out-GridView -PassThru | Export-Csv $env:TEMP\sessions.csv -NoTypeInformation
Invoke-Item $env:TEMP\sessions.csv
Lima

About the author

Lima is the visual nautical indicator for "stop instantly."

Leave a Reply