Cmdlets for Odoo

Build 24.0.9060

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 Odoo 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 OdooCmdlets

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

Import-Module OdooCmdlets;

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

$conn = Connect-Odoo -User 'MyUser' -APIToken 'MyToken' -URL 'https://MyOdooSite/' -Database 'MyDatabase'

Connecting to Odoo

To connect, set the Url to a valid Odoo site, User and APIToken to the connection details of the user you are connecting with, and Database to the Odoo database.

If you are not using API tokens (they are only supported in Odoo 14 and above), you can also provide the password in the APIToken field.

Access Rights

In order for the cmdlet to determine what models you can access in Odoo, the user you connect with must have permissions to read from "ir.model.access" (an internal Odoo model that governs access rights). Normally this is reserved for administrators, but it can be granted to any user by creating a service group:
  1. As an administrator, open the Odoo settings page and enable "developer mode". If this is not available you may need to login as the superuser instead.
  2. Open the Groups page (under the Users dropdown) and create a new group
  3. Set the Application to "Administration" and the name to "Service Access"
  4. Add any users who need service access in the Users tab
  5. In the Access Rights tab, add an entry for the "ir.model.access" object (it may be called "Model Access"), check Read Access, and give it the name "Inspect Models"
  6. Save the group

If making this change is not possible, then you should set the CheckPermissions option to false. That will list all models in Odoo as tables, regardless of what permissions your user actually has for those models.

Retrieving Data

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

$results = Select-Odoo -Connection $conn -Table "res_users" -Columns @("name, email") -Where "company_name='Company Inc.'"
The Invoke-Odoo 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-Odoo -Connection $conn -Table res_users -Where "company_name = 'Company Inc.'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myres_usersData.csv -NoTypeInformation

You will notice that we piped the results from Select-Odoo 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-Odoo -User 'MyUser' -APIToken 'MyToken' -URL 'https://MyOdooSite/' -Database 'MyDatabase'
PS C:\> $row = Select-Odoo -Connection $conn -Table "res_users" -Columns (name, email) -Where "company_name = 'Company Inc.'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
  "Connection":  {

  },
  "Table":  "res_users",
  "Columns":  [

  ],
  "name":  "Myname",
  "email":  "Myemail"
} 

Deleting Data

The following line deletes any records that match the criteria:

Select-Odoo -Connection $conn -Table res_users -Where "company_name = 'Company Inc.'" | Remove-Odoo

Modifying Data

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

Import-Csv -Path C:\Myres_usersUpdates.csv | %{
  $record = Select-Odoo -Connection $conn -Table res_users -Where ("id = `'"+$_.id+"`'")
  if($record){
    Update-Odoo -Connection $conn -Table res_users -Columns @("name","email") -Values @($_.name, $_.email) -Where "id  = `'$_.id`'"
  }else{
    Add-Odoo -Connection $conn -Table res_users -Columns @("name","email") -Values @($_.name, $_.email)
  }
}

Copyright (c) 2024 CData Software, Inc. - All rights reserved.
Build 24.0.9060