Here we are, got to part 3 of this. The topic of the day for this article is extracting the solution package from an environment.

But, let’s see first what we discussed so far:

Part one – this deals with the initial setup

Part two – this talks about connecting to an instance

So, in part two we got the connection. We have now the variable $myCRM to hold that.

With that in place, we could do something as simple as:

Export-CrmSolution –conn $myCRM –SolutionName “solutionName” –SolutionFilePath “C:\YourPathHere” –SolutionZipFileName “solutionName.zip”

What’s that going to do? Well, it takes a solution named solutionName and exports it in an unmanaged form to the specified path.

But, is that really enough? I’d say not. What can we do better here?

First off, we can export as managed or unmanaged. That’s simple enough by simply adding the –Managed switch. By default you get the unmanaged. If you want to export both just run the two separate lines.

Next, to make this even better, you can compose the name of the solution with the managed or unmanaged suffix to the solution name. That’s as simple as composing the name

$unmanagedFileName = $solutionName + “_Unmanaged.zip”

But let’s make it even better. Every time we update the solution version in CRM, we could update the script to capture the solution version in the exported solution file name. But what if we can do that in a more automated way? Of course, it fully relies on the version defined in your instance, but you don’t have to manually update your script.

Actually, where I’ve seen this used first and inspired the idea is in one of Jason’s posts HERE.

Digging a little through the script he’s put up, you’ll find the ability to use a FetchXML query to retrieve what you need. So, the updated script is:

# Get the version for the source solution

$returnSolutionVersion = “”

$solutionVersion = Get-CrmRecordsByFetch -Fetch “<fetch><entity name=’solution’><attribute name=’friendlyname’ /><attribute name=’version’ /><filter><condition attribute=’friendlyname’ operator=’eq’ value=’$solutionName’ /></filter></entity></fetch>” -conn $Crm1 -TopCount 1

if ($solutionVersion.CrmRecords.Count -eq 1) {

$returnSolutionVersion = $solutionVersion.CrmRecords[0].version.Replace(“.”,”_”)

}

And now, you can actually compose the name of the solution you are saving, including the solution name, the version as it exists in your instance, as well as whether it’s a managed or unmanaged package.

$unmanagedFileName = $solutionName + “_” + $returnSolutionVersion + “_Unmanaged.zip”

OR

$managedFileName = $solutionName + “_” + $returnSolutionVersion + “_Managed.zip”

You plug that back into your original Export-CrmSolution line and there you have it. Now you can run one script, and have both unmanaged and managed solutions exported, including the version number at the time of the export.

Neat, eh?