Cmdlets for IBM Cloud SQL Query

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 IBMCloudSQLQuery 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 IBMCloudSQLQueryCmdlets

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

Import-Module IBMCloudSQLQueryCmdlets;

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

$conn = Connect-IBMCloudSQLQuery -ApiKey "MyAPIKey" -InstanceCRN "myInstanceCRN" -Region "myRegion" -Schema "mySchema" -OAuthClientId "myOAuthClientId" -OAuthClientSecret "myOAuthClientSecret"

Authenticate via OAuth

OAuth authentication does not require any interaction with the browser to use the IBM Cloud SQL Query. The cmdlet facilitates this process as described in Connecting to IBM Cloud

Retrieving Data

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

$results = Select-IBMCloudSQLQuery -Connection $conn -Table "Jobs" -Columns @("Id, Status") -Where "UserId=''"
The Invoke-IBMCloudSQLQuery 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-IBMCloudSQLQuery -Connection $conn -Table Jobs -Where "UserId = ''" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myJobsData.csv -NoTypeInformation

You will notice that we piped the results from Select-IBMCloudSQLQuery 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-IBMCloudSQLQuery -ApiKey "MyAPIKey" -InstanceCRN "myInstanceCRN" -Region "myRegion" -Schema "mySchema" -OAuthClientId "myOAuthClientId" -OAuthClientSecret "myOAuthClientSecret"
PS C:\> $row = Select-IBMCloudSQLQuery -Connection $conn -Table "Jobs" -Columns (Id, Status) -Where "UserId = ''" | select -first 1
PS C:\> $row | ConvertTo-Json
  "Connection":  {

  "Table":  "Jobs",
  "Columns":  [

  "Id":  "MyId",
  "Status":  "MyStatus"

Deleting Data

The following line deletes any records that match the criteria:

Select-IBMCloudSQLQuery -Connection $conn -Table Jobs -Where "UserId = ''" | Remove-IBMCloudSQLQuery

Updating Data

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

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

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