PowerShell でSage US Data データをCSV に落としてみた

標準PowerShell cmdlets を使ってSage US テーブルにアクセス。

CData Cmdlets Module for Sage US は、直感的なSage US データ連携を提供する標準cmdlet です。 本記事では、Sage50US Cmdlets を使ったサンプルを提供します。

Creating a Connection to Your Sage US Data

The Application Id and Company Name connection string options are required to connect to Sage as a data source. You can obtain an Application Id by contacting Sage directly to request access to the Sage 50 SDK.

Sage must be installed on the machine. The Sage.Peachtree.API.dll and Sage.Peachtree.API.Resolver.dll assemblies are required. These assemblies are installed with Sage in C:\Program Files\Sage\Peachtree\API\. Additionally, the Sage SDK requires .NET Framework 4.0 and is only compatible with 32-bit applications. To use the Sage SDK in Visual Studio, set the Platform Target property to "x86" in Project -> Properties -> Build.

You must authorize the application to access company data: To authorize your application to access Sage, restart the Sage application, open the company you want to access, and connect with your application. You will then be prompted to set access permissions for the application in the resulting dialog.

While the compiled executable will require authorization only once, during development you may need to follow this process to reauthorize a new build. To avoid restarting the Sage application when developing with Visual Studio, click Build -> Configuration Manager and uncheck "Build" for your project.

$conn = Connect-Sage50US  -ApplicationId "$ApplicationId" -CompanyName "$CompanyName"

Selecting Data

Follow the steps below to retrieve data from the Customer table and pipe the result into to a CSV file:

Select-Sage50US -Connection $conn -Table Customer | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myCustomerData.csv -NoTypeInformation

You will notice that we piped the results from Select-Sage50US 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" in the result set, and we do not necessarily want that information in our CSV file.

The Connection, Table, and Columns are appended to the results in order to facilitate piping results from one of the CData Cmdlets directly into another one.

Deleting Data

The following line deletes any records that match the criteria:

Select-Sage50US -Connection $conn -Table Customer -Where "Name = ALDRED" | Remove-Sage50US

Inserting and Updating Data

The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into Sage US, checking first whether a record already exists and needs to be updated instead of inserted.

Import-Csv -Path C:\MyCustomerUpdates.csv | %{
  $record = Select-Sage50US -Connection $Sage50US -Table Customer -Where ("Key = `'"+$_.Key+"`'")
  if($record){
    Update-Sage50US -Connection $sage50us -Table Customer -Columns ("Name","LastInvoiceAmount") -Values ($_.Name, $_.LastInvoiceAmount) -Where ("Key = `'"+$_.Key+"`'")
  }else{
    Add-Sage50US -Connection $sage50us -Table Customer -Columns ("Name","LastInvoiceAmount") -Values ($_.Name, $_.LastInvoiceAmount)
  }
}

As always, our goal is to simplify the way you connect to data. With cmdlets users can install a data module, set the connection properties, and start building. Download Cmdlets and start working with your data in PowerShell today!

 
 
ダウンロード