接続の確立
CData Cmdlets ユーザーは、データモジュールをインストールし、接続プロパティを設定してスクリプトを開始できます。このセクションでは、CSV インポートおよびエクスポートcmdlet などのネイティブPowerShell cmdlet でSAPSuccessFactorsLMS 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.
接続オブジェクトの作成
Connect-SAPSuccessFactorsLMS cmdlet を使って、別のcmdlet に渡すことができる接続オブジェクトを作成します。
$conn = Connect-SAPSuccessFactorsLMS -User "username" -UserType "admin" -CompanyId "CompanyId" -Url "https://api4.successfactors.com" -OAuthClientId "OAuthClientId" -OAuthClientSecret "OAuthClientSecret"
データの取得
Select-SAPSuccessFactorsLMS cmdlet はデータを取得するためのネイティブなPowerShell インターフェースを提供します。
$results = Select-SAPSuccessFactorsLMS -Connection $conn -Table "SampleTable_1" -Columns @("Id, Column1") -Where "Column2='Bob'"
Invoke-SAPSuccessFactorsLMS cmdlet はSQL インターフェースを提供します。このcmdlet を使うと、Query パラメータを介してSQL クエリを実行できます。
cmdlet 出力のパイプ処理
cmdlet は行オブジェクトをパイプラインに一度に一行ずつ返します。以下は、結果をCSV ファイルにエクスポートします。
Select-SAPSuccessFactorsLMS -Connection $conn -Table SampleTable_1 -Where "Column2 = 'Bob'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\mySampleTable_1Data.csv -NoTypeInformation
Select-SAPSuccessFactorsLMS からの結果をSelect-Object cmdlet にパイプして、Export-CSV cmdlet にパイプする前にいくつかのプロパティを実行していることがわかるでしょう。これをする理由は、CData Cmdlets は接続、テーブル、およびカラムの情報を結果セットの各行オブジェクトに追加しますが、必ずしもその情報がCSV ファイルに必要ではないからです。
ただし、これによってcmdlet の出力を別のcmdlet にパイプすることが容易になります。以下に、結果セットを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"
}
データの削除
以下は、抽出条件に合うあらゆるレコードを削除します。
Select-SAPSuccessFactorsLMS -Connection $conn -Table SampleTable_1 -Where "Column2 = 'Bob'" | Remove-SAPSuccessFactorsLMS
データの変更
cmdlet はデータクレンジング同様、データの変換を容易にします。次の例は、レコードがすでに存在するかどうか、挿入する前に更新が必要かどうかをチェックしてから、CSV ファイルのデータをSAPSuccessFactorsLMS にロードします。
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)
}
}