Automate Dynamics 365 Business Central Integration Tasks from PowerShell

Ready to get started?

Download a free trial:

Download Now

Learn more:

Dynamics 365 Business Central ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with Dynamics 365 Business Central data including Items, Sales Orders, Purchase Orders, and more!



Are you looking for a quick and easy way to access Dynamics 365 Business Central data from PowerShell? We show how to use the Cmdlets for Dynamics 365 Business Central and the CData ADO.NET Provider for Dynamics 365 Business Central to connect to Dynamics 365 Business Central data and synchronize, automate, download, and more.

The CData Cmdlets for Dynamics 365 Business Central are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to Dynamics 365 Business Central.

Cmdlets or ADO.NET?

The cmdlets are not only a PowerShell interface to the Dynamics 365 Business Central API, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Dynamics 365 Business Central data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Dynamics 365 Business Central. To access Dynamics 365 Business Central data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Dynamics 365 Business Central.

After obtaining the needed connection properties, accessing Dynamics 365 Business Central data in PowerShell consists of three basic steps.

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.

PowerShell

  1. Install the module:

    Install-Module D365BusinessCentralCmdlets
  2. Connect:

    $d365businesscentral = Connect-D365BusinessCentral -OrganizationUrl "$OrganizationUrl"
  3. Search for and retrieve data:

    $name = "MyAccount" $accounts = Select-D365BusinessCentral -Connection $d365businesscentral -Table "Accounts" -Where "Name = `'$Name`'" $accounts

    You can also use the Invoke-D365BusinessCentral cmdlet to execute SQL commands:

    $accounts = Invoke-D365BusinessCentral -Connection $d365businesscentral -Query 'SELECT * FROM Accounts WHERE Name = @Name' -Params @{'@Name'='MyAccount'}

ADO.NET

  1. Load the provider's assembly:

    [Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Dynamics 365 Business Central\lib\System.Data.CData.D365BusinessCentral.dll")
  2. Connect to Dynamics 365 Business Central:

    $conn= New-Object System.Data.CData.D365BusinessCentral.D365BusinessCentralConnection("OrganizationUrl=https://myaccount.financials.dynamics.com/;") $conn.Open()
  3. Instantiate the D365BusinessCentralDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT accountid, Name from Accounts" $da= New-Object System.Data.CData.D365BusinessCentral.D365BusinessCentralDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.accountid $_.name }

Update Dynamics 365 Business Central Data

PowerShell

Update-D365BusinessCentral -Connection $D365BusinessCentral -Columns @('accountid','Name') -Values @('Myaccountid', 'MyName') -Table Accounts -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.D365BusinessCentral.D365BusinessCentralCommand("UPDATE Accounts SET Name='MyAccount' WHERE Id = @myId", $conn) $cmd.Parameters.Add(new System.Data.CData.D365BusinessCentral.D365BusinessCentralParameter("@myId","10456255-0015501366")) $cmd.ExecuteNonQuery()

Insert Dynamics 365 Business Central Data

PowerShell

Add-D365BusinessCentral -Connection $D365BusinessCentral -Table Accounts -Columns @("accountid", "Name") -Values @("Myaccountid", "MyName")

ADO.NET

$cmd = New-Object System.Data.CData.D365BusinessCentral.D365BusinessCentralCommand("INSERT INTO Accounts (Name) VALUES (@myName)", $conn) $cmd.Parameters.Add(new System.Data.CData.D365BusinessCentral.D365BusinessCentralParameter("@myName","MyAccount")) $cmd.ExecuteNonQuery()

Delete Dynamics 365 Business Central Data

PowerShell

Remove-D365BusinessCentral -Connection $D365BusinessCentral -Table "Accounts" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.D365BusinessCentral.D365BusinessCentralCommand("DELETE FROM Accounts WHERE Id=@myId", $conn) $cmd.Parameters.Add(new System.Data.CData.D365BusinessCentral.D365BusinessCentralParameter("@myId","001d000000YBRseAAH")) $cmd.ExecuteNonQuery()