Deploying Managed and Unmanaged Solutions using PowerShell

Loading

In Power Platform, solutions help package and distribute customizations across environments. Using PowerShell, you can automate the deployment of managed and unmanaged solutions efficiently.

Key Differences Between Managed and Unmanaged Solutions

FeatureManaged SolutionUnmanaged Solution
EditabilityRead-only (cannot be modified)Fully editable
Use CaseUsed for deployment in productionUsed for development
CustomizationCannot be customized directlyCan be customized
RollbackDeletes all components upon removalComponents remain after deletion

Step 1: Prerequisites

1.1 Install Required PowerShell Modules

Ensure the Power Platform PowerShell modules are installed:

Install-Module -Name Microsoft.PowerApps.Administration.PowerShell -Force
Install-Module -Name Microsoft.PowerApps.PowerShell -Force

1.2 Authenticate to Power Platform

Sign in to Power Platform using:

Add-PowerAppsAccount

1.3 Identify Environment Name

Retrieve the Environment Name where the solution will be deployed:

Get-AdminPowerAppEnvironment | Select-Object DisplayName, EnvironmentName

Example Output:

DisplayName      EnvironmentName
------------ -----------------------------
DevelopmentEnv abcdef12-3456-7890-abcd-ef1234567890
ProductionEnv fedcba98-7654-3210-fedc-ba9876543210

Step 2: Exporting a Solution

Before deploying a solution, it must be exported from the source environment.

$sourceEnv = "abcdef12-3456-7890-abcd-ef1234567890"
$solutionName = "MySolution"
$exportPath = "C:\Solutions\$solutionName.zip"

# Export Managed Solution
Export-CrmSolution -SolutionName $solutionName -Managed -OutFile $exportPath -EnvironmentName $sourceEnv

# Export Unmanaged Solution
Export-CrmSolution -SolutionName $solutionName -OutFile $exportPath -EnvironmentName $sourceEnv

This exports the solution as either Managed or Unmanaged.


Step 3: Importing a Solution

Once exported, the solution must be imported into the target environment.

$targetEnv = "fedcba98-7654-3210-fedc-ba9876543210"
$solutionFile = "C:\Solutions\MySolution.zip"

Import-CrmSolution -ImportMode "Upgrade" -SolutionFilePath $solutionFile -EnvironmentName $targetEnv

The Upgrade mode ensures a seamless transition for the new version.


Step 4: Automating Solution Deployment

To deploy a solution across multiple environments, use the following PowerShell script.

Automated Solution Deployment Script

param (
[string]$sourceEnv = "abcdef12-3456-7890-abcd-ef1234567890",
[string]$targetEnv = "fedcba98-7654-3210-fedc-ba9876543210",
[string]$solutionName = "MySolution",
[string]$solutionPath = "C:\Solutions\$solutionName.zip"
)

# Authenticate
Add-PowerAppsAccount

# Export Solution
Export-CrmSolution -SolutionName $solutionName -Managed -OutFile $solutionPath -EnvironmentName $sourceEnv
Write-Host "Solution '$solutionName' exported successfully."

# Import Solution
Import-CrmSolution -ImportMode "Upgrade" -SolutionFilePath $solutionPath -EnvironmentName $targetEnv
Write-Host "Solution '$solutionName' imported successfully into environment '$targetEnv'."

Save this script as Deploy-Solution.ps1 and run:

powershell.exe -File "C:\Scripts\Deploy-Solution.ps1"

Step 5: Scheduling Automatic Solution Deployment

To automate solution deployment, use Windows Task Scheduler.

  1. Open Task Scheduler (taskschd.msc).
  2. Click Create Basic Task → Name it Deploy Power Platform Solution.
  3. Set Trigger (e.g., weekly or daily).
  4. Choose Start a Program, and enter: powershell.exe -File "C:\Scripts\Deploy-Solution.ps1"
  5. Click Finish.

Now, solutions will be automatically deployed on schedule.


Step 6: Verifying Deployment

Confirm that the solution was deployed successfully:

Get-CrmSolutions -EnvironmentName $targetEnv | Where-Object {$_.UniqueName -eq "MySolution"}

Expected Output:

FriendlyName       UniqueName      Version
------------- ------------ ---------
MySolution MySolution 1.0.0.2

Leave a Reply

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