Automate Dynamics 365 Sales Integration Tasks from PowerShell

Ready to get started?

Download for a free trial:

Download Now

Learn more:

Dynamics 365 ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with Microsoft Dynamics 365 account data including Leads, Contacts, Opportunities, Accounts, and more!



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

The CData Cmdlets for Dynamics 365 Sales 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 Sales.

Cmdlets or ADO.NET?

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

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

Dynamics 365 uses the OAuth authentication standard. See the Getting Started section in the help documentation for a guide to completing the process.

PowerShell

  1. Install the module:

    Install-Module D365SalesCmdlets
  2. Connect:

    $d365sales = Connect-D365Sales -OrganizationUrl "$OrganizationUrl"
  3. Search for and retrieve data:

    $name = "MyAccount" $accounts = Select-D365Sales -Connection $d365sales -Table "Accounts" -Where "Name = `'$Name`'" $accounts

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

    $accounts = Invoke-D365Sales -Connection $d365sales -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 Sales\lib\System.Data.CData.D365Sales.dll")
  2. Connect to Dynamics 365 Sales:

    $conn= New-Object System.Data.CData.D365Sales.D365SalesConnection("OrganizationUrl=https://myaccount.crm.dynamics.com/;InitiateOAuth=GETANDREFRESH") $conn.Open()
  3. Instantiate the D365SalesDataAdapter, execute an SQL query, and output the results:

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

Update Dynamics 365 Sales Data

PowerShell

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

ADO.NET

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

Insert Dynamics 365 Sales Data

PowerShell

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

ADO.NET

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

Delete Dynamics 365 Sales Data

PowerShell

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

ADO.NET

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