Cmdlets for SAP Gateway

Build 22.0.8479

接続の確立

CData Cmdlets ユーザーは、データモジュールをインストールし、接続プロパティを設定してスクリプトを開始できます。このセクションでは、CSV インポートおよびエクスポートcmdlet などのネイティブPowerShell cmdlet でSAPGateway Cmdlets を使用する例を示します。

インストールおよび接続

PSGet がある場合は、PowerShell Gallery から次のコマンドを使ってcmdlet をインストールできます。CData サイトからセットアップを取得することもできます。

Install-Module SAPGatewayCmdlets

プロファイルに以下を追加すると、次のセッションでcmdlet がロードされます。

Import-Module SAPGatewayCmdlets;

Connect-SAPGateway cmdlet を使って、別のcmdlet に渡すことができる接続オブジェクトを作成します。

$conn = Connect-SAPGateway -User "username" -Password "password" -URL "https://sapes5.sapdevcenter.com/sap/opu/odata/IWBEP/GWSAMPLE_BASIC/"

SAP Gateway への接続

次の接続プロパティを設定してSAP Gateway テーブルにアクセスします。

  • Urlお使いの環境のURL、またはサービスの完全URL に設定。例えば、完全URL は次のようになります:https://sapes5.sapdevcenter.com/sap/opu/odata/IWBEP/GWSAMPLE_BASIC/。この例では、環境URL は次のようになります:https://sapes5.sapdevcenter.com。
  • Namespace適切なService Namespace を設定。先ほどの例では、IWBEP が名前空間です。サービスへの完全URL が指定されている場合は任意です。
  • Serviceデータを取得するサービスに設定。先ほどの例では、サービスはGWSAMPLE_BASIC です。完全URL が指定されている場合は必須ではありません。
  • CustomUrlParamsSet this to any required additional properties that need to be included with the HTTP request, for example sap-client=001&sap-language=EN.

SAP Gateway への認証

SAP Gateway はBasic 認証とOAuth 2.0 認証の両方を許可します。Basic 認証を使用して自分のアカウントに接続するか、OAuth を使用して他のユーザーが彼らのアカウントでサービスからデータを取得できるようにすることができます。

Basic

Basic 認証では、自分のログイン資格情報を使用して接続します。次のプロパティを設定します。

  • AuthScheme Basic に設定。
  • User これはSAP Gateway へのログインに使用するユーザー名です。
  • Password これはSAP Gateway へのログインに使用するパスワードです。

OAuth

ユーザー資格情報の接続プロパティを設定せずに接続できます。

接続すると、本製品 はデフォルトブラウザでSAP Gateway OAuth エンドポイントを開きます。ログインして、本製品 にアクセス許可を与えます。本製品 が以下のOAuth プロセスを完了します。

他のOAuth 認証フローについては、OAuth 認証の使用 を参照してください。

API キー

APIKey プロパティを設定するだけで、API キーを使用した接続が可能です。

In order to acquire an APIKey, open the Developer Portal and select the Product or Service for which you have published the API. Then, click on Subscribe > Create a new Application. After filling the necessary information and creating the application, the Application Key is going to be displayed. You can copy this value into the APIKey connection property.

You may set the AuthScheme to Token to enforce the Token based Authentication scheme.

データの取得

Select-SAPGateway cmdlet はデータを取得するためのネイティブなPowerShell インターフェースを提供します。

$results = Select-SAPGateway -Connection $conn -Table "SampleTable_1" -Columns @("Id, Column1") -Where "Column2='Bob'"
Invoke-SAPGateway cmdlet はSQL インターフェースを提供します。このcmdlet を使うと、Query パラメータを介してSQL クエリを実行できます。

cmdlet 出力のパイプ処理

cmdlet は行オブジェクトをパイプラインに一度に一行ずつ返します。以下は、結果をCSV ファイルにエクスポートします。

Select-SAPGateway -Connection $conn -Table SampleTable_1 -Where "Column2 = 'Bob'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\mySampleTable_1Data.csv -NoTypeInformation

Select-SAPGateway からの結果をSelect-Object cmdlet にパイプして、Export-CSV cmdlet にパイプする前にいくつかのプロパティを実行していることがわかるでしょう。これをする理由は、CData Cmdlets は接続、テーブル、およびカラムの情報を結果セットの各行オブジェクトに追加しますが、必ずしもその情報がCSV ファイルに必要ではないからです。

ただし、これによってcmdlet の出力を別のcmdlet にパイプすることが容易になります。以下に、結果セットをJSON に変換する例を示します。

 
PS C:\> $conn  = Connect-SAPGateway -User "username" -Password "password" -URL "https://sapes5.sapdevcenter.com/sap/opu/odata/IWBEP/GWSAMPLE_BASIC/"
PS C:\> $row = Select-SAPGateway -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-SAPGateway -Connection $conn -Table SampleTable_1 -Where "Column2 = 'Bob'" | Remove-SAPGateway

データの変更

cmdlet はデータクレンジング同様、データの変換を容易にします。次の例は、レコードがすでに存在するかどうか、挿入する前に更新が必要かどうかをチェックしてから、CSV ファイルのデータをSAP Gateway にロードします。

Import-Csv -Path C:\MySampleTable_1Updates.csv | %{
  $record = Select-SAPGateway -Connection $conn -Table SampleTable_1 -Where ("Id = `'"+$_.Id+"`'")
  if($record){
    Update-SAPGateway -Connection $conn -Table SampleTable_1 -Columns @("Id","Column1") -Values @($_.Id, $_.Column1) -Where "Id  = `'$_.Id`'"
  }else{
    Add-SAPGateway -Connection $conn -Table SampleTable_1 -Columns @("Id","Column1") -Values @($_.Id, $_.Column1)
  }
}

Copyright (c) 2023 CData Software, Inc. - All rights reserved.
Build 22.0.8479