Automate your Dynamics CRM Builds and Deployments with PowerShell

Using the xRM CI Framework and Windows PowerShell you can easily extend the build and automate the deployment of your CRM Solutions.

PowerShell allows you to automate various processes by scripting the tasks that are normally performed manually on servers and applications. PowerShell is built on the .NET Framework. That allows you to use any .NET library in your scripts and allows you to work with and manipulate standard .NET objects in a friendly syntax that is close to C# and JavaScript.

Microsoft provides lots of PowerShell commands “cmdlets” that allow you to manage Windows Servers (IIS, File System, Registry) and applications such as Microsoft Office.

The Dynamics CRM SDK includes some PowerShell cmdlets but these are limited to managing CRM organizations through the Deployment Web Service. Check this post for more information about these.

The xRM CI Framework provides you with PowerShell “cmdlets” to allow you to script some of the task that are done manually during the build and/or deployment of Dynamics CRM Solutions.

Using the custom “cmdlets” you are able to assemble a script that can be triggered during your TFS build process or executed on demand from your desktop or other automation tools.

Below is a list of “cmdlets” that are available in the initial release:

CmdLet Name Description SDK Message
Import-XrmSolution Imports a CRM Solution ImportSolutionRequest
Export-XrmSolution Exports a managed or unmanaged CRM Solution ExportSolutionRequest
Get-XrmSolution Retrieves CRM Solution details using the unique solution name Not Applicable
Publish-XrmCustomizations Publishes all customizations for a CRM organization PublishAllXmlRequest
Add-XrmEntity Create a new entity in CRM CreateRequest
New-XrmEntity Creates a new instance of the Entity class Not Applicable
Set-XrmEntity Updates an existing entity in CRM UpdateRequest
Remove-XrmEntity Deletes an existing entity from CRM DeleteRequest

Note that most cmdlets will accept the “ConnectionString” parameter. The format is based on the SDK connection string and the cmdlet will use that parameter to connect to CRM.

To get started follow the setup instructions in my previous post.

To get more information about the parameters each cmdlet provides, run the Get-Help cmdlet as shown below from the PowerShell Console. More information on PowerShell can be found in  the PowerShell getting started manual. All the parameters are inline with the Dynamics CRM SDK messages of the corresponding cmdlets as shown in the table above.


The next step is to write your first PowerShell Script. Below is a sample script that checks the version of the currently deployed CRM solution and updates the target system if the version deployed is older than the latest version.

Write-Host “Deployment Started…”

# Import the xRM CI Framework Dynamics CRM Cmdlets
Import-Module “C:\Program Files (x86)\Xrm CI Framework\CRM 2011\PowerShell Cmdlets\Xrm.Framework.CI.PowerShell.dll”

#Define the CRM connection
$targetCrmConnectionUrl = “ServiceUri=http://test/testorg/XRMServices/2011/Organization.svc`;”

# Retrieve the solution details from CRM

$testSolution = Get-XrmSolution -ConnectionString $targetCrmConnectionUrl -UniqueSolutionName “TestSolution”

if ($testSolution -eq $null)
Write-Host “Test Solution not currently installed”
Write-Host “Current Test Solution version: “ -NoNewline $testSolution.Version

if (($testSolution -eq $null) -or ($testSolution.Version -ne “″))
$importPath = “”
Write-Host “Importing Solution: $importPath”

# Import CRM Solution
Import-XrmSolution -ConnectionString $targetCrmConnectionUrl -SolutionFilePath $importPath -PublishWorkflows $true -OverwriteUnmanagedCustomizations $true
Write-Host “Skipped import of Test Solution”

This library contains only a subset of what you can do with Dynamics CRM. You can expose any Dynamics CRM SDK message as a PowerShell cmdlet and use it to automate anything that you could be doing manually today.

Having your builds and deployment automated will allow you to build and deploy quickly and will give you the added consistency and reliability.

In future posts, I will show you how you can implement continuous integration by combining the automated builds discussed in this post and the PowerShell cmdlets for Dynamics CRM.

About these ads

Posted on August 20, 2013, in CRM 2011, Dynamics CRM, xRM CI Framework and tagged , , , , . Bookmark the permalink. 23 Comments.

  1. Hi Wael,
    I am getting the following error while importing the solution using powershell.

    Import-XrmSolution : The HTTP request is unauthorized with client authentication scheme ‘Anonymous’. The authentication header received from the server was ‘Negotiate,NTLM’

    please help me to resolve this issue.

  2. During Import XRM Soloution
    Then the PowerShell throws following Exception:

    Import-XrmSolution : The request channel timed out while waiting for a reply after 00:01:59.8729770. Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. The time allotted to this operation may have been a portion of a longer timeout. At C:\Users\rkhalid\Desktop\PowerShell Scripts\Automated.ps1:35 char:5 + Import-XrmSolution -ConnectionString $ConUrl -SolutionFilePath $path -Overwr … + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Import-XrmSolution], TimeoutException + FullyQualifiedErrorId : System.TimeoutException,Xrm.Framework.CI.PowerShell.ImportXrmSolutionCommand

    I don’t know how to Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding ?

  3. When using Export-XrmSolution can you specify the solution name to be used for the exported file? I would love to use date like names for my exported solutions, rather than their original name and version number. Or even append the version number when saving…

  4. Any examples that would demonstrate proper usage of Set-XrmEntity? Say, to simply update a text field on a record?

    • You have to use the New-XrmEntity to create an entity. I will add a retrieve cmdlet shortly.
      Then you just need to set the properties and do the update. Don’t have samples yet. Something like the below.

      record = New-XrmEntity -EntityName “contact”
      record["textfield"] = “Hello”
      record.Id = id
      Set-XrmEntity -EntityObject record -ConnectionString “yourconnection”

  5. Hi! I’m getting the following error when running the sample script that you provided above:

    Get-XrmSolution : The specified type ‘Xrm.Framework.CI.Common.Entities.Solution’ is not a known entity type.
    At C:\Users\******l\Desktop\test.ps1:16 char:17
    + $testSolution = Get-XrmSolution -ConnectionString $targetCrmConnectionUrl -Uniqu …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Get-XrmSolution], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException,Xrm.Framework.CI.PowerShell.GetXrmSolutionCommand

    I’ve installed the framework on this machine. Could you give me some help about this?

    • Hi, Is there Xrm.Framework.CI.Common.dll and other dependencies in the same folder as the rest of the dlls?

    • You need to have the Visual studio IDE dlls in your machine.
      Import-Module “Microsoft.TeamFoundation.Build.Client.dll”
      Import-Module “Microsoft.TeamFoundation.VersionControl.Client.dll”
      Import-Module “Microsoft.TeamFoundation.Client.dll”
      Import-Module “Microsoft.TeamFoundation.Common.dll”
      Import-Module “Microsoft.TeamFoundation.VersionControl.Common.dll”

      You can get the dll from the below location in a machine having visual studio installed.

      C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ReferenceAssemblies\v2.0

      • I will be removing this dependency in the next couple of weeks. By the way you just need the Dll in the same location you don’t need to import all of the above.

  6. Hi waelhamze, Thanks for this very useful post.

    I am getting the below error while trying to do an Import
    Import-XrmSolution : Plugin Assemblies import: FAILURE
    At line:5 char:1
    + Import-XrmSolution -ConnectionString $targetCrmConnectionUrl -SolutionFilePath $ …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Import-XrmSolution], FaultException`1
    + FullyQualifiedErrorId : System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Micro
    soft.Xrm.Sdk, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35]],Xrm.Framework.CI.PowerShell.Imp

    Any idea on this error?Is there a way to get the logs or have a detailed error?


  7. please help
    PS C:\Program Files (x86)\xRM CI Framework\CRM 2013\PowerShell Cmdlets> Import-Module .\Xrm.Framework.CI.PowerShell.dll
    Import-Module : Could not load file or assembly ‘file:///C:\Program Files (x86)\xRM CI Framework\CRM 2013\PowerShell Cm
    dlets\Xrm.Framework.CI.PowerShell.dll’ or one of its dependencies. This assembly is built by a runtime newer than the c
    urrently loaded runtime and cannot be loaded.
    At line:1 char:14
    + Import-Module <<<< .\Xrm.Framework.CI.PowerShell.dll
    + CategoryInfo : NotSpecified: (:) [Import-Module], BadImageFormatException
    + FullyQualifiedErrorId : System.BadImageFormatException,Microsoft.PowerShell.Commands.ImportModuleCommand

    • Hi Sergiy,

      You need to change the powershell runtime as outline in the setup instructions on my blog or use the latest version of powershell that uses .NET 4.0 runtime.

  8. I was trying to use the cmdlet to import an org and it complained that it couldn’t load a bunch of Microsoft.TeamFoundation. I have VS installed, but no those DLLs – don’t use TFS.

    Could you make seperate cmdlets? One for managing CRM solutions and another for managing TFS templates?

    Or put the src on codeplex?

    • Hi Pete,

      This is exactly what I am doing right now. Removing the dependency on build. Will also publish to codeplex once it is ready. Watch out for notifications.

  9. I am getting following error while trying to import CRM solution form one server(CRM Instance) to another Server(CRM Instance), can you please suggest ??

    Import-XrmSolution : Import failed
    At line:1 char:1
    + Import-XrmSolution -ConnectionString “ServiceUri=http://……….
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Import-XrmSolution], FaultException`1
    + FullyQualifiedErrorId : System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFau
    lt, Microsoft.Xrm.Sdk, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35]],Xrm.Framework

    • Is this for Dynamics CRM 2013/2011 online/on-premise? Do you have more details with regards to the error? I suppose would be good to know if you can import this solution manually first to try and narrow down the problem.

      • Thanks for the response…
        I have two CRM 2013 On-premise Instance.

        First CRM 2013 On-Premise instance:where Release manager & xRM CI Framework already installed.
        -Here in the first instace when I tried to import the solution using PowerShell and xRM CI Framework , it is successfully Imported.
        But when I changec the connection string and call the organization service of second instance to import the solution there I am geeting the follwoing error

        this line in the powershell script :
        # Import CRM Solution
        Import-XrmSolution -ConnectionString $targetCrmConnectionUrl -SolutionFilePath $importPath -PublishWorkflows $true -OverwriteUnmanagedCustomizations $true

        Import-XrmSolution : Import failed
        At line:1 char:1
        + Import-XrmSolution -ConnectionString “ServiceUri=http://……….
        + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo : NotSpecified: (:) [Import-XrmSolution], FaultException`1
        + FullyQualifiedErrorId : System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFau
        lt, Microsoft.Xrm.Sdk, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35]],Xrm.Framework

        I tried to import the soultion manually into the second crm instance I won’t get any error.

        Please suggest.

  1. Pingback: On Technology with Mitch Milam » PowerShell and Dynamics CRM Administration

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 44 other followers

%d bloggers like this: