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 Teradata 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 TeradataCmdlets
The following line is then added to your profile, loading the cmdlets on the next session:
Import-Module TeradataCmdlets;
You can then use the Connect-Teradata cmdlet to create a connection object that can be passed to other cmdlets:
$conn = Connect-Teradata -User 'Admin' -Password 'test123' -Database 'Northwind' -DataSource '127.0.0.1'
Deploying the Provider
To connect using the CData Cmdlets PowerShell Module for Teradata, you must install the Teradata .NET Data Provider into the GAC.
Connecting to Teradata
The cmdlet wraps the official Teradata Database driver. You can connect to the CData Cmdlets PowerShell Module for Teradata using the same connection properties, and access the same functionality, as the underlying Teradata driver.
Required Properties
To connect to Teradata, set these properties:
- AuthScheme: Specify your Teradata server's required authentication mechanism: either TD2 (default) or LDAP.
- User: Teradata user username.
- Password: Teradata user password.
- DataSource: Specify the Teradata server name, DBC Name, or TDPID.
- Port: Specify the port the server is running on.
- Database: Specify the database name. If not specified, the cmdlet connects to the default database.
TLS/SSL Configuration
To encrypt connections with TLS/SSL, enable DataEncryption.
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-Teradata cmdlet provides a native PowerShell interface for retrieving data:
$results = Select-Teradata -Connection $conn -Table ""CData"."dbo".NorthwindProducts" -Columns @("ProductId, ProductName") -Where "CategoryId='5'"The Invoke-Teradata 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-Teradata -Connection $conn -Table "CData"."dbo".NorthwindProducts -Where "CategoryId = '5'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\my"CData"."dbo".NorthwindProductsData.csv -NoTypeInformation
You will notice that we piped the results from Select-Teradata 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-Teradata -User 'Admin' -Password 'test123' -Database 'Northwind' -DataSource '127.0.0.1' PS C:\> $row = Select-Teradata -Connection $conn -Table ""CData"."dbo".NorthwindProducts" -Columns (ProductId, ProductName) -Where "CategoryId = '5'" | select -first 1 PS C:\> $row | ConvertTo-Json { "Connection": { }, "Table": ""CData"."dbo".NorthwindProducts", "Columns": [ ], "ProductId": "MyProductId", "ProductName": "MyProductName" }
Deleting Data
The following line deletes any records that match the criteria:
Select-Teradata -Connection $conn -Table "CData"."dbo".NorthwindProducts -Where "CategoryId = '5'" | Remove-Teradata
Modifying Data
The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into Teradata, checking first whether a record already exists and needs to be updated instead of inserted.
Import-Csv -Path C:\My"CData"."dbo".NorthwindProductsUpdates.csv | %{ $record = Select-Teradata -Connection $conn -Table "CData"."dbo".NorthwindProducts -Where ("ProductId = `'"+$_.ProductId+"`'") if($record){ Update-Teradata -Connection $conn -Table "CData"."dbo".NorthwindProducts -Columns @("ProductId","ProductName") -Values @($_.ProductId, $_.ProductName) -Where "ProductId = `'$_.ProductId`'" }else{ Add-Teradata -Connection $conn -Table "CData"."dbo".NorthwindProducts -Columns @("ProductId","ProductName") -Values @($_.ProductId, $_.ProductName) } }