Automate Dynamics CRM Integration Tasks from PowerShell



Are you in search of a quick and easy way to access Dynamics CRM data from PowerShell? This article demonstrates how to utilize the Dynamics CRM Cmdlets for tasks like connecting to Dynamics CRM data, automating operations, downloading data, and more.

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

PowerShell Cmdlets or ADO.NET Provider?

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

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

The connection string options meet the authentication and connection requirements of different Dynamics CRM instances. To connect to your instance, set the User and Password properties, under the Authentication section, to valid Dynamics CRM user credentials and set the Url to a valid Dynamics CRM server organization root. Additionally, set the CRMVersion property to 'CRM2011+' or 'CRMOnline'. IFD configurations are supported as well; set InternetFacingDeployment to true.

Additionally, you can provide the security token service (STS) or AD FS endpoint in the STSURL property. This value can be retrieved with the GetSTSUrl stored procedure. Office 365 users can connect to the default STS URL by simply setting CRMVersion.

PowerShell

  1. Install the module:

    Install-Module DynamicsCRMCmdlets
  2. Connect:

    $dynamicscrm = Connect-DynamicsCRM -User "$User" -Password "$Password" -URL "$URL" -CRM Version "$CRM Version"
  3. Search for and retrieve data:

    $firstname = "Bob" $account = Select-DynamicsCRM -Connection $dynamicscrm -Table "Account" -Where "FirstName = `'$FirstName`'" $account

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

    $account = Invoke-DynamicsCRM -Connection $dynamicscrm -Query 'SELECT * FROM Account WHERE FirstName = @FirstName' -Params @{'@FirstName'='Bob'}

ADO.NET

  1. Load the provider's assembly:

    [Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Dynamics CRM\lib\System.Data.CData.DynamicsCRM.dll")
  2. Connect to Dynamics CRM:

    $conn= New-Object System.Data.CData.DynamicsCRM.DynamicsCRMConnection("User=myuseraccount;Password=mypassword;URL=https://myOrg.crm.dynamics.com/;CRM Version=CRM Online;") $conn.Open()
  3. Instantiate the DynamicsCRMDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT FirstName, NumberOfEmployees from Account" $da= New-Object System.Data.CData.DynamicsCRM.DynamicsCRMDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.firstname $_.numberofemployees }

Update Dynamics CRM Data

PowerShell

Update-DynamicsCRM -Connection $DynamicsCRM -Columns @('FirstName','NumberOfEmployees') -Values @('MyFirstName', 'MyNumberOfEmployees') -Table Account -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.DynamicsCRM.DynamicsCRMCommand("UPDATE Account SET FirstName='Bob' WHERE Id = @myId", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.DynamicsCRM.DynamicsCRMParameter("@myId","10456255-0015501366"))) $cmd.ExecuteNonQuery()

Insert Dynamics CRM Data

PowerShell

Add-DynamicsCRM -Connection $DynamicsCRM -Table Account -Columns @("FirstName", "NumberOfEmployees") -Values @("MyFirstName", "MyNumberOfEmployees")

ADO.NET

$cmd = New-Object System.Data.CData.DynamicsCRM.DynamicsCRMCommand("INSERT INTO Account (FirstName) VALUES (@myFirstName)", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.DynamicsCRM.DynamicsCRMParameter("@myFirstName","Bob"))) $cmd.ExecuteNonQuery()

Delete Dynamics CRM Data

PowerShell

Remove-DynamicsCRM -Connection $DynamicsCRM -Table "Account" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.DynamicsCRM.DynamicsCRMCommand("DELETE FROM Account WHERE Id=@myId", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.DynamicsCRM.DynamicsCRMParameter("@myId","001d000000YBRseAAH"))) $cmd.ExecuteNonQuery()

Ready to get started?

Download a free trial of the Dynamics CRM Data Provider to get started:

 Download Now

Learn more:

Dynamics CRM Icon Dynamics CRM ADO.NET Provider

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