The right tool for each purpose
Tags: powerplatform, dataverse, powershell, tools
Published: 2024-06-07 10:04:27

Yesterday I was struggling with a common, basic, argument: I needed to provide instructions to deploy a solution to a target environment, and that solution to work properly also required some basic data-model-related activities: once installed, the "deployer" needed to create a couple of records on table X, a couple of records on table Y, create a list of predefined BUs, assign a specific role to the teams associated to each of those BUs, and so on.

I started thinking about **Configuration Migration Tool**, It allows to migrate data from an environment to another, but:

1. It does not preserves GUIDs (and I always like to preserve GUIDs between environments on config data, it makes really easy to lookup records)
2. It just moves records... It doesn't helps me with team / role association

Thinking on issue #1 above, my mind went to my own [_**\_n.EnvironmentComparer**_](https://www.xrmtoolbox.com/plugins/Greg.Xrm.EnvironmentComparer/), a great tool for XrmToolbox I made a couple of years ago that saved (and continues saving) HOURS on my projects 🙂... but:

1. It just moves records... It doesn't helps me with team / role association
2. It requires the "deployer" to access both the test and the prod environment to compare configs

Thus, was not acceptable in my scenario.

The next thing that came in my mind was to create a custom, special purpose, console app. But this meant to:

1. Another deliverable to be packaged and deployed
2. Managing possible issues with .NET framework version on the "deployer" machine

Moreover, I don't like to do stuff that is too specialized... if I can't reuse it, it's a waste of time.
PACX then came in my mind... I thought what if I add a few commands on PACX just to create/update records via command line, and deal with security management. Then I could create a Powershell/Terminal script that can be runnable without any particular issue (except for .NET Core).

But it would have required at least a couple of hours to create the commands, then the time to create the setup script... too much, I wanted to finish everything in an hour or less...

Powershell drove my mind [towards THIS](https://github.com/seanmcne/Microsoft.Xrm.Data.PowerShell). **Microsoft.Xrm.Data.PowerShell** is an open source library created by Sean McNellis I started using years ago. **Basically, provides the same features I wanted to add to PACX, already available for Powershell scripts!**

🔝 But I'm not a 🥷🏻of Powershell... and the documentation on **Microsoft.Xrm.Data.PowerShell** is not so rich... where to start?

I asked Bing Copilot:

![Chatting with Copilot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/550qky1oypqs9oteslid.png)

Then I started from there. The code provided by Bing Copilot was far from perfect but... in a few try-and-error, in an hour. while also dealing with my daughter's hunger needs, I created and tested a 700 lines PowerShell script that performs all the environment data setup actions with a differential approach (for each data, if the data is already there, do nothing), in order to be able to run the script twice or three times without collateral effects.

It works like a charme.

---

**Lessons learned**: 
- **I added another tool to my toolbelt**, and I learned where and when is the right time to use it
- **I learned how to script with Powershell vs Dataverse**. Copilot was not my teacher, but more like a collegue that does must of the work, that I have to review and correct. Reviewing and correcting, I learned a lot more (and a lot faster) than studying everything from scratch
- **Never re-build the wheel**. There's no need to create a command for PACX If there is another solution to the same problem, with the same effectiveness, already available
Hope this will help you too!