We are proud to share our inclusion in the 2024 Gartner Magic Quadrant for Data Integration Tools. We believe this recognition reflects the differentiated business outcomes CData delivers to our customers.
Get the Report →Automate Dynamics 365 Integration Tasks from PowerShell
Are you in search of a quick and easy way to access Dynamics 365 data from PowerShell? This article demonstrates how to utilize the Dynamics 365 Cmdlets for tasks like connecting to Dynamics 365 data, automating operations, downloading data, and more.
The CData Cmdlets for Dynamics 365 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.
About Dynamics 365 Data Integration
CData simplifies access and integration of live Microsoft Dynamics 365 data. Our customers leverage CData connectivity to:
- Read and write data in the full Dynamics 365 ecosystem: Sales, Customer Service, Finance & Operations, Marketing, and more.
- Extend the native features of Dynamics CRM with customizable caching and intelligent query aggregation and separation.
- Authenticate securely with Dynamics 365 in a variety of ways, including Azure Active Directory, Azure Managed Service Identity credentials, and Azure Service Principal using either a client secret or a certificate.
- Use SQL stored procedures to manage their Dynamics 365 entities - listing, creating, and removing associations between entities.
CData customers use our Dynamics 365 connectivity solutions for a variety of reasons, whether they're looking to replicate their data into a data warehouse (alongside other data sources) or analyze live Dynamics 365 data from their preferred data tools inside the Microsoft ecosystem (Power BI, Excel, etc.) or with external tools (Tableau, Looker, etc.).
Getting Started
PowerShell Cmdlets or ADO.NET Provider?
The Cmdlets are not only a PowerShell interface to Dynamics 365, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Dynamics 365 data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Dynamics 365. To access Dynamics 365 data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Dynamics 365.
Once you have acquired the necessary connection properties, accessing Dynamics 365 data in PowerShell can be enabled in three steps.
Edition and OrganizationUrl are required connection properties. The Dynamics 365 connector supports connecting to the following editions: CustomerService, FieldService, FinOpsOnline, FinOpsOnPremise, HumanResources, Marketing, ProjectOperations and Sales.
For Dynamics 365 Business Central, use the separate Dynamics 365 Business Central driver.
OrganizationUrl is the URL to your Dynamics 365 organization. For instance, https://orgcb42e1d0.crm.dynamics.com
PowerShell
-
Install the module:
Install-Module Dynamics365Cmdlets
-
Connect:
$dynamics365 = Connect-Dynamics365 -OrganizationUrl "$OrganizationUrl" -Edition "$Edition"
-
Search for and retrieve data:
$name = "MyAccount" $goalheadings = Select-Dynamics365 -Connection $dynamics365 -Table "GoalHeadings" -Where "Name = `'$Name`'" $goalheadings
You can also use the Invoke-Dynamics365 cmdlet to execute SQL commands:
$goalheadings = Invoke-Dynamics365 -Connection $dynamics365 -Query 'SELECT * FROM GoalHeadings 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\lib\System.Data.CData.Dynamics365.dll")
-
Connect to Dynamics 365:
$conn= New-Object System.Data.CData.Dynamics365.Dynamics365Connection("OrganizationUrl=https://myaccount.operations.dynamics.com/;Edition=Sales;InitiateOAuth=GETANDREFRESH") $conn.Open()
-
Instantiate the Dynamics365DataAdapter, execute an SQL query, and output the results:
$sql="SELECT GoalHeadingId, Name from GoalHeadings" $da= New-Object System.Data.CData.Dynamics365.Dynamics365DataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.goalheadingid $_.name }
Update Dynamics 365 Data
PowerShell
Update-Dynamics365 -Connection $Dynamics365 -Columns @('GoalHeadingId','Name') -Values @('MyGoalHeadingId', 'MyName') -Table GoalHeadings -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.Dynamics365.Dynamics365Command("UPDATE GoalHeadings SET Name='MyAccount' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Dynamics365.Dynamics365Parameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Insert Dynamics 365 Data
PowerShell
Add-Dynamics365 -Connection $Dynamics365 -Table GoalHeadings -Columns @("GoalHeadingId", "Name") -Values @("MyGoalHeadingId", "MyName")
ADO.NET
$cmd = New-Object System.Data.CData.Dynamics365.Dynamics365Command("INSERT INTO GoalHeadings (Name) VALUES (@myName)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Dynamics365.Dynamics365Parameter("@myName","MyAccount")))
$cmd.ExecuteNonQuery()
Delete Dynamics 365 Data
PowerShell
Remove-Dynamics365 -Connection $Dynamics365 -Table "GoalHeadings" -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.Dynamics365.Dynamics365Command("DELETE FROM GoalHeadings WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Dynamics365.Dynamics365Parameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject