接続の確立
CData Cmdlets ユーザーは、データモジュールをインストールし、接続プロパティを設定してスクリプトを開始できます。このセクションでは、CSV インポートおよびエクスポートcmdlet などのネイティブPowerShell cmdlet でAcumatica Cmdlets を使用する例を示します。
インストールおよび接続
PSGet がある場合は、PowerShell Gallery から次のコマンドを使ってcmdlet をインストールできます。CData サイトからセットアップを取得することもできます。
Install-Module AcumaticaCmdlets
プロファイルに以下を追加すると、次のセッションでcmdlet がロードされます。
Import-Module AcumaticaCmdlets;
Connect-Acumatica cmdlet を使って、別のcmdlet に渡すことができる接続オブジェクトを作成します。
$conn = Connect-Acumatica -OAuthClientId 'MyApplicationId' -OAuthClientSecret 'MySecretKey' -OAuthCallbackURL 'http://localhost:33333'
Connecting to Acumatica
In order to connect to the Acumatica data source, you must specify the following connection properties.
- Url: (Required) The base URL for the Acumatica ERP instance. For example: https://domain.acumatica.com/entity/.
- Schema: (Optional) There are two schemas which contains different data. The default one is REST, which uses the Acumatica REST Contract-Based API, and the OData schema, which uses the Acumatica OData API. The OData schema is used to query Acumatica Generic Inquiries.
- Company: (Partially required) Set this to the name of your company or tenant. It is required if Schema is set to OData.
- EndpointVersion: (Optional) The version of the Web Services endpoint. For example: 17.200.001. This applies only to the REST schema.
- EndpointName: (Optional) The name of the Web Services endpoint. For example: Default. This is applies only to the REST schema.
To find out the EndpointVersion and EndpointName for your Acumatica instance, log into Acumatica in a web browser, and then navigate to the Web Service Endpoints page. If necessary, navigate to this page by editing the web browser URL and replacing ScreenId=00000000 (the homepage) with ScreenId=SM207060. If you are redirected back to the homepage, this means your user does not have the necessary permissions to access web services. Under Endpoints properties get the Endpoint Name and Endpoint Version.
Authenticating to Acumatica
There are two authentication methods available for connecting to Acumatica data source, Basic and OAuth.
User Credentials
Set the AuthScheme to Basic and set the User and Password to your login credentials.
OAuth
OAuth requires the authenticating user to interact with Acumatica using the browser, so all OAuth flows require a custom OAuth application. Also, for all flows, set AuthScheme to OAuth. The following sections assume that you have done so.
Desktop Applications
CData provides an embedded OAuth application that simplifies OAuth desktop Authentication. Alternatively, you can create a custom OAuth application. See カスタムOAuth アプリの作成 for information about creating custom applications and reasons for doing so.
For authentication, the only difference between the two methods is that you must set two additional connection properties when using custom OAuth applications.
After setting the following connection properties, you are ready to connect:
- OAuthClientId: Set this to the client Id in your application settings.
- OAuthClientSecret: Set this to the client secret in your application settings.
- CallbackURL: Set this to the Redirect URL in your application settings.
When you connect the 本製品 opens the OAuth endpoint in your default browser. Log in and grant permissions to the application.
Web Applications
When connecting via a Web application, you need to create and register a custom OAuth application with Acumatica. See カスタムOAuth アプリの作成 for more information about custom applications. You can then use the 本製品 to acquire and manage the OAuth token values.
Get an OAuth Access Token
Set the following connection properties to obtain the OAuthAccessToken:
- OAuthClientId: Set this to the client Id in your application settings.
- OAuthClientSecret: Set this to the client secret in your application settings
Then call stored procedures to complete the OAuth exchange:
- Call the GetOAuthAuthorizationURL stored procedure. Set the CallbackURL input to the callback URL you specified in your application settings. If necessary;, set the Scope parameter to request custom permissions.
The stored procedure returns the URL to the OAuth endpoint.
- Open the URL, log in, and authorize the application. You are redirected back to the callback URL.
- Call the GetOAuthAccessToken stored procedure. Set the AuthMode input to WEB. Set the Verifier input to the "code" parameter in the query string of the callback URL. If necessary, set the Scope parameter to request custom permissions.
Once you have obtained the access and refresh tokens, you can connect to data and refresh the OAuth access token either automatically or manually.
Automatic Refresh of the OAuth Access Token
To have the driver automatically refresh the OAuth access token, set the following on the first data connection:
- InitiateOAuth: Set this to REFRESH.
- OAuthClientId: Set this to the client Id in your application settings.
- OAuthClientSecret: Set this to the client secret in your application settings.
- OAuthAccessToken: Set this to the access token returned by GetOAuthAccessToken.
- OAuthRefreshToken: Set this to the refresh token returned by GetOAuthAccessToken.
- OAuthSettingsLocation: Set this to the path where the 本製品 saves the OAuth token values, which persist across connections.
Manual Refresh of the OAuth Access Token
The only value needed to manually refresh the OAuth access token when connecting to data is the OAuth refresh token.
Use the RefreshOAuthAccessToken stored procedure to manually refresh the OAuthAccessToken after the ExpiresIn parameter value returned by GetOAuthAccessToken has elapsed, then set the following connection properties:
- OAuthClientId: Set this to the client Id in your application settings.
- OAuthClientSecret: Set this to the client secret in your application settings.
Then call RefreshOAuthAccessToken with OAuthRefreshToken set to the OAuth refresh token returned by GetOAuthAccessToken. After the new tokens have been retrieved, open a new connection by setting the OAuthAccessToken property to the value returned by RefreshOAuthAccessToken.
Finally, store the OAuth refresh token so that you can use it to manually refresh the OAuth access token after it has expired.
データの取得
Select-Acumatica cmdlet はデータを取得するためのネイティブなPowerShell インターフェースを提供します。
$results = Select-Acumatica -Connection $conn -Table "Events" -Columns @("Id, location_displayName") -Where "Id='Jq74mCczmFXk1tC10GB'"
Invoke-Acumatica cmdlet はSQL インターフェースを提供します。このcmdlet を使うと、Query パラメータを介してSQL クエリを実行できます。
cmdlet 出力のパイプ処理
cmdlet は行オブジェクトをパイプラインに一度に一行ずつ返します。以下は、結果をCSV ファイルにエクスポートします。
Select-Acumatica -Connection $conn -Table Events -Where "Id = 'Jq74mCczmFXk1tC10GB'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myEventsData.csv -NoTypeInformation
Select-Acumatica からの結果をSelect-Object cmdlet にパイプして、Export-CSV cmdlet にパイプする前にいくつかのプロパティを実行していることがわかるでしょう。これをする理由は、CData Cmdlets は接続、テーブル、およびカラムの情報を結果セットの各行オブジェクトに追加しますが、必ずしもその情報がCSV ファイルに必要ではないからです。
ただし、これによってcmdlet の出力を別のcmdlet にパイプすることが容易になります。以下に、結果セットをJSON に変換する例を示します。
PS C:\> $conn = Connect-Acumatica -OAuthClientId 'MyApplicationId' -OAuthClientSecret 'MySecretKey' -OAuthCallbackURL 'http://localhost:33333'
PS C:\> $row = Select-Acumatica -Connection $conn -Table "Events" -Columns (Id, location_displayName) -Where "Id = 'Jq74mCczmFXk1tC10GB'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
"Connection": {
},
"Table": "Events",
"Columns": [
],
"Id": "MyId",
"location_displayName": "Mylocation_displayName"
}
データの削除
以下は、抽出条件に合うあらゆるレコードを削除します。
Select-Acumatica -Connection $conn -Table Events -Where "Id = 'Jq74mCczmFXk1tC10GB'" | Remove-Acumatica
データの変更
cmdlet はデータクレンジング同様、データの変換を容易にします。次の例は、レコードがすでに存在するかどうか、挿入する前に更新が必要かどうかをチェックしてから、CSV ファイルのデータをAcumatica にロードします。
Import-Csv -Path C:\MyEventsUpdates.csv | %{
$record = Select-Acumatica -Connection $conn -Table Events -Where ("Id = `'"+$_.Id+"`'")
if($record){
Update-Acumatica -Connection $conn -Table Events -Columns @("Id","location_displayName") -Values @($_.Id, $_.location_displayName) -Where "Id = `'$_.Id`'"
}else{
Add-Acumatica -Connection $conn -Table Events -Columns @("Id","location_displayName") -Values @($_.Id, $_.location_displayName)
}
}