Automatic Deployment and Extraction from CRM Using PowerShell Scripts
Recently I was tasked with the task of creating an automatic extraction and importing solution using PowerShell Scripts. To do this I created two files one for export and extracting with the other in charge of pack and importing the solution. I was using the Visual Studio template of CRM Package that comes with the sdk.
Export and Extracting The Solution
First thing I did was implement the export and extraction of the CRM solution. The PowerShell script first exports the solution from the web using a module called Microsoft.Xrm.Data.PowerShell. Then it extracts the downloaded zip into a folder for the correct file structure. A nice thing about using the Microsoft.Xrm.Data.PowerShell dll is the fact it gives you additional CRM commands you can do. For example before downloading the solution it allows you to change the version number of the solution before downloading.
As you can see I can export the solution into the project location. The if statement is there to help parse the user input if the user wanted a managed solution or not. Later on I’ll show you how I set up my file structure inside the project.
Afterwards I extract the solution using the SolutionPackager.exe to get the file structure. Also It allows the user to check the extracted folder into source control so they can track the changes to the solution in source control. This seems to be the go to way to ensure that developers working on the same solution don’t overwrite each other’s changes to the solution when they publish their changes.
Pack and Import the Solution
For the Pack and Import PowerShell file I first extract and pack the solution. I do this because the file structure inside the extracted solution from the zip file has to match what CRM expects. As a precautionary step I just use the SolutionPackager to extract and pack the data to ensure that the file structure in the zip file is good. Then I pack it back into the zip file ready to be imported.
Then using the deployment packager I import the solution into CRM.
Now notice that the it uses the dll COREPackageDeployer. This dll is generated on build of the solution of the project so depending on what you named the project the dll name is different.
Before I go into how to run the scripts to get everything working I’ll show you my file structure and explain it.
The reason for the RegisterXRMTooling.ps1, Microsoft.Xrm.Tooling.CrmConnector.Powershell.dll , and the Microsoft.Xrm.Tooling.PackageDeployment.Powershell.dll files is due to the fact that if this is the first time running the code you have to run the RegisterXRMTooling.ps1 file to get the dependencies locked into PowerShell. The other two dll files are required to run the RegisterXRMTooling.ps1 file. These files can be found in the SDK folder inside the PackageDeployer/PowerShell folder. If this is your first time running the file make sure to run the RegisterXRMTooling.ps1 file in an Admin PowerShell console.
Note that I had dragged the SolutionPackager.exe file into the project. The project doesn’t normally come with the SolutionPackager.exe. This file can be found in the bin folder of the SDK.
I have the ExtractedSolution zip and folder in the solution so that the PowerShell scripts can grab and paste the solution data there.
Building the Project
If you’re planning to do the export and extract solution into the Visual Studio using the ExtractedSolution.zip you need to make sure the property of Copy to Output Directory is set to copy always. Don’t forget to change the ImportConfig.xml file to also copy always. This file is where the dll specifically the package deployer looks for the zip file to import into CRM. In other words any files you decide to use with the package deployer should be set to copy always in order to be built and stored inside the debug folder.
To run these PowerShell scripts you’re going to have to open a PowerShell console as Admin. Then you will have to navigate to the project folder.
Note that even though you can run the Export and Extract file directly from the project files. You can’t do that with the Pack And Import file. To run that file you need to navigate to the bin folder where the dll is created and run it there. The package deployer requires the dll that is why you need to navigate to the debug folder with the dll to run the file.
Also note that you cannot run the two files in the same PowerShell console. Due to the fact that Microsoft.Xrm.Data.PowerShell and the package deployer have conflicting snap-in dlls for the Microsoft.Xrm.Tooling.Connector. I wasn’t able to get them to use the same snap in file so my work around was just to open two PowerShell consoles to run the two files.
Here are some additional links for further understanding I found to be helpful: