Cmdlets for Sybase

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 Sybase 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 SybaseCmdlets

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

Import-Module SybaseCmdlets;

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

$conn = Connect-Sybase -User 'myUser' -Password 'myPassword' -Database 'NorthWind' -Server 'myServer'

Connecting to Sybase

To connect to the Sybase, specify the following connection properties:

  • Server: Set this to the name or network address of the Sybase database instance.
  • Database: Set this to the name of the Sybase database running on the specified Server.

Optionally, you can also secure your connections with TLS/SSL by setting UseSSL to true.

Authenticating to Sybase

Sybase supports several methods for authentication including basic, Kerberos, and, LDAP.

Basic

Set the AuthScheme to Basic and set the following connection properties to use Sybase authentication.

  • User: Set this to the username of the authenticating Sybase user.
  • Password: Set this to the username of the authenticating Sybase user.

LDAP

To connect with LDAP authentication, you will need to configure Sybase server-side to use the LDAP authentication mechanism.

After configuring Sybase for LDAP, you can connect using the same credentials as basic authentication.

Kerberos

To leverage Kerberos authentication, begin by enabling it via the following connection property:

  • AuthScheme: Set to Kerberos will be used for authentication to Sybase.
See Using Kerberos information regarding the connection properties that need to be set for Kerberos authentication.

You can find an example connection string below:

Server=MyServer;Port=MyPort;User=SampleUser;Password=SamplePassword;Database=MyDB;Kerberos=true;KerberosKDC=MyKDC;KerberosRealm=MYREALM.COM;KerberosSPN=server-name

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-Sybase cmdlet provides a native PowerShell interface for retrieving data:

$results = Select-Sybase -Connection $conn -Table "[master].[dbo].Products" -Columns @("Id, ProductName") -Where "ProductName='Konbu'"
The Invoke-Sybase 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-Sybase -Connection $conn -Table [master].[dbo].Products -Where "ProductName = 'Konbu'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\my[master].[dbo].ProductsData.csv -NoTypeInformation

You will notice that we piped the results from Select-Sybase 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-Sybase -User 'myUser' -Password 'myPassword' -Database 'NorthWind' -Server 'myServer'
PS C:\> $row = Select-Sybase -Connection $conn -Table "[master].[dbo].Products" -Columns (Id, ProductName) -Where "ProductName = 'Konbu'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
  "Connection":  {

  },
  "Table":  "[master].[dbo].Products",
  "Columns":  [

  ],
  "Id":  "MyId",
  "ProductName":  "MyProductName"
} 

Deleting Data

The following line deletes any records that match the criteria:

Select-Sybase -Connection $conn -Table [master].[dbo].Products -Where "ProductName = 'Konbu'" | Remove-Sybase

Modifying Data

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

Import-Csv -Path C:\My[master].[dbo].ProductsUpdates.csv | %{
  $record = Select-Sybase -Connection $conn -Table [master].[dbo].Products -Where ("Id = `'"+$_.Id+"`'")
  if($record){
    Update-Sybase -Connection $conn -Table [master].[dbo].Products -Columns @("Id","ProductName") -Values @($_.Id, $_.ProductName) -Where "Id  = `'$_.Id`'"
  }else{
    Add-Sybase -Connection $conn -Table [master].[dbo].Products -Columns @("Id","ProductName") -Values @($_.Id, $_.ProductName)
  }
}

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