Discover how a bimodal integration strategy can address the major data management challenges facing your organization today.
Get the Report →Automate Dynamics 365 Business Central Integration Tasks from PowerShell
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 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
-
Install the module:
Install-Module D365BusinessCentralCmdlets
-
Connect:
$d365businesscentral = Connect-D365BusinessCentral -OrganizationUrl "$OrganizationUrl"
-
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
-
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")
-
Connect to Dynamics 365 Business Central:
$conn= New-Object System.Data.CData.D365BusinessCentral.D365BusinessCentralConnection("OrganizationUrl=https://myaccount.financials.dynamics.com/;") $conn.Open()
-
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