Cmdlets for OData

Build 20.0.7587

Establishing a Connection

With the CData Cmdlets users can install a data module, set the connection properties, and start scripting. This section provides examples of using our OData Cmdlets with native PowerShell cmdlets, like the CSV import and export cmdlets.

Installing and Connecting

If you have PSGet, installing the cmdlets can be accomplished from the PowerShell Gallery with the following command. You can also obtain a setup from the CData site.

Install-Module ODataCmdlets

The following line is then added to your profile, loading the cmdlets on the next session:

Import-Module ODataCmdlets;

You can then use the Connect-OData cmdlet to create a connection object that can be passed to other cmdlets:

$conn = Connect-OData -User 'MyUser' -Password 'MyPassword' -URL 'http://myserver/myOrgRoot'

Connecting to OData

To connect, you need to set the Url to a valid OData service root URI in addition to the authentication values.

Also, you can specify a CacheLocation to store the metadata of your OData organization. This keeps the CData Cmdlets PowerShell Module for OData from having to send requests for metadata on each connection.

Authenticating to OData

The cmdlet supports the major authentication schemes, including HTTP and Windows.

Set AuthScheme to use the following authentication types.

  • HTTP Authentication: The cmdlet supports authentication with HTTP Basic, Digest, and custom headers. To use Basic or Digest, set the User and Password. You can specify other authentication values in CustomHeaders.
  • Windows (NTLM): Set the Windows User and Password to connect and set AuthScheme to "NTLM".
  • Kerberos and Kerberos Delegation: To authenticate with Kerberos, set AuthScheme to NEGOTIATE. To use Kerberos Delegation, set AuthScheme to KERBEROSDELEGATION. If needed, provide the User, Password, and KerberosSPN. By default, the cmdlet attempts to communicate with the SPN at the specified Url.
The cmdlet simplifies OAuth configuration. See Using OAuth Authentication for a how-to.

Securing OData Connections

By default, the cmdlet attempts to negotiate SSL/TLS by checking the server's certificate against the system's trusted certificate store. To specify another certificate, see the SSLServerCert property for the available formats to do so.

Retrieving Data

The Select-OData cmdlet provides a native PowerShell interface for retrieving data:

$results = Select-OData -Connection $conn -Table "Lead" -Columns @("Id, FullName") -Where "FirstName='Bartholomew'"
The Invoke-OData cmdlet provides an SQL interface. This cmdlet can be used to execute an SQL query via the Query parameter.

Piping Cmdlet Output

The cmdlets return row objects to the pipeline one row at a time. The following line exports results to a CSV file:

Select-OData -Connection $conn -Table Lead -Where "FirstName <> 'Bartholomew'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myLeadData.csv -NoTypeInformation

You will notice that we piped the results from Select-OData into a Select-Object cmdlet and excluded some properties before piping them into an Export-CSV cmdlet. We do this because the CData Cmdlets append Connection, Table, and Columns information onto each row object in the result set, and we do not necessarily want that information in our CSV file.

However, this makes it easy to pipe the output of one cmdlet to another. The following is an example of converting a result set to JSON:

PS C:\> $conn  = Connect-OData -User 'MyUser' -Password 'MyPassword' -URL 'http://myserver/myOrgRoot'
PS C:\> $row = Select-OData -Connection $conn -Table "Lead" -Columns (Id, FullName) -Where "FirstName <> 'Bartholomew'" | select -first 1
PS C:\> $row | ConvertTo-Json
  "Connection":  {

  "Table":  "Lead",
  "Columns":  [

  "Id":  "MyId",
  "FullName":  "MyFullName"

Deleting Data

The following line deletes any records that match the criteria:

Select-OData -Connection $conn -Table Lead -Where "FirstName = 'Bartholomew'" | Remove-OData

Updating Data

The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into OData, checking first whether a record already exists and needs to be updated instead of inserted.

Import-Csv -Path C:\MyLeadUpdates.csv | %{
  $record = Select-OData -Connection $conn -Table Lead -Where ("Id = `'"+$_.Id+"`'")
    Update-OData -Connection $conn -Table Lead -Columns @("Id","FullName") -Values @($_.Id, $_.FullName) -Where "Id  = `'$_.Id`'"
    Add-OData -Connection $conn -Table Lead -Columns @("Id","FullName") -Values @($_.Id, $_.FullName)

Copyright (c) 2020 CData Software, Inc. - All rights reserved.
Build 20.0.7587