Automate OData Integration Tasks from PowerShell

Jerod Johnson
Jerod Johnson
Director, Technology Evangelism
Are you in search of a quick and easy way to access OData services from PowerShell? This article demonstrates how to utilize the OData Cmdlets for tasks like connecting to OData services, automating operations, downloading data, and more.

The CData ADO.NET Provider for OData is a standard ADO.NET Provider that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to OData.

About OData Data Integration

CData simplifies access and integration of live OData services data. Our customers leverage CData connectivity to:

  • Access OData versions 2.0, 3.0, and 4.0, working with legacy services and the latest features and capabilities.
  • Leverage advanced query options, including $filter, $select, and $expand, enhancing data retrieval from 3rd party tools.
  • Use Server-side execution of aggregation and grouping to minimize data transfer and boost performance.
  • Authenticate securely using a variety of schemes, including Azure AD, digest, negotiate, NTLM, OAuth, and more means secure authentication with every connection.
  • Use SQL stored procedures to manage OData service entities - listing, creating, and removing associations between entities.

Customers use CData's solutions to regularly integrate their OData services with preferred tools, such as Power BI, MicroStrategy, or Tableau, and to replicate data from OData services to their databases or data warehouses.


Getting Started


ADO.NET Provider

The ADO.NET Provider provides a SQL interface for OData; this tutorial shows how to use the Provider to create, retrieve, update, and delete OData services.

Once you have acquired the necessary connection properties, accessing OData services in PowerShell can be enabled in three steps.

The User and Password properties, under the Authentication section, must be set to valid OData user credentials. In addition, specify a URL to a valid OData server organization root or OData services file.

  1. Load the provider's assembly:

    [Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for OData\lib\System.Data.CData.OData.dll")
  2. Connect to OData:

    $conn= New-Object System.Data.CData.OData.ODataConnection("URL=http://services.odata.org/V4/Northwind/Northwind.svc;UseIdUrl=True;OData Version=4.0;Data Format=ATOM;")
    $conn.Open()
  3. Instantiate the ODataDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT OrderName, Freight from Orders"
    
    $da= New-Object System.Data.CData.OData.ODataDataAdapter($sql, $conn)
    $dt= New-Object System.Data.DataTable
    $da.Fill($dt)
    
    $dt.Rows | foreach {
        Write-Host $_.ordername $_.freight
    }

Update OData Services

$cmd =  New-Object System.Data.CData.OData.ODataCommand("UPDATE Orders SET ShipCity='New York' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.OData.ODataParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()

Insert OData Services

$cmd =  New-Object System.Data.CData.OData.ODataCommand("INSERT INTO Orders (ShipCity) VALUES (@myShipCity)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.OData.ODataParameter("@myShipCity","New York")))
$cmd.ExecuteNonQuery()

Delete OData Services

$cmd =  New-Object System.Data.CData.OData.ODataCommand("DELETE FROM Orders WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.OData.ODataParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()

Ready to get started?

Download a free trial of the OData Data Provider to get started:

 Download Now

Learn more:

OData  Icon OData ADO.NET Provider

Easy-to-use OData client (consumer) enables developers to build .NET applications that easily communicate with OData services.