Ready to get started?

Learn more about the CData ADO.NET Provider for OData or download a free trial:

Download Now

Automate OData Integration Tasks from PowerShell

Are you looking for a quick and easy way to access OData services from PowerShell? We show how to use the Cmdlets for OData and the CData ADO.NET Provider for OData to connect to OData services and synchronize, automate, download, and more.

The CData Cmdlets for OData are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to OData.

Cmdlets or ADO.NET?

The cmdlets are not only a PowerShell interface to the OData API, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete OData services. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for OData. To access OData services from other .NET applications, like LINQPad, use the CData ADO.NET Provider for OData.

After obtaining the needed connection properties, accessing OData services in PowerShell consists of three basic steps.

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

PowerShell

  1. Install the module:

    Install-Module ODataCmdlets
  2. Connect:

    $odata = Connect-OData -URL "$URL" -UseIdUrl "$UseIdUrl" -OData Version "$OData Version" -Data Format "$Data Format"
  3. Search for and retrieve data:

    $shipcity = "New York" $orders = Select-OData -Connection $odata -Table "Orders" -Where "ShipCity = `'$ShipCity`'" $orders

    You can also use the Invoke-OData cmdlet to execute SQL commands:

    $orders = Invoke-OData -Connection $odata -Query 'SELECT * FROM Orders WHERE ShipCity = @ShipCity' -Params @{'@ShipCity'='New York'}

ADO.NET

  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

PowerShell

Update-OData -Connection $OData -Columns @('OrderName','Freight') -Values @('MyOrderName', 'MyFreight') -Table Orders -Id "MyId"

ADO.NET

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

Insert OData Services

PowerShell

Add-OData -Connection $OData -Table Orders -Columns @("OrderName", "Freight") -Values @("MyOrderName", "MyFreight")

ADO.NET

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

Delete OData Services

PowerShell

Remove-OData -Connection $OData -Table "Orders" -Id "MyId"

ADO.NET

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