Using PowerShell to export/import solutions, data, and Word Templates

By | September 26, 2019

 

I blogged about it before, but now that ItAintBoring.CDS.PowerShell library has been fine tuned, it might be time to do it again.

There are three parts to this post:

  • Introduction
  • ItAintBoring.CDS.PowerShell installation instructions
  • ItAintBoring.CDS.PowerShell usage example

 

INTRODUCTION

There are different ways we can set up ci/cd – we can use Azure Devops, we can use PowerApp Build Tools, we can use various community tools, or we can even create our own powershell library.

Each has some pros and cons, but this post is all about that last option, which is using our own “powershell library”.

What are the main benefits? Well, it’s the amount of control we have over what we can do. You might say that “no code” is always better, but I would argue that, when you are setting up ci/cd, “no code” will probably be the least of your concerns.

Anyway, in this case we can use the library to export/import solutions, and, also, to export/import configuration data. Including, as of v 1.0.1, word templates.

INSTALLATION INSTRUCTIONS

1. Create a new folder, call it “CDS Deployment Scripts”(although, you can give it a different name if you prefer)

2. Create a new ps1 file in that folder with the content below

function Get-NuGetPackages{

$sourceNugetExe = “https://dist.nuget.org/win-x86-commandline/latest/nuget.exe”
$targetNugetExe = “.\nuget.exe”
Remove-Item .\Tools -Force -Recurse -ErrorAction Ignore
Invoke-WebRequest $sourceNugetExe -OutFile $targetNugetExe
Set-Alias nuget $targetNugetExe -Scope Global -Verbose

./nuget install ItAintBoring.CDS.PowerShell -O .
}
Get-NuGetPackages

Here is how it should all look like:

image

3. Run the file above with PowerShell – you will have the scripts downloaded

image

image

4. Update system path variable so it has a path to the deployment.psm1

image

 

USAGE EXAMPLE

For the remaining part of this post, you will need to download sources from Git (just clone the repo if you prefer):

https://github.com/ashlega/ItAintBoring.Deployment

That repository includes all script sources, but, also, a sample project.

1. In the file explorer, navigate to the Setup/Projects/DeploymentDemo folder

image

2. Update settings.ps1 with the correct environment url-s

Open settings.ps1 and update your connection settings for the source/destination. If you are just trying the demo, don’t worry about the source, but make sure to update the destination.

image

3. IF YOU HAD A SOURCE environment

Which you don’t, at least while working on this example. But the idea is that, if you start using the script in your ci/cd, you would have the source.

So, if you had a source environment, you would now need to update Export.ps1. The purpose of that file is to export solutions and data from the source:

image

You can see how, for the data, it’s using FetchXml, which also works for the Word Document Templates.

4. Run import_solutions.ps1 to deploy solutions and data to the destination environment

image

5. Run import_data.ps1 to deploy data (including Word Templates) for the new entities to the destination environment

image

 

As a result of those last two steps above, you will have the solution deployed and some data uploaded:

image

image

image

Most importantly, it literally takes only a few clicks once those export/import files have been prepared.

And what about the source control? Well, it can still be there if you want. Once the solution file has been exported, you can run solution packager to unpack the file, then use Git to put it in the DevOps or on GitHub. Before running the import, you will need to get those sources from the source control, package them using the solution packager, and run the import scripts.

2 thoughts on “Using PowerShell to export/import solutions, data, and Word Templates

  1. Alberto

    Hi there! qq, does this apply to power automate solutions? thanks

    Reply
    1. Alex Shlega Post author

      It just works with solutions – does not matter what’s in them

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *