Recently Microsoft have added the Common Data Services to Powerapps and PowerShell. It is great to have a central place to store data, however configuring all the entities can be painful if you have to click through all the options. The new solution model helps you to move configurations between different environments, but still it is a lot of mouse clicks to get all the work done. I’ve been looking for an option to script my custom solutions and I found that it was less than easy to get started.
There isn’t a CDS specific PowerShell available, therefor I started by looking at the CRM/XRM option available. Quite Quickly I stumbled upon Sean McNellis’s PowerShell tools.
I started by downloading Microsoft Dynamics 365 (v8.x) Software Development Kit (SDK) as the Microsoft.Xrm.Data.PowerShell Powershell tools depend on this being installed.
Once I had the tools installed I started by trying to get the details out of my CDS environment. The Connect-CRMOnlinDiscovery cmdlet got this all sorted out for me.
After Setting the Deployment Type and the login details
I received the output for my environment for my default environment
If you want to get to a different environment simply select the Display list of organisations.
Once connected you can get details on the connection using the Get-CRMConnection Cmdlet.
Time to have a look at the Entities.
Access Entities through PowerShell
I’m using the Entity account as a starting point. Using the Get-CrmEntityAttributes it is possible to get all details for the Entity. Note that the EntityLogicalName is the name
Get-CrmEntityAttributes -EntityLogicalName "account"
Note that the above cmdlet is case sensitive and only the Entity’s Name will work.
Data Management in CDS using PowerShell
Now I want to take this further I want to be able to add data to my entities.
Using the following commend I can create new records, get record, update records and delete record
New-CrmRecord -EntityLogicalName account -Id $accountId -Fields name Set-CrmRecord -CrmRecord $account Get-CrmRecord -EntityLogicalName account -Id $accountId -Fields name Remove-CrmRecord -CrmRecord $account