Exporting and importing solution using PowerShell

 

 

Powershell scripts are used with data centres, clouds, virtual machines, servers and applications to perform a variety of administration tasks, and Dynamics 365 is not an exception. The following script can be used to export a solution from one environment and import into other. It works with managed, unmanaged, and patch solutions. Execution of script can be triggered manually or it can be put into release pipeline.

About Script

This script uses Microsoft.Xrm.Data.Powershell (it is a great module to work with Dynamics 365 implemented by Sean McNellis and Kenichiro Nakamura). The script will attempt to install the module in case it is missing. Don’t forget to update CRM credentials, URLs and solution name in code.


#Update name of solution in below line, instead of rte_V1 add name of solution
$solutionName =”rte_V1″
$env:TEMP
Set-StrictMode -Version latest
function InstallModule{
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force
$moduleName = “Microsoft.Xrm.Data.Powershell”
$moduleVersion = “2.7.2”
if (!(Get-Module -ListAvailable -Name $moduleName )) {
Write-host “Module Not found, installing now”
$moduleVersion
Install-Module -Name $moduleName -MinimumVersion $moduleVersion -Force
}
else
{
Write-host “Module Found”
}
}
function GetCrmConn{
param(
[string]$user,
[string]$secpasswd,
[string]$crmUrl)
Write-Host “UserId: $user Password: $secpasswd CrmUrl: $crmUrl”
$secpasswd2 = ConvertTo-SecureString -String $secpasswd -AsPlainText -Force
write-host “Creating credentials”
$mycreds = New-Object System.Management.Automation.PSCredential ($User, $secpasswd2)
write-host “Credentials object created”
write-host “Establishing crm connection next”
$crm = Connect-CrmOnline -Credential $mycreds -ServerUrl $CrmUrl
write-host “Crm connection established”
return $crm
}
InstallModule
#Update Source CRM instance details below:
Write-Host “going to create first connection”
$Crm1 = GetCrmConn -user “user@crm1st.onmicrosoft.com” -secpasswd “P@ssword001122” -crmUrl “https://crm1st.crm6.dynamics.com”
Write-Host “first connection created”
Set-CrmConnectionTimeout -conn $Crm1 -TimeoutInSeconds 1000
#Update target CRM instance below:
Write-Host “going to create second connection”
$Crm2 = GetCrmConn -user “user@crm2nd.onmicrosoft.com” -secpasswd “P@ssword001122” -crmUrl “https://crm.crm6.dynamics.com”
Write-Host “second connection created”
Write-Host “Exporting Solution”
Export-CrmSolution -conn $Crm1 -SolutionName “$solutionName” -SolutionFilePath $env:TEMP -SolutionZipFileName “$solutionName.zip”
Set-CrmConnectionTimeout -conn $Crm2 -TimeoutInSeconds 1000
Write-host “Importing Solution”
Import-CrmSolution -conn $Crm2 -SolutionFilePath “$env:TEMP\$solutionName.zip”
Write-host “Solution Imported”

I hope this is helpful.

Enjoy your 365 day.

About Me 🙂

I m an IT consultant working in Melbourne Australia. I solve business problems using Microsoft technologies (Dynamics 365, Office 365, Azure, Flow, Power Apps, Power BI). I m involved in community activities and I blog at http://www.crmtechie.com/

I love to get connected with people working in IT, providing solutions or who just like Microsoft technologies. To get in touch please follow my blog, and connect through Linkedin, Twitter or Facebook

Blog: http://www.crmtechie.com/

Twitter: @YawerIqbal

Linkedin: YawerIqbal

Facebook: Yawer.Iqbal