Quantcast
Channel: Wael Hamze » Dynamics CRM
Viewing all articles
Browse latest Browse all 20

Automate your Dynamics CRM Builds and Deployments with PowerShell

$
0
0

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.

Get-Help

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”
}
else
{
Write-Host “Current Test Solution version: “ -NoNewline $testSolution.Version
}

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

# Import CRM Solution
Import-XrmSolution -ConnectionString $targetCrmConnectionUrl -SolutionFilePath $importPath -PublishWorkflows $true -OverwriteUnmanagedCustomizations $true
}
else
{
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.



Viewing all articles
Browse latest Browse all 20

Trending Articles