VOOZH about

URL: https://www.cdata.com/kb/tech/d365businesscentral-ado-powershell.rst

⇱ Automate Dynamics 365 Business Central Integration Tasks from PowerShell


Automate Dynamics 365 Business Central Integration Tasks from PowerShell

👁 Jerod Johnson
Jerod Johnson
Director, Technology Evangelism
Are you in search of a quick and easy way to access Dynamics 365 Business Central data from PowerShell? This article demonstrates how to utilize the Dynamics 365 Business Central Cmdlets for tasks like connecting to Dynamics 365 Business Central data, automating operations, downloading data, 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.

PowerShell Cmdlets or ADO.NET Provider?

The Cmdlets are not only a PowerShell interface to Dynamics 365 Business Central, 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.

Once you have acquired the necessary connection properties, accessing Dynamics 365 Business Central data in PowerShell can be enabled in three steps.

To authenticate to Dynamics 365 Business Central, you must select an AuthScheme and provide the required properties (OAuth by default).

All Methods

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

Access Key

To authenticate with an Access Key, set AuthScheme to "AccessKey" and provide the and properties.

To obtain the and 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 and connection string properties. Note that the User Name is not your email address. It is a shortened user name.

If you wish to authenticate through other methods, refer to the Help documentation.

PowerShell

  1. Install the module:

    Install-Module D365BusinessCentralCmdlets
  2. Connect:

    $d365businesscentral = Connect-D365BusinessCentral -OrganizationUrl "$OrganizationUrl" -InitiateOAuth "$InitiateOAuth"
    
  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/;InitiateOAuth=GETANDREFRESH;")
    $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-Object 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-Object 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-Object System.Data.CData.D365BusinessCentral.D365BusinessCentralParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject

Ready to get started?

Download a free trial of the Dynamics 365 Business Central Data Provider to get started:

 Download Now

Learn more:

👁 Dynamics 365 Business Central (NAV) Icon
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!