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 SybaseIQ 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 SybaseIQCmdlets
The following line is then added to your profile, loading the cmdlets on the next session:
Import-Module SybaseIQCmdlets;
You can then use the Connect-SybaseIQ cmdlet to create a connection object that can be passed to other cmdlets:
$conn = Connect-SybaseIQ -User 'myUser' -Password 'myPassword' -Database 'NorthWind' -Server 'myServer'
Connecting to SybaseIQ
To connect to the SybaseIQ, specify the following connection properties:
- Server: Set this to the name or network address of the SybaseIQ or SAP SQL Anywhere database instance.
- Database: Set this to the name of the SybaseIQ or SAP SQL Anywhere database running on the specified Server.
Optionally, you can also secure your connections with TLS/SSL by setting UseSSL to true.
Note: It is also possible to connect to an instance of SAP SQL Anywhere with the above cmdlet configuration.
Authenticating to SybaseIQ
SybaseIQ supports several methods for authentication including basic, Kerberos, and, LDAP.
Basic
Set the AuthScheme to Basic and set the following connection properties to use SybaseIQ authentication.
- User: Set this to the username of the authenticating SybaseIQ user.
- Password: Set this to the username of the authenticating SybaseIQ user.
LDAP
To connect with LDAP authentication, you will need to configure SybaseIQ server-side to use the LDAP authentication mechanism.
After configuring SybaseIQ 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 SybaseIQ.
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-SybaseIQ cmdlet provides a native PowerShell interface for retrieving data:
$results = Select-SybaseIQ -Connection $conn -Table "[master].[dbo].Products" -Columns @("Id, ProductName") -Where "ProductName='Konbu'"The Invoke-SybaseIQ 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-SybaseIQ -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-SybaseIQ 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-SybaseIQ -User 'myUser' -Password 'myPassword' -Database 'NorthWind' -Server 'myServer' PS C:\> $row = Select-SybaseIQ -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-SybaseIQ -Connection $conn -Table [master].[dbo].Products -Where "ProductName = 'Konbu'" | Remove-SybaseIQ
Modifying Data
The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into SybaseIQ, 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-SybaseIQ -Connection $conn -Table [master].[dbo].Products -Where ("Id = `'"+$_.Id+"`'") if($record){ Update-SybaseIQ -Connection $conn -Table [master].[dbo].Products -Columns @("Id","ProductName") -Values @($_.Id, $_.ProductName) -Where "Id = `'$_.Id`'" }else{ Add-SybaseIQ -Connection $conn -Table [master].[dbo].Products -Columns @("Id","ProductName") -Values @($_.Id, $_.ProductName) } }