Ready to get started?

Learn more about the SAP Netweaver Gateway PowerShell Cmdlets or download a free trial:

Download Now

Pipe SAP Netweaver Gateway Data to CSV in PowerShell

Use standard PowerShell cmdlets to access SAP Netweaver Gateway tables.

The CData Cmdlets Module for SAP Netweaver Gateway is a standard PowerShell module offering straightforward integration with SAP Netweaver Gateway. Below, you will find examples of using our SAPGateway Cmdlets with native PowerShell cmdlets.

Creating a Connection to Your SAP Netweaver Gateway Data

SAP Gateway allows both basic and OAuth 2.0 authentication. You can use basic authentication to connect to your own account, or you can use OAuth to enable other users to retrieve data from your service with their accounts. In addition to authenticating, set the following connection properties to access SAP Gateway tables.

  • Url: Set this to the URL of your environment, or to the full URL of the service. For example, the full URL might appear as: https://sapes5.sapdevcenter.com/sap/opu/odata/IWBEP/GWSAMPLE_BASIC/. In this example, the environment URL would just be: https://sapes5.sapdevcenter.com.
  • Namespace: Set the appropriate Service Namespace. In the example above, IWBEP is the namespace. It is optional if the full URL to the service is specified.
  • Service: Set this to the service you want to retrieve data from. In the example above, the service is GWSAMPLE_BASIC. It is not required if the full URL is specified.

Authenticate via Basic Authentication

In basic authentication, you use your login credentials to connect. Set the following properties:

  • User: This is the username you use to log in to SAP Gateway.
  • Password: This is the password you use to log in to SAP Gateway.

Authenticate via OAuth Authentication

You can connect to SAP Gateway using the embedded OAuth connectivity (without setting any additional authentication connection properties). When you connect, the OAuth endpoint opens in your browser. Log in and grant permissions to complete the OAuth process. See the OAuth section in the online Help documentation for more information on other OAuth authentication flows.

$conn = Connect-SAPGateway  -User "$User" -Password "$Password" -URL "$URL"

Selecting Data

Follow the steps below to retrieve data from the SalesOrderLineItems table and pipe the result into to a CSV file:

Select-SAPGateway -Connection $conn -Table SalesOrderLineItems | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\mySalesOrderLineItemsData.csv -NoTypeInformation

You will notice that we piped the results from Select-SAPGateway 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" in the result set, and we do not necessarily want that information in our CSV file.

The Connection, Table, and Columns are appended to the results in order to facilitate piping results from one of the CData Cmdlets directly into another one.

Deleting Data

The following line deletes any records that match the criteria:

Select-SAPGateway -Connection $conn -Table SalesOrderLineItems -Where "Quantity = 15" | Remove-SAPGateway

Inserting and Updating Data

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

Import-Csv -Path C:\MySalesOrderLineItemsUpdates.csv | %{
  $record = Select-SAPGateway -Connection $SAPGateway -Table SalesOrderLineItems -Where ("Id = `'"+$_.Id+"`'")
  if($record){
    Update-SAPGateway -Connection $sapgateway -Table SalesOrderLineItems -Columns ("ProductID","Quantity") -Values ($_.ProductID, $_.Quantity) -Where ("Id = `'"+$_.Id+"`'")
  }else{
    Add-SAPGateway -Connection $sapgateway -Table SalesOrderLineItems -Columns ("ProductID","Quantity") -Values ($_.ProductID, $_.Quantity)
  }
}

As always, our goal is to simplify the way you connect to data. With cmdlets users can install a data module, set the connection properties, and start building. Download Cmdlets and start working with your data in PowerShell today!