Once you are familiar with the Cmdlets, you can start combining these along with other Cmdlets available on the Windows Platform or provided by Third Parties to create scripts that can automate some of the tasks that you do manually today to build or deploy your Dynamics CRM solutions.

Exporting your CRM Solution using PowerShell

  1. You can use the Windows PowerShell ISE “powershell_ise.exe” to create your PowerShell Script. This provides Syntax highlighting and allows you to debug your PowerShell scripts.
  2. Let’s start. Declare any parameters you might need. These will make your script re-usable. For example in this case I am going to declare the connection string as a parameter, this will allow me to export my Sample solution from multiple environments.
  3. Load the xRM CI Framework similar to what I have done in the first post. In this case the script is loading the PowerShell Module from the same directory to which I will be saving my script. Feel free to store the xRM CI Framework in a shared location and reference that location from your scripts.
  4. Finally the important part. Use the “Export-XrmSolution” to export your Solution. In this case I am exporting the “Sample” solution to “C:\Temp” as managed. There are some optional parameters that you can still pass to this Cmdlet to specific other export options. Now your script should be ready see below.

    001
    002
    003
    004
    005
    006
    007
    008
    009
    010
    011
    012
    013
    014
    015
    016
    017
    018
    019
    020
    021
    022
    023
    024
    025
    026
    #Define Parameters
    param(
    [string]$CrmConnectionString
    )

    $ErrorActionPreference = "Stop"

    Write-Host "CrmConnectionString: $CrmConnectionString"

    #Script Location
    $scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
    Write-Host "Script Path: $scriptPath"

    #Load XrmCIFramework
    $xrmCIToolkit = $scriptPath + "\Xrm.Framework.CI.PowerShell.dll"
    Write-Host "Importing CIToolkit: $xrmCIToolkit" 
    Import-Module $xrmCIToolkit
    Write-Host "Imported CIToolkit"


    #Export Solution
    Write-Host "Exporting Managed Solution"
           
    $exportedFile = Export-XrmSolution -ConnectionString $CrmConnectionString -IncludeVersionInName $true -UniqueSolutionName "Sample" -OutputFolder "C:\Temp" -Managed $true
           
    Write-Host "Managed Solution Exported $exportedFile"
  5. Save the script to the same directory as the PowerShell assemblies and call it “ExportSampleSolution.ps1”
    image
  6. Let’s execute the script using PowerShell. You just need to pass the connection string as a parameter to this script.
  7. You can now use a similar approach to write more complex scripts to automate some of your manual activities.

Last edited Aug 5, 2014 at 10:20 PM by waelhamze, version 13