Cmdlets for Informix

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 Informix 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 InformixCmdlets

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

Import-Module InformixCmdlets;

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

$conn = 

Connecting to Informix

Set the following properties to connect to Informix:

  • Server: Set this to the name of the server running Informix.
  • Port: Set this to the port the Informix server is listening on.
  • Database: Set this to the name of the Informix database.
  • User: Set this to the username of a user allowed to access the database.
  • Password: Set this to the password of a user allowed to access the 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-Informix cmdlet provides a native PowerShell interface for retrieving data:

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

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

  },
  "Table":  "[informix].Books",
  "Columns":  [

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

Deleting Data

The following line deletes any records that match the criteria:

Select-Informix -Connection $conn -Table [informix].Books -Where "Category = 'US'" | Remove-Informix

Modifying Data

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

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

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