Cmdlets for DB2

Build 23.0.8839

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 DB2 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 DB2Cmdlets

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

Import-Module DB2Cmdlets;

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

$conn = 

Connecting to DB2

Set the following properties to connect:
  • Server: The name of the server running DB2.
  • Port: The port the DB2 server is listening on.
  • Database: The name of the DB2 database.

Authenticating to DB2

The cmdlet supports authenticating directly to the DB2 with user credentials as well as authenticating using the API key of an application integrated with DB2, such as Watson Query.

DB2 User Credentials

Set AuthScheme to USRIDPWD. Provide the following credentials:

  • User: The username of a user with access to the database.
  • Password: The password of a user with access to the database.

IAM

The cmdlet supports authenticating to the DB2 server using the API key of an application that connects to it, such as Watson Query.

Set AuthScheme to IBMIAMAuth. Provide the following credentials:

  • User: The IBMid or service ID tied to a user of the DB2 server.
  • Password: The API key associated with the application that requires access to the DB2 database.

Retrieving Data

After you have created a connection, you can use the other cmdlets to perform operations that you would normally expect to be able to perform against a relational database. The Select-DB2 cmdlet provides a native PowerShell interface for retrieving data:

$results = Select-DB2 -Connection $conn -Table ""Sample"."DB2INST1".Books" -Columns @("Id, Author") -Where "Category='US'"
The Invoke-DB2 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-DB2 -Connection $conn -Table "Sample"."DB2INST1".Books -Where "Category = 'US'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\my"Sample"."DB2INST1".BooksData.csv -NoTypeInformation

You will notice that we piped the results from Select-DB2 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  = 
PS C:\> $row = Select-DB2 -Connection $conn -Table ""Sample"."DB2INST1".Books" -Columns (Id, Author) -Where "Category = 'US'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
  "Connection":  {

  },
  "Table":  ""Sample"."DB2INST1".Books",
  "Columns":  [

  ],
  "Id":  "MyId",
  "Author":  "MyAuthor"
} 

Deleting Data

The following line deletes any records that match the criteria:

Select-DB2 -Connection $conn -Table "Sample"."DB2INST1".Books -Where "Category = 'US'" | Remove-DB2

Modifying Data

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

Import-Csv -Path C:\My"Sample"."DB2INST1".BooksUpdates.csv | %{
  $record = Select-DB2 -Connection $conn -Table "Sample"."DB2INST1".Books -Where ("Id = `'"+$_.Id+"`'")
  if($record){
    Update-DB2 -Connection $conn -Table "Sample"."DB2INST1".Books -Columns @("Id","Author") -Values @($_.Id, $_.Author) -Where "Id  = `'$_.Id`'"
  }else{
    Add-DB2 -Connection $conn -Table "Sample"."DB2INST1".Books -Columns @("Id","Author") -Values @($_.Id, $_.Author)
  }
}

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