Cmdlets for Xero

Build 22.0.8479

接続の確立

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

インストールおよび接続

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

Install-Module XeroCmdlets

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

Import-Module XeroCmdlets;

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

$conn = Connect-Xero

Xero API への接続

本製品 は、以下のXero API をサポートしています。

  • Accounting API:Schema 接続プロパティをACCOUNTING に設定。
  • Australian Payroll API:Schema 接続プロパティをPAYROLLAUS に設定。
  • Files API:Schema 接続プロパティをFILES に設定。
  • Fixed Assets API:Schema 接続プロパティをASSETS に設定。
  • Projects API:Schema 接続プロパティをPROJECTS に設定。

It is also recommended that you set the Tenant property, which can be the name or ID of a Xero organization. Xero allows you to authorize the 本製品 to access multiple organizations. By default the 本製品 will pick the first one, which may not be the one you expect if the 本製品 is granted access to additional organizations.

If you need to confirm what organizations the 本製品 has access to, you can connect without an Tenant and read the Tenants view. This will provide both the ID and the name of each connected organization. You should use the ID to set the Tenant property when possible since multiple organizations can have the same name.

Xero への認証

All connections to Xero are authenticated using OAuth. The 本製品 supports using PKCE applications and OAuth applications. For desktop applications, the 本製品's embedded application is the simplest way to authenticate.

When the 本製品 starts, it will open a browser and Xero will request your login information. The 本製品 will use the credentials you provide to access your Xero data. These credentials will be saved and automatically refreshed as needed. For desktop applications, the 本製品's default application is the simplest way to authenticate.

When the 本製品 starts, it will open a browser and Xero will request your login information. The 本製品 will use the credentials you provide to access your Xero data. These credentials will be saved and automatically refreshed as needed.

データの取得

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

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

cmdlet 出力のパイプ処理

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

Select-Xero -Connection $conn -Table Contacts -Where "ContactStatus = 'ACTIVE'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myContactsData.csv -NoTypeInformation

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

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

 
PS C:\> $conn  = Connect-Xero
PS C:\> $row = Select-Xero -Connection $conn -Table "Contacts" -Columns (Discount, Name) -Where "ContactStatus = 'ACTIVE'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
  "Connection":  {

  },
  "Table":  "Contacts",
  "Columns":  [

  ],
  "Discount":  "MyDiscount",
  "Name":  "MyName"
} 

データの削除

以下は、抽出条件に合うあらゆるレコードを削除します。

Select-Xero -Connection $conn -Table Contacts -Where "ContactStatus = 'ACTIVE'" | Remove-Xero

データの変更

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

Import-Csv -Path C:\MyContactsUpdates.csv | %{
  $record = Select-Xero -Connection $conn -Table Contacts -Where ("ContactId = `'"+$_.ContactId+"`'")
  if($record){
    Update-Xero -Connection $conn -Table Contacts -Columns @("Discount","Name") -Values @($_.Discount, $_.Name) -Where "ContactId  = `'$_.ContactId`'"
  }else{
    Add-Xero -Connection $conn -Table Contacts -Columns @("Discount","Name") -Values @($_.Discount, $_.Name)
  }
}

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