Automate SAP HANA Integration Tasks from PowerShell



Are you in search of a quick and easy way to access SAP HANA data from PowerShell? This article demonstrates how to utilize the SAP HANA Cmdlets for tasks like connecting to SAP HANA data, automating operations, downloading data, and more.

The CData Cmdlets for SAP HANA are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to SAP HANA.

PowerShell Cmdlets or ADO.NET Provider?

The Cmdlets are not only a PowerShell interface to SAP HANA, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete SAP HANA data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for SAP HANA. To access SAP HANA data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for SAP HANA.

Once you have acquired the necessary connection properties, accessing SAP HANA data in PowerShell can be enabled in three steps.

Set the Server, Database and Port properties to specify the address of your SAP Hana database to interact with. Set the User and the Password properties to authenticate to the server.

PowerShell

  1. Install the module:

    Install-Module SAPHANACmdlets
  2. Connect:

    $saphana = Connect-SAPHANA -User "$User" -Password "$Password" -Server "$Server" -Database "$Database"
  3. Search for and retrieve data:

    $name = "TestBucket" $buckets = Select-SAPHANA -Connection $saphana -Table "Buckets" -Where "Name = `'$Name`'" $buckets

    You can also use the Invoke-SAPHANA cmdlet to execute SQL commands:

    $buckets = Invoke-SAPHANA -Connection $saphana -Query 'SELECT * FROM Buckets WHERE Name = @Name' -Params @{'@Name'='TestBucket'}

ADO.NET

  1. Load the provider's assembly:

    [Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for SAP HANA\lib\System.Data.CData.SAPHANA.dll")
  2. Connect to SAP HANA:

    $conn= New-Object System.Data.CData.SAPHANA.SAPHANAConnection("User=system;Password=mypassword;Server=localhost;Database=systemdb;") $conn.Open()
  3. Instantiate the SAPHANADataAdapter, execute an SQL query, and output the results:

    $sql="SELECT Name, OwnerId from Buckets" $da= New-Object System.Data.CData.SAPHANA.SAPHANADataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.name $_.ownerid }

Update SAP HANA Data

PowerShell

Update-SAPHANA -Connection $SAPHANA -Columns @('Name','OwnerId') -Values @('MyName', 'MyOwnerId') -Table Buckets -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.SAPHANA.SAPHANACommand("UPDATE Buckets SET Name='TestBucket' WHERE Id = @myId", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.SAPHANA.SAPHANAParameter("@myId","10456255-0015501366"))) $cmd.ExecuteNonQuery()

Insert SAP HANA Data

PowerShell

Add-SAPHANA -Connection $SAPHANA -Table Buckets -Columns @("Name", "OwnerId") -Values @("MyName", "MyOwnerId")

ADO.NET

$cmd = New-Object System.Data.CData.SAPHANA.SAPHANACommand("INSERT INTO Buckets (Name) VALUES (@myName)", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.SAPHANA.SAPHANAParameter("@myName","TestBucket"))) $cmd.ExecuteNonQuery()

Delete SAP HANA Data

PowerShell

Remove-SAPHANA -Connection $SAPHANA -Table "Buckets" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.SAPHANA.SAPHANACommand("DELETE FROM Buckets WHERE Id=@myId", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.SAPHANA.SAPHANAParameter("@myId","001d000000YBRseAAH"))) $cmd.ExecuteNonQuery()

Ready to get started?

Download a free trial of the SAP HANA Data Provider to get started:

 Download Now

Learn more:

SAP HANA Icon SAP HANA ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with SAP HANA databases.