Cmdlets for OData

Build 20.0.7654

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 following authentication schemes.

HTTP Auth Schemes

The following general HTTP Auth schemes are supported:

  • None: If no authentication is required, set the AuthScheme to None.
  • Basic: If Basic Auth is supported, set the AuthScheme to Basic. In addition, set the User and Password.
  • NTLM: A type of Windows authentication often used across a LAN using your Windows user credentials. Set the AuthScheme to NTLM to support this method of authentication. In addition, set the User and Password if you are not connecting from a Windows machine, or your currently logged in user account should not be used for the connection.
  • Digest: If Digest Auth is supported, set the AuthScheme to Digest. In addition, set the User and Password.

Kerberos Authentication

Please see Using Kerberos for a description of how to support Kerberos authentication.

Authenticating with OAuth

Set the AuthScheme to OAuth. See Using OAuth Authentication for an authentication guide.

Authenticating with AzureAD

AzureAD is a form of OAuth that goes through Azure. Set the AuthScheme to AzureAD. The CData Cmdlets PowerShell Module for OData will internally automatically take care of known Azure URLs. Specifically, the following are unnecessary to specify with the AzureAD AuthScheme:

  • OAuthAccessTokenURL
  • OAuthAuthorizationURL
  • OAuthRefreshTokenURL
  • OAuthRequestTokenURL

Other connection properties may be required for this connection method including:

  • Scope: Must be specified if InitiateOAuth is set to GETANDREFRESH as the Scope is submitted to Microsoft during retrieval of credentails. This will vary depending on the service, but is generally a combination of the resource (hostname in the URL) and permission name. For example: https://host/user_impersonation.
  • AzureADTenant: The specific Azure Tenant to authenticate against during Microsoft login. If none is specified, your user account's default tenant via the common login endpoint will be used. This may not be correct depending on the specific resource you are connecting to. For example, if the resource is stored on a seperate tenant for cases where you have access to multiple tenants.

Otherwise, the steps are identical to the Using OAuth Authentication guide.

Authenticating with SharePoint Online

SharePoint Online connections may be established by retrieving a SharePoint Online cookie. Specify the following connection properties to authenticate:

  • AuthScheme: Set this to SharePointOnline.
  • User: Set this to your SharePoint Online user account.
  • Password: Set this to your SharePoint Online password.

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.7654