Cmdlets for SuiteCRM

Build 22.0.8462

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 SuiteCRM 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 SuiteCRMCmdlets

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

Import-Module SuiteCRMCmdlets;

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

$conn = Connect-SuiteCRM -URL 'http://mySuiteCRM.com' -User 'myUser' -Password 'myPassword'

Connecting To SuiteCRM V4.1 API

You can connect to SuiteCRM data via the V4.1 API by simply setting the following connection properties:

  • Schema: Set this to suitecrmv4.
  • Url: Set this to the URL associated with the SuiteCRM application, for example http://suite.crm.com.
  • User: Set this to the user associated with the SuiteCRM account.
  • Password: Set this to the password associated with the SuiteCRM account.

Note that retrieving SuiteCRM metadata can be expensive. It is advised that you store the metadata locally as described in Caching Metadata.

Connecting To SuiteCRM V8 API

Before Connecting

Before you connect to SuiteCRM V8 API you will need to first configure it in your SuiteCRM instance. The API can be configured in SuiteCRM version 7.10+. To configure the API, please follow the steps written in the SuiteCRM JSON API docs, found here: https://docs.suitecrm.com/developer/api/developer-setup-guide/json-api/#_before_you_start_calling_endpoints .

The SuiteCRM V8 API uses OAuth2.0 as its main method of authentication using 2 types of grant type, password or client credentials. Please see Using OAuth Authentication for a guide on how to connect with OAuth.

Retrieving Data

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

$results = Select-SuiteCRM -Connection $conn -Table "Accounts" -Columns @("Name, Industry") -Where "Industry='Manufacturing'"
The Invoke-SuiteCRM 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-SuiteCRM -Connection $conn -Table Accounts -Where "Industry = 'Manufacturing'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myAccountsData.csv -NoTypeInformation

You will notice that we piped the results from Select-SuiteCRM 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-SuiteCRM -URL 'http://mySuiteCRM.com' -User 'myUser' -Password 'myPassword'
PS C:\> $row = Select-SuiteCRM -Connection $conn -Table "Accounts" -Columns (Name, Industry) -Where "Industry = 'Manufacturing'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
  "Connection":  {

  },
  "Table":  "Accounts",
  "Columns":  [

  ],
  "Name":  "MyName",
  "Industry":  "MyIndustry"
} 

Deleting Data

The following line deletes any records that match the criteria:

Select-SuiteCRM -Connection $conn -Table Accounts -Where "Industry = 'Manufacturing'" | Remove-SuiteCRM

Modifying Data

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

Import-Csv -Path C:\MyAccountsUpdates.csv | %{
  $record = Select-SuiteCRM -Connection $conn -Table Accounts -Where ("Id = `'"+$_.Id+"`'")
  if($record){
    Update-SuiteCRM -Connection $conn -Table Accounts -Columns @("Name","Industry") -Values @($_.Name, $_.Industry) -Where "Id  = `'$_.Id`'"
  }else{
    Add-SuiteCRM -Connection $conn -Table Accounts -Columns @("Name","Industry") -Values @($_.Name, $_.Industry)
  }
}

Copyright (c) 2023 CData Software, Inc. - All rights reserved.
Build 22.0.8462