Cmdlets for SAP HANA

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 SAPHANA 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 SAPHANACmdlets

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

Import-Module SAPHANACmdlets;

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

$conn = Connect-SAPHANA -User 'system' -Password 'myPassword' -Database 'systemdb' -Server 'myServer'

Connecting to SAP HANA Cloud

Set the following to connect to data:
  • Server: Set this property to the name or network address of the SAP HANA database instance.
  • Port: Set this to 443.
  • UseSSL: Set this to True.

Connecting to SAP HANA On-Prem

Set the following to connect to data:

  • Server: Set this property to the name or network address of the SAP HANA database instance.
  • Port: The port of the server hosting the SAP HANA database.

Authenticating to SAP HANA

Once you have provided the connection details, set the following database credentials to authenticate to SAP HANA:

  • User: The username provided for authentication with the SAP HANA database.
  • Password: The password provided for authentication with the SAP HANA database.

Okta

Set the AuthScheme to Okta. The following connection properties are used to connect to Okta:

  • User: Set this to the Okta user.
  • Password: Set this to Okta password for the user.
  • SSOProperties (optional): Set this to the OTP code that was sent to your device. This property should be used only when MFA is required for OKTA sign on.

The following SSOProperties are needed to authenticate to Okta:

  • SSOLoginUrl: Set this to the OKTA's login URL.
  • MFAType (optional): Set this to the multi-factor type. This property should be used only when MFA is required for OKTA sign on. This property accepts one of the following values:
    • OKTAVerify
    • Email
    • SMS
  • APIToken (optional): Set this to the API Token that the customer created from the Okta org. It should be used when authenticating a user via a trusted application or proxy that overrides OKTA client request context.

The following is an example connection string:

AuthScheme=OKTA;User=username;Password=password;Server=myserver;SSO Properties='SSOLoginUrl=https://cdata-okta.okta.com';

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

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

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

  },
  "Table":  ""SYSTEMDB"."DEMO".Products",
  "Columns":  [

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

Deleting Data

The following line deletes any records that match the criteria:

Select-SAPHANA -Connection $conn -Table "SYSTEMDB"."DEMO".Products -Where "ProductName = 'Konbu'" | Remove-SAPHANA

Modifying Data

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

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

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