Automate Dynamics 365 Business Central Integration Tasks from PowerShell
The CData ADO.NET Provider for Dynamics 365 Business Central is a standard ADO.NET Provider 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.
ADO.NET Provider
The ADO.NET Provider provides a SQL interface for Dynamics 365 Business Central; this tutorial shows how to use the Provider to create, retrieve, update, and delete Dynamics 365 Business Central data.
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 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.
Access Key
To authenticate with an Access Key, set AuthScheme to "AccessKey" and 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.
If you wish to authenticate through other methods, refer to the Help documentation.
-
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/;InitiateOAuth=GETANDREFRESH;") $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
$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
$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
$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