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 SAPHybrisC4C 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 SAPHybrisC4CCmdlets
The following line is then added to your profile, loading the cmdlets on the next session:
Import-Module SAPHybrisC4CCmdlets;
You can then use the Connect-SAPHybrisC4C cmdlet to create a connection object that can be passed to other cmdlets:
$conn = Connect-SAPHybrisC4C -Tenant "mytenant.crm.ondemand.com" -User "User" -Password "Password"
Connecting to SAP Cloud for Customer
To identify your SAP Cloud for Customer instance, set the following:- Tenant: The full domain name of your SAP Cloud for Customer tenant.
- Url: The URL corresponding to your SAP Cloud for Customer instance. You must specify this if it differs from the default service of "c4codata". Otherwise, omit this connection property.
Authenticating to SAP Cloud for Customer
User Credentials
Set the AuthScheme to Basic and specify the following:
- User: Your SAP Cloud for Customer username.
- Password: Your SAP Cloud for Customer password.
Azure AD
Set the AuthScheme to AzureAD.
Configuring a Custom Extension
This configuration requires a custom extension to SAP Hybris. This extension can be created using the yempty tool.
Connection Configuration
After configuring your custom extension, set the following to authenticate:
- OAuthClientId: The application Id of the connector application, found in the Overview section of the app registration.
- OAuthClientSecret: The client secret value of the connector application. Azure AD displays this when you create a new client secret.
- CallbackURL: The redirect URI of the connector application. For example: https://localhost:33333
- SSOProperties: Set the following additional properties, each separated by a commma, in the format A=B;C=D; etc.
- Resource: The URL of your environment.
- AzureTentant: The Id of the Azure AD tenant where the applications are registered.
Retrieving Data
The Select-SAPHybrisC4C cmdlet provides a native PowerShell interface for retrieving data:
$results = Select-SAPHybrisC4C -Connection $conn -Table "AccountCollection" -Columns @("ObjectID, AccountName") -Where "AccountName='MyAccount'"The Invoke-SAPHybrisC4C 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-SAPHybrisC4C -Connection $conn -Table AccountCollection -Where "AccountName <> 'MyAccount'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myAccountCollectionData.csv -NoTypeInformation
You will notice that we piped the results from Select-SAPHybrisC4C 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-SAPHybrisC4C -Tenant "mytenant.crm.ondemand.com" -User "User" -Password "Password" PS C:\> $row = Select-SAPHybrisC4C -Connection $conn -Table "AccountCollection" -Columns (ObjectID, AccountName) -Where "AccountName <> 'MyAccount'" | select -first 1 PS C:\> $row | ConvertTo-Json { "Connection": { }, "Table": "AccountCollection", "Columns": [ ], "ObjectID": "MyObjectID", "AccountName": "MyAccountName" }
Deleting Data
The following line deletes any records that match the criteria:
Select-SAPHybrisC4C -Connection $conn -Table AccountCollection -Where "AccountName = 'MyAccount'" | Remove-SAPHybrisC4C
Modifying Data
The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into SAP Cloud for Customer, checking first whether a record already exists and needs to be updated instead of inserted.
Import-Csv -Path C:\MyAccountCollectionUpdates.csv | %{ $record = Select-SAPHybrisC4C -Connection $conn -Table AccountCollection -Where ("ObjectID = `'"+$_.ObjectID+"`'") if($record){ Update-SAPHybrisC4C -Connection $conn -Table AccountCollection -Columns @("ObjectID","AccountName") -Values @($_.ObjectID, $_.AccountName) -Where "ObjectID = `'$_.ObjectID`'" }else{ Add-SAPHybrisC4C -Connection $conn -Table AccountCollection -Columns @("ObjectID","AccountName") -Values @($_.ObjectID, $_.AccountName) } }