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 SAPSuccessFactorsLMS Cmdlets with native PowerShell cmdlets, like the CSV import and export cmdlets.
Connecting to SAPSuccessFactorsLMS
Before you can connect to SAPSuccessFactorsLMS, you must configure the OAuth application tied to your SAPSuccessFactorsLMS account. See Creating a Custom OAuth App for a procedure.After you have configured your OAuth application, set the following to connect:
- URL: The base URL of your SAP SuccessFactors instance. You can find this value in the URL of your SAP SuccessFactors homepage before the first forward slash (/). For example: https://mycompany.successfactors.eu
- User: The username you use to log in to your SAP SuccessFactors account.
- CompanyId: The unique name assigned to your SAPSuccessFactorsLMS tenant. You can find this value in the URL of your SAP SuccessFactors homepage, between "https://" and ".successfactors" (or ".sapsf" in the case of preview instances).
- UserType: The type of user being used. Select either Admin if your account has the admin role or User if it does not.
- OAuthClientId: The value of the Client ID field, as noted during the configuration of your OAuth application.
- OAuthClientSecret: The value of the Newly Generated Client Secret, as noted during the configuration of your OAuth application.
Creating a Connection Object
You can then use the Connect-SAPSuccessFactorsLMS cmdlet to create a connection object that can be passed to other cmdlets:
$conn = Connect-SAPSuccessFactorsLMS -User "username" -UserType "admin" -CompanyId "CompanyId" -Url "https://api4.successfactors.com" -OAuthClientId "OAuthClientId" -OAuthClientSecret "OAuthClientSecret"
Retrieving Data
The Select-SAPSuccessFactorsLMS cmdlet provides a native PowerShell interface for retrieving data:
$results = Select-SAPSuccessFactorsLMS -Connection $conn -Table "SampleTable_1" -Columns @("Id, Column1") -Where "Column2='Bob'"
The Invoke-SAPSuccessFactorsLMS 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-SAPSuccessFactorsLMS -Connection $conn -Table SampleTable_1 -Where "Column2 = 'Bob'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\mySampleTable_1Data.csv -NoTypeInformation
You will notice that we piped the results from Select-SAPSuccessFactorsLMS 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-SAPSuccessFactorsLMS -User "username" -UserType "admin" -CompanyId "CompanyId" -Url "https://api4.successfactors.com" -OAuthClientId "OAuthClientId" -OAuthClientSecret "OAuthClientSecret"
PS C:\> $row = Select-SAPSuccessFactorsLMS -Connection $conn -Table "SampleTable_1" -Columns (Id, Column1) -Where "Column2 = 'Bob'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
"Connection": {
},
"Table": "SampleTable_1",
"Columns": [
],
"Id": "MyId",
"Column1": "MyColumn1"
}
Deleting Data
The following line deletes any records that match the criteria:
Select-SAPSuccessFactorsLMS -Connection $conn -Table SampleTable_1 -Where "Column2 = 'Bob'" | Remove-SAPSuccessFactorsLMS
Modifying Data
The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into SAPSuccessFactorsLMS, checking first whether a record already exists and needs to be updated instead of inserted.
Import-Csv -Path C:\MySampleTable_1Updates.csv | %{
$record = Select-SAPSuccessFactorsLMS -Connection $conn -Table SampleTable_1 -Where ("Id = `'"+$_.Id+"`'")
if($record){
Update-SAPSuccessFactorsLMS -Connection $conn -Table SampleTable_1 -Columns @("Id","Column1") -Values @($_.Id, $_.Column1) -Where "Id = `'$_.Id`'"
}else{
Add-SAPSuccessFactorsLMS -Connection $conn -Table SampleTable_1 -Columns @("Id","Column1") -Values @($_.Id, $_.Column1)
}
}