Ready to get started?

Learn more about the Dynamics 365 Business Central PowerShell Cmdlets or download a free trial:

Download Now

PowerShell でDynamics 365 Business Central Data データをCSV に落としてみた

標準PowerShell cmdlets を使ってDynamics 365 Business Central テーブルにアクセス。

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

Creating a Connection to Your Dynamics 365 Business Central Data

To authenticate to Dynamics 365 Business Central, you must provide the User and AccessKey properties.

To obtain the User and AccessKey values, navigate to the Users page in Dynamics 365 Business Central and then click on Edit. The User Name and Web Service Access Key values are what you will enter as the User and AccessKey connection string properties. Note that the User Name is not your email address. It is a shortened user name.

To connect to data, specify OrganizationUrl. If you have multiple companies in your organization, you must also specify the Company to indicate which company you would like to connect to. Company does not need to be specified if you have only one company.

$conn = Connect-D365BusinessCentral  -OrganizationUrl "$OrganizationUrl"

Selecting Data

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

Select-D365BusinessCentral -Connection $conn -Table Accounts | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myAccountsData.csv -NoTypeInformation

You will notice that we piped the results from Select-D365BusinessCentral 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-D365BusinessCentral -Connection $conn -Table Accounts -Where "Name = MyAccount" | Remove-D365BusinessCentral

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 Dynamics 365 Business Central, checking first whether a record already exists and needs to be updated instead of inserted.

Import-Csv -Path C:\MyAccountsUpdates.csv | %{
  $record = Select-D365BusinessCentral -Connection $D365BusinessCentral -Table Accounts -Where ("Id = `'"+$_.Id+"`'")
  if($record){
    Update-D365BusinessCentral -Connection $d365businesscentral -Table Accounts -Columns ("accountid","Name") -Values ($_.accountid, $_.Name) -Where ("Id = `'"+$_.Id+"`'")
  }else{
    Add-D365BusinessCentral -Connection $d365businesscentral -Table Accounts -Columns ("accountid","Name") -Values ($_.accountid, $_.Name)
  }
}

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!

 
 
ダウンロード